import { useCallback, useMemo, useState, type ReactNode } from "react"; import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; import { Loader2 } from "lucide-react"; import { decisionEffectTargetIssueIds, type Agent, type AttentionSubject, type DecisionEffect } from "@paperclipai/shared"; import { decisionsApi, type DecisionOutcome } from "../api/decisions"; import { issuesApi } from "../api/issues"; import { queryKeys } from "../lib/queryKeys"; import { cn } from "../lib/utils"; import { useCompany } from "../context/CompanyContext"; import { DecisionCard, type DecisionIssueRef } from "./DecisionCard"; import { DecisionCardFooter } from "./DecisionCardFooter"; import { MarkdownBody } from "./MarkdownBody"; import { NeedsYouPanel, NeedsYouPanelNote } from "./coordinator/NeedsYouCardKit"; import { Button } from "./ui/button"; import { Textarea } from "./ui/textarea"; import { ANALYTICS_MASK_CLASS } from "@/lib/analytics/privacy"; interface DecisionResolverProps { companyId: string; decisionId: string; /** Origin issue subject from the attention row (already carries identifier/href). */ originIssue?: AttentionSubject | null; agentMap?: Map; /** Complete terminal list item used to avoid one detail request per history row. */ initialDecision?: DecisionOutcome; /** Fresh, authorized terminal record owned by an enclosing detail query. */ recordedDecision?: DecisionOutcome; /** Called after a decide so the parent can refresh the feed row. */ onResolved?: () => void; /** A Needs You card's task link (PAP-775). Passing it renders the compact * card: the options in an inset panel, the task link and Submit below. */ leading?: ReactNode; } type IssueDetail = Awaited>; // A module-level combine function lets TanStack Query structurally share this // projection until issue data actually changes. Depending on useQueries' raw // result array would invalidate resolveIssue on every render. function combineIssueData(results: Array<{ data?: IssueDetail }>) { return results.map((result) => result.data); } export function signedCancelTreePreviewIds(targetIssueId: string, snapshot: { descendantIds?: string[] } | undefined) { return snapshot?.descendantIds ? [targetIssueId, ...snapshot.descendantIds] : null; } /** * Container for a Decisions-v1 decision surfaced in the attention feed. Fetches * the full decision (`get`) plus the shared open-list (for `targetChanged`), * resolves referenced issue ids + the cancel-tree preview, owns the decide * mutation, and invalidates the attention feed + target-issue keys on * success — same conventions as {@link AttentionInteractionResolver}. */ function issueLabel(ref: DecisionIssueRef | null, id: string) { return ref?.identifier ?? ref?.title ?? `issue ${id.slice(0, 8)}`; } /** What choosing an option does, in one line (mirrors the full card's preview). */ function effectLine(effect: DecisionEffect, resolve: (id: string) => DecisionIssueRef | null) { const target = issueLabel(resolve(effect.targetIssueId), effect.targetIssueId); switch (effect.type) { case "comment_on_issue": return `Comment on ${target}`; case "create_issue": return `Create “${effect.draft.title}”`; case "update_issue_status": return `Set ${target} to ${effect.status.replaceAll("_", " ")}`; case "assign_issue": return `Reassign ${target}`; case "resolve_blocker": return `Unblock ${target}`; case "cancel_issue_tree": return `Cancel ${target} and its sub-tree`; default: return "Apply effect"; } } /** * Paper "Make a choice" (PAP-775): the ask, its options as a radio list in the * inset panel, and one Submit on the right of the footer. A decision that can * cancel an issue tree keeps the full card and its typed confirmation. */ function CompactDecision({ decision, resolveIssue, targetChanged, busy, errorMessage, leading, onDecide }: { decision: DecisionOutcome; resolveIssue: (id: string) => DecisionIssueRef | null; targetChanged: Record | null; busy: boolean; errorMessage: string | null; leading: ReactNode; onDecide: (optionId: string, inputValues: Record) => void; }) { const [optionId, setOptionId] = useState(null); const [inputValues, setInputValues] = useState>({}); const stale = new Set(Object.entries(targetChanged ?? {}).filter(([, changed]) => changed).map(([id]) => id)); const requiredUnmet = (decision.inputs ?? []).some((field) => field.required && !(inputValues[field.id] ?? "").trim()); const blocked = (id: string | null) => decision.options.find((option) => option.id === id)?.effects .some((effect) => effect.staleness === "strict" && decisionEffectTargetIssueIds(effect).some((target) => stale.has(target))) ?? false; const body = decision.body?.trim(); return (

{decision.title}

{body ?
{body}
: null}
{(decision.inputs ?? []).map((field) => (