import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { RESPONSIBILITY_STATUS_REPORT_LABEL, type Agent, type ResponsibilityHealth, type ResponsibilityRollup, type ResponsibilityStatusReportState, type ResponsibilityStatusReportStatus, } from "@paperclipai/shared"; import { Loader2, MessageSquare } from "lucide-react"; import { describeError } from "@/api/errors"; import { responsibilitiesApi } from "@/api/responsibilities"; import { Button } from "@/components/ui/button"; import { queryKeys } from "@/lib/queryKeys"; import { describeReadingAge } from "@/lib/responsibilities"; import { cn } from "@/lib/utils"; import { ResponsibilityHealthBadge, StaleFlag } from "./ResponsibilityHealthBadge"; const REPORT_HEALTH: Record = { behind: "behind", at_risk: "at_risk", on_track: "on_track", unknown: "not_measured", }; /** * The agent-written status of a responsibility (the status-report module's * read, shared with the Dashboard). A failed or missing read is simply no * status: the card stays as it was. */ export function useResponsibilityStatusReport(responsibilityId: string, enabled = true) { return useQuery({ queryKey: queryKeys.responsibilities.statusReport(responsibilityId), queryFn: () => responsibilitiesApi.statusReport(responsibilityId), enabled, staleTime: 30_000, }); } /** "Written by Ada · 3 days ago", "Written by you · today". */ export function describeStatusAuthor( current: NonNullable, agentsById: ReadonlyMap, now: Date, ownerUserId: string, ): string { const who = current.agentId ? agentsById.get(current.agentId)?.name ?? "an agent" : current.userId ? current.userId === ownerUserId ? "the owner" : "a teammate" : "an agent no longer here"; return `Written by ${who} · ${describeReadingAge(current.writtenAt, now)}`; } /** * The bar card's status: the agent's written status as the lead line when it * exists (status, its one line, who wrote it and when, marked when out of * date, and asking the agent for an update), with the status derived from * the number beneath as the fact it is. Without a written status, the * derived status alone, as before. */ export function ResponsibilityStatusBlock({ responsibility, agentsById, now, editable, className, }: { responsibility: { id: string; ownerUserId: string; status: string; rollup: Pick }; agentsById: ReadonlyMap; now: Date; /** Whether the person can ask for an update. */ editable: boolean; className?: string; }) { const queryClient = useQueryClient(); const report = useResponsibilityStatusReport(responsibility.id, responsibility.status === "active"); const ask = useMutation({ mutationFn: () => responsibilitiesApi.requestStatusUpdate(responsibility.id), onSuccess: (state) => queryClient.setQueryData(queryKeys.responsibilities.statusReport(responsibility.id), state), }); const { rollup } = responsibility; const derived = ( <>

{rollup.statusSentence}

); const current = report.data?.current ?? null; if (!current) return
{derived}
; const reporterId = report.data?.reporterAgentId ?? current.agentId; const reporterName = reporterId ? agentsById.get(reporterId)?.name ?? "the agent" : null; const requested = report.data?.updateRequested ?? false; return (

{current.message}

{describeStatusAuthor(current, agentsById, now, responsibility.ownerUserId)} {current.outOfDate ? : null}

{editable && reporterName ? (
{ask.error ? (

Couldn’t ask. {describeError(ask.error).body}

) : null}
) : null}
From the number {derived}
); }