"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Trash2 } from "lucide-react"; import { toast } from "sonner"; interface BlockingRecording { id: number; name: string; } export interface SessionRow { id: number; name: string | null; kind: "live" | "replay"; status: "active" | "completed" | "aborted"; startedAt: number; durationMs: number | null; } function formatDuration(ms: number | null): string { if (ms === null) return "-"; const totalSeconds = Math.round(ms / 1000); const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; return `${minutes}:${seconds.toString().padStart(2, "0")}`; } export function SessionsTable({ sessions }: { sessions: SessionRow[] }) { const router = useRouter(); const [conflict, setConflict] = useState<{ id: number; recordings: BlockingRecording[] } | null>(null); const [deleting, setDeleting] = useState(false); async function handleDelete(id: number) { const res = await fetch(`/api/play-sessions/${id}`, { method: "DELETE" }); if (res.ok) { toast.success("Session deleted"); router.refresh(); } else if (res.status === 409) { const body: { recordings?: BlockingRecording[] } = await res.json().catch(() => ({})); setConflict({ id, recordings: body.recordings ?? [] }); } else { toast.error("Could not delete session"); } } async function handleCascadeDelete() { if (!conflict) return; setDeleting(true); const res = await fetch(`/api/play-sessions/${conflict.id}?cascade=true`, { method: "DELETE" }); setDeleting(false); if (res.ok) { toast.success("Session and recording deleted"); setConflict(null); router.refresh(); } else { toast.error("Could not delete session"); } } if (sessions.length === 0) { return
No sessions yet.
; } return ( <>