"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { api } from "@/lib/api/client"; import { GlassCard } from "@/components/ui/glass-card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { PageHeader } from "@/components/dashboard/page-header"; import { Trash2, ExternalLink } from "lucide-react"; type League = { id: string; name: string; description: string | null; slug: string; }; export function MasterLeaguesClient({ initialLeagues }: { initialLeagues: League[] }) { const router = useRouter(); const [leagues, setLeagues] = useState(initialLeagues); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [loading, setLoading] = useState(false); const [deleteTarget, setDeleteTarget] = useState(null); const [error, setError] = useState(null); async function handleCreate(e: React.FormEvent) { e.preventDefault(); if (loading) return; setLoading(true); setError(null); try { const created = (await api.leagues.create({ name: name.trim(), description: description.trim() || undefined, })) as League; setLeagues((prev) => [created, ...prev]); setName(""); setDescription(""); router.refresh(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to create"); } finally { setLoading(false); } } return (
{error &&

{error}

}
setName(e.target.value)} required className="mt-1" placeholder="Sunday League" />
setDescription(e.target.value)} className="mt-1" />
{leagues.map((l) => ( ))}
Name ID Actions
{l.name} {l.id.slice(0, 8)}…
!o && setDeleteTarget(null)} title="Delete league?" description={`Permanently delete "${deleteTarget?.name}" and all competitions, teams, and matches? This cannot be undone.`} confirmLabel="Delete league" loading={loading} onConfirm={async () => { if (!deleteTarget) return; setLoading(true); try { await api.leagues.delete(deleteTarget.id); setLeagues((prev) => prev.filter((x) => x.id !== deleteTarget.id)); setDeleteTarget(null); router.refresh(); } catch (e) { setError(e instanceof Error ? e.message : "Delete failed"); } finally { setLoading(false); } }} />
); }