import { useMemo, useState } from "react" import { CaretDown, CaretUp } from "../../ui/icons" import type { AgentRecord, ApprovalRecord, DocumentRecord } from "@shared/types" import { Card, Eyebrow } from "../../ui" import { useT } from "../../../i18n" import { useApprovalPayload } from "./useApprovalPayload" interface DocRef { id: string type: string title: string } function toDocRefs(value: unknown): DocRef[] { if (!Array.isArray(value)) return [] return value.filter( (entry): entry is DocRef => !!entry && typeof entry === "object" && typeof (entry as DocRef).id === "string" && typeof (entry as DocRef).type === "string" && typeof (entry as DocRef).title === "string", ) } function toDocIds(value: unknown, fallback: DocRef[]): string[] { if (Array.isArray(value)) { return value.filter((entry): entry is string => typeof entry === "string") } return fallback.map((d) => d.id) } export function DeliverableReviewPanel({ approval, documents, agents, }: { approval: ApprovalRecord documents?: DocumentRecord[] agents: AgentRecord[] }) { const t = useT() const payload = useApprovalPayload(approval) const docRefs = useMemo(() => toDocRefs(payload?.documents), [payload]) const docIds = useMemo(() => toDocIds(payload?.documentIds, docRefs), [payload, docRefs]) const fullDocs = useMemo(() => { if (!documents) return [] as DocumentRecord[] return docIds .map((id) => documents.find((d) => d.id === id)) .filter((entry): entry is DocumentRecord => entry != null) }, [documents, docIds]) const [expandedDocId, setExpandedDocId] = useState( fullDocs.length === 1 ? fullDocs[0]!.id : null, ) return (
{t("approval.deliverableReview")} {fullDocs.length > 0 ? ( fullDocs.map((doc) => { const isExpanded = expandedDocId === doc.id const author = doc.authorAgentId ? agents.find((a) => a.id === doc.authorAgentId) : null return ( {isExpanded ? (
{author ? (
{t("approval.hireAuthor")} {author.name} ({author.role})
) : null}
{doc.content || t("approval.docEmpty")}
) : null}
) }) ) : docRefs.length > 0 ? ( docRefs.map((doc) => ( {doc.type.replaceAll("_", " ")} {doc.title} )) ) : (
{approval.payloadSummary}
)} {approval.impactSummary ? (
{approval.impactSummary}
) : null}
) }