Skip to content

Commit 1f89fef

Browse files
committed
prefetch it
1 parent 183b252 commit 1f89fef

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

view/src/lib/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const DAY = 24 * 60 * 60 * 1000;
22
export const HOUR = 60 * 60 * 1000;
3+
export const MAX_DURATION = 6 * DAY;

view/src/routes/+page.server.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
import type { Visit } from '~/db/schema';
2-
import type { PageServerLoad } from './$types';
3-
4-
export const load: PageServerLoad = ({ fetch }) => {
5-
return {
6-
visits: fetch('/visits').then((res) => res.json() as Promise<Visit[]>)
7-
};
8-
};

view/src/routes/+page.svelte

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
<script lang="ts">
2-
import { HOUR, DAY } from '~/lib/consts';
2+
import { MAX_DURATION, HOUR, DAY } from '~/lib/consts';
33
import * as v from 'valibot';
44
import Dashboard from '~/pages/Dashboard.svelte';
55
import { type Kind, visit } from '~/share/schema';
66
import type { PageData } from './$types';
7-
type Props = { data: PageData };
8-
const { data }: Props = $props();
9-
let visits = $state(data.visits);
107
118
let kind: Kind | 'all' = $state('all');
129
let duration: number = $state(3 * HOUR);
1310
let lastFetch: Date = $state(new Date());
14-
$effect(() => {
15-
visits = fetch(`/visits?kind=${kind}&duration=${duration}`)
16-
.then((res) => res.json())
17-
.then((val) => {
18-
const parsed = v.safeParse(v.array(visit), val);
19-
if (!parsed.success) {
20-
console.error(val, parsed.issues);
21-
throw new Error(parsed.issues.toString());
22-
}
23-
return parsed.output;
24-
})
25-
.then((val) => {
26-
lastFetch = new Date();
27-
return val;
28-
});
29-
});
11+
const data = fetch(`/visits?kind=all&duration=${MAX_DURATION}`)
12+
.then((val) => {
13+
const parsed = v.safeParse(v.array(visit), val);
14+
if (!parsed.success) {
15+
console.error(val, parsed.issues);
16+
throw new Error(parsed.issues.toString());
17+
}
18+
return parsed.output;
19+
})
20+
.then((val) => {
21+
lastFetch = new Date();
22+
return val;
23+
});
24+
25+
const lower_limit = $derived(lastFetch.getTime() - duration);
26+
const visits = $derived(
27+
data.then((data) =>
28+
data.filter((val) => val.at.getTime() > lower_limit && (kind === 'all' || val.kind === kind))
29+
)
30+
);
3031
</script>
3132

3233
<header>

view/src/routes/visits/+server.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ServerLoad } from '@sveltejs/kit';
2-
import { and, eq, gte } from 'drizzle-orm';
2+
import { gte } from 'drizzle-orm';
33
import { drizzle } from 'drizzle-orm/d1';
44
import { visitsTable } from '~/db/schema';
55

@@ -10,19 +10,9 @@ export const GET: ServerLoad = async ({ url, platform }) => {
1010
`{"error": "failed to parse ${url.searchParams.get('duration')} to number"}`
1111
);
1212
const threshold = new Date(new Date().getTime() - duration);
13-
const kind = url.searchParams.get('kind') ?? 'all';
1413

1514
if (!platform) return new Response('platform not found');
1615
const db = drizzle(platform.env.DB);
17-
const resp = await db
18-
.select()
19-
.from(visitsTable)
20-
.where(
21-
and(
22-
eq(visitsTable.kind, kind !== 'all' ? kind : visitsTable.kind),
23-
gte(visitsTable.at, threshold)
24-
)
25-
)
26-
.all();
16+
const resp = await db.select().from(visitsTable).where(gte(visitsTable.at, threshold)).all();
2717
return new Response(JSON.stringify(resp));
2818
};

0 commit comments

Comments
 (0)