import { useEffect, useState } from 'react'; import type { WorkSession } from '@solelog/shared'; import { useActiveSessions } from '../api/admin-sessions'; import { formatTime } from '../lib/elapsed'; export default function Live() { const { data, isLoading, isError } = useActiveSessions(); // Client-side 1s tick so the elapsed timer on each card keeps counting up // between the 5s server refreshes. const [now, setNow] = useState(() => Date.now()); useEffect(() => { const id = setInterval(() => setNow(Date.now()), 1000); return () => clearInterval(id); }, []); if (isLoading) { return (

Laden…

); } if (isError) { return (

Kon gegevens niet laden.

); } const sessions = Array.isArray(data) ? data : []; return (

Actief nu ({sessions.length})

{sessions.length === 0 ? (

Niemand is nu aan het werk.

) : (
{sessions.map((session) => ( ))}
)}
); } function LiveCard({ session, now }: { session: WorkSession; now: number }) { // While paused the worked timer freezes at the pause moment; otherwise it counts to now. // Worked = (base - start) - paused_seconds, where base is the pause moment when paused. const base = session.paused_at ? Date.parse(session.paused_at) : now; const worked = Math.max( 0, Math.floor((base - Date.parse(session.start_time)) / 1000) - session.paused_seconds ); return (
{session.user_name ?? 'Onbekend'} {session.insole_type && {session.insole_type}}
{session.activity_name ?? 'Onbekende handeling'}
{session.pair_count} zolen
{formatTime(worked)}
{session.paused_at && Gepauzeerd} {session.paused_seconds > 0 && ( Pauze {formatTime(session.paused_seconds)} )}
); }