import { error, redirect } from '@sveltejs/kit'; import { deleteSegment, getSegment, segmentLeaderboard } from '$lib/server/db'; import type { Actions, PageServerLoad } from './$types'; import type { TrackPt } from '$lib/server/segments'; export const load: PageServerLoad = ({ params, locals }) => { const segment = getSegment(Number(params.id)); if (!segment) error(404, 'Segment not found'); const points = JSON.parse(segment.points) as TrackPt[]; return { segment: { ...segment, points: undefined }, latlngs: points.map((p) => [p.lat, p.lon] as [number, number]), leaderboard: segmentLeaderboard(segment.id), myUserId: locals.user?.id ?? null, canDelete: !!locals.user && (segment.creator_id === locals.user.id || segment.creator_id === null) }; }; export const actions: Actions = { delete: async ({ params, locals }) => { if (!locals.user) error(401, 'Not signed in'); deleteSegment(Number(params.id), locals.user.id); redirect(303, '/segments'); } };