From 5ecfcec5514a6e4c2e04f375e313b67c9f7cc867 Mon Sep 17 00:00:00 2001 From: Sanyam Jain Date: Sun, 11 Aug 2024 23:41:24 -0700 Subject: [PATCH] add measure page --- essays/turso.md | 5 +++ src/lib/components/Heatmap/Heatmap.svelte | 11 ++++--- src/lib/components/PrimaryNav.svelte | 11 ++----- src/lib/db/tables/card.table.ts | 16 ++++----- .../(protected)/measure/+page.server.ts | 11 +++++++ src/routes/(protected)/measure/+page.svelte | 17 ++++++++++ .../(protected)/measure/HeatmapCard.svelte | 33 +++++++++++++++++++ src/routes/(protected)/measure/Streak.svelte | 25 ++++++++++++++ src/routes/(protected)/record/+page.server.ts | 10 +----- src/routes/(protected)/record/+page.svelte | 7 +--- src/routes/(protected)/revise/+page.server.ts | 4 +-- src/routes/(protected)/revise/+page.svelte | 7 ---- src/routes/+layout.svelte | 2 +- 13 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 essays/turso.md create mode 100644 src/routes/(protected)/measure/+page.server.ts create mode 100644 src/routes/(protected)/measure/+page.svelte create mode 100644 src/routes/(protected)/measure/HeatmapCard.svelte create mode 100644 src/routes/(protected)/measure/Streak.svelte diff --git a/essays/turso.md b/essays/turso.md new file mode 100644 index 0000000..523c426 --- /dev/null +++ b/essays/turso.md @@ -0,0 +1,5 @@ +# Turso commands + +``` +turso db shell +``` diff --git a/src/lib/components/Heatmap/Heatmap.svelte b/src/lib/components/Heatmap/Heatmap.svelte index 2de51f9..22834a5 100644 --- a/src/lib/components/Heatmap/Heatmap.svelte +++ b/src/lib/components/Heatmap/Heatmap.svelte @@ -9,13 +9,13 @@ let divElement: HTMLDivElement; const currentDate = new Date(); - currentDate.setMonth(currentDate.getMonth() - 3); + currentDate.setMonth(currentDate.getMonth() - 12); onMount(() => { cal = new CalHeatmap(); cal.paint( { itemSelector: divElement, - range: 4, // show 6 months + range: 13, // show 13 months (current month + 12 months back) domain: { type: 'month', gutter: 4, @@ -35,7 +35,7 @@ scale: { color: { type: 'threshold', - range: ['#14432a', '#166b34', '#37a446', '#4dd05a'], + range: ['#4dd05a', '#37a446', '#166b34', '#14432a'], domain: [10, 20, 30] } } @@ -57,7 +57,10 @@
diff --git a/src/lib/components/PrimaryNav.svelte b/src/lib/components/PrimaryNav.svelte index 52c4d47..3ea67ae 100644 --- a/src/lib/components/PrimaryNav.svelte +++ b/src/lib/components/PrimaryNav.svelte @@ -11,11 +11,12 @@ import { page } from '$app/stores'; import Google from '$lib/components/Buttons/Google.svelte'; import { ROUTES } from '$lib/routes.util'; - import Heatmap from '$lib/components/Heatmap/Heatmap.svelte'; + import BodyMeasures from 'virtual:icons/arcticons/body-measures'; const signedInLinks = [ { href: '/record', text: 'record', icon: JotTextEditor }, { href: '/revise', text: 'revise', icon: SoloLearn }, + { href: '/measure', text: 'measure', icon: BodyMeasures }, { href: '/learn', text: 'generate with ai', icon: BrainF } ]; const signedOuLinks = [ @@ -30,7 +31,6 @@ ]; export let user; - export let groupedInfo; let links; if (user) { @@ -85,11 +85,4 @@ {/if} - diff --git a/src/lib/db/tables/card.table.ts b/src/lib/db/tables/card.table.ts index b45855a..77812bd 100644 --- a/src/lib/db/tables/card.table.ts +++ b/src/lib/db/tables/card.table.ts @@ -35,16 +35,16 @@ export const getCardsOrderByCreated = async (userId: string) => { return data } -// export const getCardsGroupedByCreated = async (userId: string) => { -// console.time('getCardsGroupedByCreated') -// const data = await db.select({ date: sql`DATE(${activityTable.createdAt}, 'unixepoch')`, count: count() }).from(activityTable).where(and(eq(activityTable.userId, userId), eq(activityTable.action, 'INSERT'))).groupBy(sql`DATE(${activityTable.createdAt}, 'unixepoch')`) -// console.timeEnd('getCardsGroupedByCreated') -// return data -// } +export const getCardsRecorded = async (userId: string) => { + console.time('getCardsGroupedByCreated') + const data = await db.select({ date: activityTable.createdAt }).from(activityTable).where(and(eq(activityTable.userId, userId), eq(activityTable.action, 'INSERT'))) + console.timeEnd('getCardsGroupedByCreated') + return data +} -export const getCardsGroupedByUpdated = async (userId: string) => { +export const getCardsReviewed = async (userId: string) => { console.time('getCardsGroupedByUpdated') - const data = await db.select({ date: sql`DATE(${activityTable.createdAt}, 'unixepoch')`, count: count() }).from(activityTable).where(and(eq(activityTable.userId, userId), eq(activityTable.action, 'UPDATE'))).groupBy(sql`DATE(${activityTable.createdAt}, 'unixepoch')`) + const data = await db.select({ date: activityTable.createdAt }).from(activityTable).where(and(eq(activityTable.userId, userId), eq(activityTable.action, 'UPDATE'))) console.timeEnd('getCardsGroupedByUpdated') return data } diff --git a/src/routes/(protected)/measure/+page.server.ts b/src/routes/(protected)/measure/+page.server.ts new file mode 100644 index 0000000..dd4e592 --- /dev/null +++ b/src/routes/(protected)/measure/+page.server.ts @@ -0,0 +1,11 @@ +import { getCardsRecorded, getCardsReviewed } from '$lib/db/tables/card.table.js'; + +export async function load({ locals }) { + const reviewedInfo = getCardsReviewed(locals.user!.id) + const recordedInfo = getCardsRecorded(locals.user!.id) + + return { + reviewedInfo, + recordedInfo + }; +} \ No newline at end of file diff --git a/src/routes/(protected)/measure/+page.svelte b/src/routes/(protected)/measure/+page.svelte new file mode 100644 index 0000000..09640b7 --- /dev/null +++ b/src/routes/(protected)/measure/+page.svelte @@ -0,0 +1,17 @@ + + +
+ {#await data.reviewedInfo then info} +
+ +
+ + {/await} + {#await data.recordedInfo then info} + + {/await} +
diff --git a/src/routes/(protected)/measure/HeatmapCard.svelte b/src/routes/(protected)/measure/HeatmapCard.svelte new file mode 100644 index 0000000..4e803f3 --- /dev/null +++ b/src/routes/(protected)/measure/HeatmapCard.svelte @@ -0,0 +1,33 @@ + + +
+

{title}

+
+ +
+
diff --git a/src/routes/(protected)/measure/Streak.svelte b/src/routes/(protected)/measure/Streak.svelte new file mode 100644 index 0000000..1c0d2dd --- /dev/null +++ b/src/routes/(protected)/measure/Streak.svelte @@ -0,0 +1,25 @@ + + +
+

Current Streak: {calculateStreak(data)}

+
diff --git a/src/routes/(protected)/record/+page.server.ts b/src/routes/(protected)/record/+page.server.ts index 92b339e..3a54383 100644 --- a/src/routes/(protected)/record/+page.server.ts +++ b/src/routes/(protected)/record/+page.server.ts @@ -1,28 +1,20 @@ -import { redirect } from "@sveltejs/kit"; import { superValidate } from "sveltekit-superforms"; import { zod } from "sveltekit-superforms/adapters"; import { ROUTES } from "$lib/routes.util.js"; import { cardAddSchema } from "$lib/schemas.js"; -import { sessionExists } from "$lib/common.util.js"; import { addAction, deleteAction } from "$lib/actions/card.action.js"; import { getCardsOrderByCreated } from "$lib/db/tables/card.table.js"; export async function load({ locals }) { - if (!sessionExists(locals)) { - redirect(302, ROUTES.LOGIN) - } - const addForm = await superValidate(zod(cardAddSchema)); - const cards = await getCardsOrderByCreated(locals.user.id) - // const groupedInfo = await getCardsGroupedByCreated(locals.user.id) + const cards = await getCardsOrderByCreated(locals.user!.id) return { addForm, cards, - // groupedInfo }; } diff --git a/src/routes/(protected)/record/+page.svelte b/src/routes/(protected)/record/+page.svelte index e7ffdb8..05043f9 100644 --- a/src/routes/(protected)/record/+page.svelte +++ b/src/routes/(protected)/record/+page.svelte @@ -1,5 +1,4 @@ @@ -7,9 +6,3 @@ {#await data.cards then cards} {/await} - -{#await data.groupedInfo then groupedInfo} -
- -
-{/await} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index db24f22..81f3d3b 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -21,7 +21,7 @@ $: webManifestLink = pwaInfo ? pwaInfo.webManifest.linkTag : ''; - +