import type { Driver, Graphic, Race, Substitution, Team } from "$lib/schema"; import type { LayoutServerLoad } from "./$types"; // On each page load (every route), this function runs serverside. // The "locals.user" object is only available on the server, // since it's populated inside hooks.server.ts per request. // It will populate the "user" attribute of each page's "data" object, // so each page has access to the current user (or knows if no one is signed in). export const load: LayoutServerLoad = ({ locals }) => { const fetch_graphics = async (): Promise => { const graphics: Graphic[] = await locals.pb .collection("graphics") .getFullList({ fetch: fetch }); graphics.map((graphic: Graphic) => { graphic.file_url = locals.pb.files.getURL(graphic, graphic.file); }); return graphics; }; const fetch_teams = async (): Promise => { const teams: Team[] = await locals.pb.collection("teams").getFullList({ sort: "+name", fetch: fetch, }); teams.map((team: Team) => { team.banner_url = locals.pb.files.getURL(team, team.banner); team.logo_url = locals.pb.files.getURL(team, team.logo); }); return teams; }; const fetch_drivers = async (): Promise => { const drivers: Driver[] = await locals.pb.collection("drivers").getFullList({ sort: "+code", fetch: fetch, }); drivers.map((driver: Driver) => { driver.headshot_url = locals.pb.files.getURL(driver, driver.headshot); }); return drivers; }; const fetch_races = async (): Promise => { const races: Race[] = await locals.pb.collection("races").getFullList({ sort: "+step", fetch: fetch, }); races.map((race: Race) => { race.pictogram_url = locals.pb.files.getURL(race, race.pictogram); }); return races; }; const fetch_substitutions = async (): Promise => { const substitutions: Substitution[] = await locals.pb.collection("substitutions").getFullList({ expand: "race", fetch: fetch, }); // Sort by race step (ascending) substitutions.sort( (a: Substitution, b: Substitution) => a.expand.race.step - b.expand.race.step, ); return substitutions; }; return { // User information user: locals.user, admin: locals.user?.admin ?? false, // Return static data asynchronously graphics: fetch_graphics(), teams: fetch_teams(), drivers: fetch_drivers(), races: fetch_races(), substitutions: fetch_substitutions(), }; };