"use client"; import { useEffect, useState } from "react"; import { Avatar } from "@/components/avatar"; import { createClient } from "@/lib/supabase/client"; import type { ProfileRow } from "./page"; function computeSecondsLeft(expiresAt: string | null): number { if (!expiresAt) return 0; return Math.max(0, Math.round((new Date(expiresAt).getTime() - Date.now()) / 1000)); } function formatCountdown(seconds: number): string { const minutes = Math.floor(seconds / 60); const remainder = seconds % 60; return `${minutes}:${remainder.toString().padStart(2, "0")}`; } // Volontairement discret (une barre compacte, pas une grande carte) : ce // n'est pas le sujet principal de la page projetée, juste un rappel présent. export function GardienBar({ isJudge, holder, expiresAt, }: { isJudge: boolean; holder: ProfileRow | null; expiresAt: string | null; }) { const [secondsLeft, setSecondsLeft] = useState(() => computeSecondsLeft(expiresAt)); const [rerolling, setRerolling] = useState(false); useEffect(() => { const supabase = createClient(); let firedForThisExpiry = false; function tick() { const remaining = computeSecondsLeft(expiresAt); setSecondsLeft(remaining); if (remaining <= 0 && !firedForThisExpiry) { firedForThisExpiry = true; supabase.rpc("reroll_gardien", { p_force: false }); } } tick(); const id = setInterval(tick, 1000); return () => clearInterval(id); }, [expiresAt]); async function handleForceReroll() { setRerolling(true); const supabase = createClient(); await supabase.rpc("reroll_gardien", { p_force: true }); setRerolling(false); } return (
Gardien du Silence : {holder ? ( {holder.pseudo} ) : ( personne )} {expiresAt && {formatCountdown(secondsLeft)}} {isJudge && ( )}
); }