"use client"; import { useEffect, useRef, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { differenceInCalendarDays, format, formatDistanceToNowStrict } from "date-fns"; import { AlertTriangle, Check, ChevronDown, ChevronRight, Clock, Flag, Link2, Pencil, Plus, Trash2, X, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { useToast } from "@/components/ui/use-toast"; import { cn, getInitials } from "@/lib/utils"; import { addBoardItems, addMilestone, deleteBoardItem, deleteMilestone, setBoardItemDone, setMilestoneDone, setProjectBoardStatus, updateBoardItem, } from "@/actions/board"; type User = { id: string; name: string | null; email: string; image: string | null }; type BoardItem = { id: string; text: string; code: string | null; group: string; waitingOn: string | null; doneAt: string | null; assignee: { id: string; name: string | null; image: string | null } | null; shot: { id: string; shotCode: string; status: string; latest: { id: string; versionNumber: number; approvalStatus: string; isClientVisible: boolean } | null; } | null; relinkTo: string | null; relinkIsExact: boolean; }; export type BoardProject = { id: string; name: string; code: string; status: string; boardStatus: string | null; boardStatusAt: string | null; shotTotal: number; shotComplete: number; milestones: { id: string; label: string; date: string; doneAt: string | null }[]; items: BoardItem[]; }; const SHOT_STATUS: Record = { WAITING: { label: "Waiting", color: "bg-zinc-500/10 text-zinc-400 border-zinc-500/20" }, IN_PROGRESS: { label: "In Progress", color: "bg-blue-500/10 text-blue-400 border-blue-500/20" }, INTERNAL_REVIEW: { label: "Internal Review", color: "bg-purple-500/10 text-purple-400 border-purple-500/20" }, READY_FOR_CLIENT: { label: "Ready for Client", color: "bg-cyan-500/10 text-cyan-400 border-cyan-500/20" }, CLIENT_REVIEW: { label: "Client Review", color: "bg-indigo-500/10 text-indigo-400 border-indigo-500/20" }, REVISIONS: { label: "Revisions", color: "bg-orange-500/10 text-orange-400 border-orange-500/20" }, COMPLETE: { label: "Complete", color: "bg-green-500/10 text-green-400 border-green-500/20" }, }; const APPROVAL: Record = { PENDING_REVIEW: { label: "pending", color: "text-zinc-500" }, APPROVED: { label: "approved", color: "text-green-400" }, REJECTED: { label: "rejected", color: "text-red-400" }, NEEDS_CHANGES: { label: "changes", color: "text-orange-400" }, }; export function BoardClient({ projects, users, who, }: { projects: BoardProject[]; users: User[]; who: string | null; }) { const router = useRouter(); const filters = [ { key: null, label: "Everyone" }, ...users.map((u) => ({ key: u.id, label: u.name?.split(" ")[0] ?? u.email })), { key: "none", label: "Unassigned" }, ]; return (

Board

{format(new Date(), "EEEE, d MMMM yyyy")}

{filters.map((f) => ( ))}
{projects.length === 0 && (

No active or on-hold projects.

)}
{projects.map((p) => ( ))}
); } function ProjectSection({ project, users, who }: { project: BoardProject; users: User[]; who: string | null }) { const [showDone, setShowDone] = useState(false); const visible = project.items.filter((i) => who === null ? true : who === "none" ? !i.assignee : i.assignee?.id === who, ); const open = visible.filter((i) => !i.doneAt); const done = visible.filter((i) => i.doneAt); const flagged = open.filter((i) => i.code && !i.shot); const waiting = open.filter((i) => i.waitingOn && !(i.code && !i.shot)); const rest = open.filter((i) => !flagged.includes(i) && !waiting.includes(i)); // Groups keep the order they were first typed in const groups: { name: string; items: BoardItem[] }[] = []; for (const item of rest) { let g = groups.find((x) => x.name === item.group); if (!g) groups.push((g = { name: item.group, items: [] })); g.items.push(item); } groups.sort((a, b) => (a.name === "" ? -1 : b.name === "" ? 1 : 0)); const pct = project.shotTotal ? Math.round((project.shotComplete / project.shotTotal) * 100) : 0; return (
{project.name} {project.code} {project.status === "ON_HOLD" && ( On hold )} {project.shotTotal > 0 && (
{project.shotComplete}/{project.shotTotal} shots complete
)}
{flagged.length > 0 && ( }> {flagged.map((i) => )} )} {waiting.length > 0 && ( }> {waiting.map((i) => )} )} {groups.map((g) => ( {g.items.map((i) => )} ))} {done.length > 0 && (
{showDone && (
{done.map((i) => )}
)}
)}
); } function ItemBlock({ title, icon, children }: { title: string; icon?: React.ReactNode; children: React.ReactNode }) { return (
{title && (
{icon} {title}
)}
{children}
); } function useAction() { const router = useRouter(); const { toast } = useToast(); const [isPending, startTransition] = useTransition(); const run = (fn: () => Promise, after?: () => void) => startTransition(async () => { try { await fn(); after?.(); router.refresh(); } catch (e) { toast({ title: "Couldn't save", description: (e as Error).message, variant: "destructive" }); } }); return { run, isPending }; } // ── Status line ────────────────────────────────────────────────────────────── function StatusLine({ project }: { project: BoardProject }) { const [editing, setEditing] = useState(false); const [value, setValue] = useState(project.boardStatus ?? ""); const { run, isPending } = useAction(); const save = () => run(() => setProjectBoardStatus({ projectId: project.id, status: value }), () => setEditing(false)); if (editing) { return (