"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import Header from "@/components/Header"; import { useRouter } from "next/navigation"; import { signOut } from "next-auth/react"; type DashboardProps = { user: any; messages: any[]; unreadCount: number; }; function formatDuration(seconds: number | null) { if (!seconds) return "—"; const m = Math.floor(seconds / 60); const s = seconds % 60; if (m === 0) return `${s}s`; return `${m}m${s.toString().padStart(2, "0")}`; } export default function MuayThaiDashboard({ user, messages, unreadCount, }: DashboardProps) { const router = useRouter(); const [media, setMedia] = useState([]); const [showMedia, setShowMedia] = useState(false); const [showHomework, setShowHomework] = useState(false); const [showMyPerf, setShowMyPerf] = useState(false); const profile = user?.muayThai; useEffect(() => { fetch("/api/media") .then((r) => r.json()) .then((data) => setMedia( (data || []).filter( (m: any) => m.visibility === "PUBLIC" || m.visibility === "MEMBERS_ONLY" || m.visibility === "MUAY_THAI_ONLY" ) ) ) .catch(() => {}); }, []); async function markAsRead(messageId: string) { try { await fetch("/api/messages", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ messageId }), }); router.refresh(); } catch {} } return (

👋 Salut, {user.name.split(" ")[0]} !

Voici ton espace Muay Thai

{/* Messages de Juanito */}

💬 Messages de Juanito{" "} {unreadCount > 0 && ( {unreadCount} nouveau(x) )}

{messages.length === 0 && (

Aucun message pour le moment. Juanito ne va pas tarder !

)} {messages.map((msg) => (
!msg.isRead && markAsRead(msg.id)} >

{msg.title}

{msg.content}

{new Date(msg.createdAt).toLocaleDateString("fr-FR", { day: "numeric", month: "long", year: "numeric", })}

{!msg.isRead && ( )}
))}
{/* Section Performance */}

🏅 Ta performance

{showMyPerf && { setShowMyPerf(false); router.refresh(); }} />}

🪢 Meilleur chrono corde

{formatDuration(profile?.jumpRopeBestTime)}

💪 100 burpees

{formatDuration(profile?.burpee100BestTime)}

Historique des entraînements

{profile?.sessions?.length === 0 && (

Aucun entraînement enregistré pour l'instant.

)}
{profile?.sessions?.map((s: any) => (
{new Date(s.date).toLocaleDateString("fr-FR", { day: "2-digit", month: "short", })} {s.jumpRopeTime && ( 🪢 {formatDuration(s.jumpRopeTime)} )} {s.burpee100Time && ( 💪 {formatDuration(s.burpee100Time)} )}
))}
{/* Actions rapides */}
📄 Certificat médical {user.certificates?.some((c: any) => c.status === "VALIDATED") ? "✓ Validé" : user.certificates?.some((c: any) => c.status === "PENDING") ? "⏳ En cours" : "À fournir"} 📅 Signaler une absence
{/* Médias */} {showMedia && (

🎬 Photos & vidéos des adhérents

{media.length === 0 && (

Pas encore de médias publiés.

)} {media.map((m) => m.type === "PHOTO" ? ( {m.title}

{m.title}

) : (
) )}
)} {/* Homework */} {showHomework && (

🏋️ Exercices à travailler

{user.homework?.length === 0 && (

Aucun exercice pour l'instant. Juanito va t'en envoyer !

)}
{user.homework?.map((h: any) => (

{h.title}

{h.description}

{h.videoUrl && (
))}
)}
); } function AddPerfForm({ profileId, onDone, }: { profileId?: string; onDone: () => void; }) { const [jumpRopeTime, setJumpRopeTime] = useState(""); const [burpee100Time, setBurpee100Time] = useState(""); const [notes, setNotes] = useState(""); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); function parseTimeToSeconds(input: string): number | null { // Accepte "3:45", "3m45", "45" (secondes) const clean = input.trim(); if (!clean) return null; const colonMatch = clean.match(/^(\d+):(\d{1,2})$/); if (colonMatch) { return parseInt(colonMatch[1]) * 60 + parseInt(colonMatch[2]); } const mMatch = clean.match(/^(\d+)m(\d{1,2})$/i); if (mMatch) { return parseInt(mMatch[1]) * 60 + parseInt(mMatch[2]); } const sMatch = clean.match(/^(\d+)s?$/i); if (sMatch) return parseInt(sMatch[1]); return null; } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!jumpRopeTime && !burpee100Time) { setError("Renseigne au moins un chrono"); return; } setSaving(true); setError(""); const payload: any = {}; const jr = parseTimeToSeconds(jumpRopeTime); const bp = parseTimeToSeconds(burpee100Time); if (jr !== null) payload.jumpRopeTime = jr; if (bp !== null) payload.burpee100Time = bp; if (notes) payload.notes = notes; const res = await fetch("/api/training", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (res.ok) { onDone(); } else { setError("Erreur lors de l'enregistrement"); setSaving(false); } } return (

⏱️ Enregistrer un chrono

setJumpRopeTime(e.target.value)} />
setBurpee100Time(e.target.value)} />
setNotes(e.target.value)} />
{error &&

{error}

}
); }