added infinite scrolling to frontend and displaying vote information

This commit is contained in:
jenz
2026-08-25 21:34:45 +02:00
parent ab481d6d65
commit 0634fde335
7 changed files with 240 additions and 25 deletions
@@ -1,8 +1,14 @@
import { NextRequest, NextResponse } from 'next/server';
import { searchMapNames } from '@/lib/queries';
const PAGE_SIZE = 100;
export async function GET(req: NextRequest) {
const q = req.nextUrl.searchParams.get('q')?.trim() ?? '';
const maps = await searchMapNames(q);
return NextResponse.json({ maps });
const offset = Math.max(0, Number(req.nextUrl.searchParams.get('offset')) || 0);
const maps = await searchMapNames(q, PAGE_SIZE, offset);
const hasMore = maps.length === PAGE_SIZE;
return NextResponse.json({ maps, hasMore });
}
@@ -11,6 +11,8 @@ interface PlayerRow {
last_connected: string | null;
}
const PAGE_SIZE = 100;
const TOTAL_MINUTES_SUBQUERY = `
(SELECT COALESCE(s.session_end_playtime_minutes, s.session_start_playtime_minutes)
FROM playtime_display_sessions s
@@ -28,28 +30,31 @@ const LAST_CONNECTED_SUBQUERY = `
export async function GET(req: NextRequest) {
const q = req.nextUrl.searchParams.get('q')?.trim() ?? '';
const sort = req.nextUrl.searchParams.get('sort') ?? 'recent';
const offset = Math.max(0, Number(req.nextUrl.searchParams.get('offset')) || 0);
// Tie-break on steamid too — without a fully deterministic ORDER BY,
// paging with LIMIT/OFFSET can show duplicates or skip rows whenever two
// players tie on the primary sort key (e.g. same last_seen timestamp).
const orderBy =
sort === 'playtime'
? 'total_minutes IS NULL, total_minutes DESC'
? 'total_minutes IS NULL, total_minutes DESC, p.steamid'
: sort === 'name'
? 'p.current_name'
: 'p.last_seen DESC';
? 'p.current_name, p.steamid'
: 'p.last_seen DESC, p.steamid';
let rows: PlayerRow[];
if (!q) {
// No search term: just list players, ordered per the chosen sort.
rows = await query<PlayerRow>(
`SELECT p.steamid, p.current_name, p.current_country, NULL AS matched_name,
${TOTAL_MINUTES_SUBQUERY},
${LAST_CONNECTED_SUBQUERY}
FROM playtime_display_players p
ORDER BY ${orderBy}
LIMIT 50`,
LIMIT ? OFFSET ?`,
[PAGE_SIZE, offset],
);
} else {
// Search matches steamid directly, or current/previous names.
const like = `%${q}%`;
rows = await query<PlayerRow>(
`SELECT p.steamid, p.current_name, p.current_country, h.player_name AS matched_name,
@@ -66,11 +71,15 @@ export async function GET(req: NextRequest) {
)
GROUP BY p.steamid
ORDER BY ${orderBy}
LIMIT 50`,
[like, like, like, like],
LIMIT ? OFFSET ?`,
[like, like, like, like, PAGE_SIZE, offset],
);
}
const players = await attachAvatars(rows);
return NextResponse.json({ players });
// A full page came back, so there might be more; a short page means we've
// hit the end. Simple and correct without needing a separate COUNT query.
const hasMore = rows.length === PAGE_SIZE;
return NextResponse.json({ players, hasMore });
}