diff --git a/src/routes/projects/[projectId]/+layout.svelte b/src/routes/projects/[projectId]/+layout.svelte index 4c5496a33..e6da9f5c2 100644 --- a/src/routes/projects/[projectId]/+layout.svelte +++ b/src/routes/projects/[projectId]/+layout.svelte @@ -8,7 +8,6 @@ import QuickCommitModal from '$lib/components/QuickCommitModal.svelte'; import { onMount } from 'svelte'; import { unsubscribe } from '$lib/utils'; - import { format } from 'date-fns'; import { PUBLIC_API_BASE_URL } from '$env/static/public'; import { events, hotkeys } from '$lib'; @@ -35,7 +34,7 @@ hotkeys.on('Meta+P', () => goto(`/projects/${$project.id}/`)), hotkeys.on('Meta+Shift+,', () => goto(`/projects/${$project.id}/settings/`)), hotkeys.on('Meta+R', () => - goto(`/projects/${$project.id}/player/${format(new Date(), 'yyyy-MM-dd')}`) + goto(`/projects/${$project.id}/player/`) ), hotkeys.on('a i p', () => goto(`/projects/${$project.id}/aiplayground/`)) ) diff --git a/src/routes/projects/[projectId]/player/+page.ts b/src/routes/projects/[projectId]/player/+page.ts index b66662e11..b8398db41 100644 --- a/src/routes/projects/[projectId]/player/+page.ts +++ b/src/routes/projects/[projectId]/player/+page.ts @@ -1,16 +1,19 @@ -import { redirect } from '@sveltejs/kit'; -import { format } from 'date-fns'; +import { error, redirect } from '@sveltejs/kit'; +import { format, compareDesc } from 'date-fns'; import type { PageLoad } from './$types'; import { wrapLoadWithSentry } from '@sentry/sveltekit'; export const load: PageLoad = wrapLoadWithSentry(async ({ parent, url }) => { const { sessions, projectId } = await parent(); - const date = format(new Date(), 'yyyy-MM-dd'); - const dateSessions = await sessions - .load() - .then((sessions) => - sessions.filter((session) => format(session.meta.startTimestampMs, 'yyyy-MM-dd') === date) - ); - const firstSession = dateSessions[dateSessions.length - 1]; - throw redirect(302, `/projects/${projectId}/player/${date}/${firstSession.id}${url.search}`); + const latestDate = await sessions.load().then((sessions) => + sessions + .map((session) => session.meta.startTimestampMs) + .sort(compareDesc) + .shift() + ); + if (!latestDate) throw error(404, 'No sessions found'); + throw redirect( + 302, + `/projects/${projectId}/player/${format(latestDate, 'yyyy-MM-dd')}/${url.search}` + ); }); diff --git a/src/routes/projects/[projectId]/player/[date]/+page.ts b/src/routes/projects/[projectId]/player/[date]/+page.ts index a1967d45d..74a55d28f 100644 --- a/src/routes/projects/[projectId]/player/[date]/+page.ts +++ b/src/routes/projects/[projectId]/player/[date]/+page.ts @@ -1,15 +1,18 @@ -import { redirect } from '@sveltejs/kit'; +import { redirect, error } from '@sveltejs/kit'; import { format } from 'date-fns'; -import { get } from '@square/svelte-store'; import type { PageLoad } from './$types'; import { wrapLoadWithSentry } from '@sentry/sveltekit'; export const load: PageLoad = wrapLoadWithSentry(async ({ parent, params, url }) => { const { sessions, projectId } = await parent(); - await sessions.load(); - const dateSessions = get(sessions).filter( - (session) => format(session.meta.startTimestampMs, 'yyyy-MM-dd') === params.date - ); + const dateSessions = await sessions + .load() + .then((sessions) => + sessions.filter( + (session) => format(session.meta.startTimestampMs, 'yyyy-MM-dd') === params.date + ) + ); + if (!dateSessions.length) throw error(404, 'No sessions found'); const firstSession = dateSessions[dateSessions.length - 1]; throw redirect( 302,