fix cmd+r redirects

This commit is contained in:
Nikita Galaiko 2023-05-11 10:37:12 +02:00
parent f04505d8e7
commit 90f475d7e5
3 changed files with 23 additions and 18 deletions

View File

@ -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/`))
)

View File

@ -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}`
);
});

View File

@ -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,