"use client"; import { useEffect, useRef, useState } from "react"; import { useDailyGame } from "../../lib/useDailyGame"; import { fmt } from "../../lib/utils"; import { ArticleView } from "./ArticleView"; import { Breadcrumbs } from "./Breadcrumbs"; import { ShareBar } from "./ShareBar"; const MEDALS = ["đŸ„‡", "đŸ„ˆ", "đŸ„‰"]; type LeaderboardEntry = { rank: number; name: string; clicks: number; timeSeconds: number; userId: string }; export function DailyScreen({ onBack, currentUserId }: { onBack: () => void; currentUserId?: string }) { const game = useDailyGame(); const breadcrumbEndRef = useRef(null); const [leaderboard, setLeaderboard] = useState([]); const [loadingLb, setLoadingLb] = useState(false); useEffect(() => { breadcrumbEndRef.current?.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "end" }); }, [game.history]); function fetchLeaderboard() { setLoadingLb(true); fetch("/api/daily/leaderboard") .then((r) => r.json()) .then(setLeaderboard) .finally(() => setLoadingLb(false)); } useEffect(() => { if (game.phase === "won" || game.phase === "gave_up" || game.phase === "already_played") { fetchLeaderboard(); } }, [game.phase]); const btnPrimary = "w-full min-h-11 rounded-xl text-sm font-semibold bg-[#7c3aed] text-white hover:bg-[#6d28d9] disabled:opacity-50 cursor-pointer transition-colors"; const btnGhost = "w-full min-h-11 rounded-xl text-sm font-semibold bg-[#242424] border border-[#2e2e2e] text-[#f0f0f0] hover:bg-[#1a1a1a] cursor-pointer transition-colors"; // Loading initial if (game.phase === "loading") return (
); // Erreur initiale if (game.loadError) return (

{game.loadError}

); // Résultat (won / gave_up / already_played) if (game.phase === "won" || game.phase === "gave_up" || game.phase === "already_played") { const result = game.myResult; return (
Défi du jour - {game.puzzle?.date}
{game.phase === "won" ? "🎉 RĂ©ussi !" : game.phase === "gave_up" ? "😔 AbandonnĂ©" : "✅ DĂ©jĂ  jouĂ©"}
{game.puzzle?.startArticle} → {game.puzzle?.targetArticle}
{result && (
{result.clicks}
clics
{fmt(result.timeSeconds)}
temps
{result.path.length > 0 && (
{result.path.map((t, i) => ( {i > 0 && â€ș} {t} ))}
)}
)} {/* Classement du jour */}

Classement du jour

{loadingLb ? (
) : leaderboard.length === 0 ? (

Aucun résultat pour l'instant.

) : (
{leaderboard.map((entry, i) => (
{MEDALS[i] ?? `#${entry.rank}`} {entry.name} {entry.clicks} clics · {fmt(entry.timeSeconds)}
))}
)}
{game.phase === "won" && result && ( )}
); } // Playing return (
{game.title &&

{game.title}

} {game.loading && (
Chargement...
)} {game.loadError && (

{game.loadError}

)} {!game.loading && !game.loadError && game.html && ( )}
{/* Bottom bar */}
🗓 DĂ©fi du jour · cible {game.puzzle?.targetArticle}
Clics {game.clicks}
{game.canGoBack && ( )}
); }