"use client"; import { useState, useTransition } from "react"; import Link from "next/link"; import * as Popover from "@radix-ui/react-popover"; import { formatDistanceToNow } from "date-fns"; import { AlertCircle, Check, Loader2, MessageSquareReply } from "lucide-react"; import { listUnreadShotComments, markShotCommentsRead } from "@/actions/comment-reads"; import type { UnreadCommentItem } from "@/lib/comment-reads"; import { useToast } from "@/components/ui/use-toast"; /** * The "!" beside a shot code while it has comments this user has not marked * read. Opening it lists them; they stay flagged until "Mark read" is clicked — * opening the list or the shot is not enough. */ export function NewCommentsMarker({ shotId, shotCode, count, latestAt, onRead, }: { shotId: string; shotCode: string; count: number; latestAt: string; onRead: (shotId: string) => void; }) { const { toast } = useToast(); const [open, setOpen] = useState(false); const [items, setItems] = useState(null); const [loadError, setLoadError] = useState(false); const [marking, startMarking] = useTransition(); const load = async () => { setLoadError(false); try { setItems(await listUnreadShotComments(shotId)); } catch { setLoadError(true); } }; const markRead = () => startMarking(async () => { // Read up to what was listed; with the list not loaded, up to what the // board showed. Anything newer stays flagged. const upTo = items?.[0]?.createdAt ?? latestAt; try { await markShotCommentsRead(shotId, upTo); setOpen(false); onRead(shotId); } catch { toast({ title: "Could not mark comments read", variant: "destructive" }); } }); return ( { setOpen(next); if (next) load(); }} >
New on {shotCode}
{loadError ? (

Could not load the comments.

) : items === null ? (

Loading…

) : items.length === 0 ? (

Nothing new — the comments may have been deleted.

) : (
    {items.map((item) => (
  • {item.kind === "reply" && } {item.authorName} {item.source === "WHATSAPP" && ( client )} v{String(item.versionNumber).padStart(3, "0")} · f{item.frameNumber} {formatDistanceToNow(new Date(item.createdAt), { addSuffix: true })}

    {item.text}

  • ))}
)}
); }