2025-10-28 13:29:34 +01:00
|
|
|
import { redirect } from "@sveltejs/kit";
|
|
|
|
|
import type { PageServerLoad } from "./$types";
|
2026-03-04 18:07:18 +01:00
|
|
|
import { gql } from "graphql-request";
|
|
|
|
|
import { getGraphQLClient } from "$lib/api";
|
|
|
|
|
|
|
|
|
|
const LEADERBOARD_QUERY = gql`
|
|
|
|
|
query Leaderboard($limit: Int, $offset: Int) {
|
|
|
|
|
leaderboard(limit: $limit, offset: $offset) {
|
|
|
|
|
user_id display_name avatar
|
|
|
|
|
total_weighted_points total_raw_points
|
|
|
|
|
recordings_count playbacks_count achievements_count rank
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`;
|
2025-10-28 13:29:34 +01:00
|
|
|
|
|
|
|
|
export const load: PageServerLoad = async ({ fetch, url, locals }) => {
|
|
|
|
|
// Guard: Redirect to login if not authenticated
|
|
|
|
|
if (!locals.authStatus.authenticated) {
|
|
|
|
|
throw redirect(302, "/login");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const limit = parseInt(url.searchParams.get("limit") || "100");
|
|
|
|
|
const offset = parseInt(url.searchParams.get("offset") || "0");
|
|
|
|
|
|
2026-03-04 18:07:18 +01:00
|
|
|
const client = getGraphQLClient(fetch);
|
|
|
|
|
const data = await client.request<{
|
|
|
|
|
leaderboard: {
|
|
|
|
|
user_id: string;
|
|
|
|
|
display_name: string | null;
|
|
|
|
|
avatar: string | null;
|
|
|
|
|
total_weighted_points: number | null;
|
|
|
|
|
total_raw_points: number | null;
|
|
|
|
|
recordings_count: number | null;
|
|
|
|
|
playbacks_count: number | null;
|
|
|
|
|
achievements_count: number | null;
|
|
|
|
|
rank: number;
|
|
|
|
|
}[];
|
|
|
|
|
}>(LEADERBOARD_QUERY, { limit, offset });
|
2025-10-28 13:29:34 +01:00
|
|
|
|
|
|
|
|
return {
|
2026-03-04 18:07:18 +01:00
|
|
|
leaderboard: data.leaderboard || [],
|
2025-10-28 13:29:34 +01:00
|
|
|
pagination: {
|
|
|
|
|
limit,
|
|
|
|
|
offset,
|
2026-03-04 18:07:18 +01:00
|
|
|
hasMore: data.leaderboard?.length === limit,
|
2025-10-28 13:29:34 +01:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Leaderboard load error:", error);
|
|
|
|
|
return {
|
|
|
|
|
leaderboard: [],
|
|
|
|
|
pagination: {
|
|
|
|
|
limit: 100,
|
|
|
|
|
offset: 0,
|
|
|
|
|
hasMore: false,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|