import { error, redirect } from '@sveltejs/kit'; import { createApiClient } from '$lib/api.server'; import { getErrorMessage } from '$lib/errors'; import type { PageServerLoad, Actions } from './$types'; export const load: PageServerLoad = async ({ fetch, url }) => { const api = createApiClient(fetch); const type = url.searchParams.get('type') ?? undefined; const readParam = url.searchParams.get('read'); const read = readParam !== null ? readParam === 'true' : undefined; const result = await api.GET('/api/notifications', { params: { query: { type: type as 'MENTION' | 'REPLY' | undefined, read, page: 0, size: 20 } } }); if (!result.response.ok) { const code = (result.error as unknown as { code?: string })?.code; throw error(result.response.status, getErrorMessage(code)); } const page = result.data!; const notifications = page.content ?? []; const unreadCount = notifications.filter((n) => !n.read).length; return { notifications, unreadCount, totalPages: page.totalPages ?? 1 }; }; export const actions: Actions = { 'mark-all': async ({ fetch }) => { const api = createApiClient(fetch); await api.POST('/api/notifications/read-all'); redirect(303, '/notifications'); } };