import { QueryErrorState, useQueryView } from "@/components/QueryView"; import { useState, type ReactNode } from "react"; import { useInfiniteQuery, useQuery } from "@tanstack/react-query"; import type { Agent, DecisionHistoryScope } from "@paperclipai/shared"; import { attentionApi } from "@/api/attention"; import { projectsApi } from "@/api/projects"; import { useNavigate } from "@/lib/router"; import { queryKeys } from "@/lib/queryKeys"; import { DECISION_HISTORY_OUTCOME_OPTIONS, DECISION_HISTORY_SCOPE_OPTIONS, DEFAULT_DECISION_HISTORY_SCOPE } from "@/lib/coordinator-attention"; import { CoordinatorDecisionReceipt as Receipt } from "@/components/coordinator/CoordinatorDecisionReceipt"; import { ToolbarSearch } from "@/components/ToolbarSearch"; import { Button } from "@/components/ui/button"; import { NativeSelect } from "@/components/ui/select"; type Group = "none" | "task" | "agent" | "project"; /** "Handled for me" is a filter on who the card was for, not an outcome (PAP-749 plan §6). */ const HANDLED_FOR_ME = "handled_for_me"; /** * The Decision history panel of both Decisions pages (PAP-749 plan §3): every * closed card — answered, approved, withdrawn, expired or closed with its * task, approvals included. It opens on "Handled by me or for me"; Everyone * lists every person's (PAP-768). Search, filter by outcome, agent and * project within either, and open the task or approval. */ export function DecisionHistoryPanel({ companyId, agents, userLabelMap, header, }: { companyId: string; agents: Agent[]; userLabelMap?: ReadonlyMap; header?: ReactNode; }) { const navigate = useNavigate(); const [search, setSearch] = useState(""); const [outcome, setOutcome] = useState(""); const [agentId, setAgentId] = useState(""); const [projectId, setProjectId] = useState(""); const [groupBy, setGroupBy] = useState("none"); const [sort, setSort] = useState<"newest" | "oldest">("newest"); const [scope, setScope] = useState(DEFAULT_DECISION_HISTORY_SCOPE); const projects = useQuery({ queryKey: queryKeys.projects.list(companyId), queryFn: () => projectsApi.list(companyId) }); const history = useInfiniteQuery({ queryKey: [...queryKeys.attention(companyId), "history", { search, outcome, agentId, projectId, sort, scope }], queryFn: ({ pageParam }) => attentionApi.history(companyId, { search, agentId, projectId, sort, scope, cursor: pageParam, limit: 40, ...(outcome === HANDLED_FOR_ME ? { handledForMe: true } : { outcome }), }), initialPageParam: undefined as string | undefined, getNextPageParam: (page) => page.nextCursor ?? undefined, }); const historyView = useQueryView(history); const waiting = historyView.kind === "loading" || historyView.kind === "reconnecting"; const items = history.data?.pages.flatMap((page) => page.items) ?? []; const groups = new Map(); for (const item of items) { const label = groupBy === "task" ? item.issueIdentifier ?? item.issueTitle ?? "No task" : groupBy === "agent" ? item.agentName ?? "Other agents" : groupBy === "project" ? item.projectName ?? "No project" : ""; groups.set(label, [...(groups.get(label) ?? []), item]); } return
{header}
setScope(event.target.value as DecisionHistoryScope)} className="col-span-2"> {DECISION_HISTORY_SCOPE_OPTIONS.map((option) => )} setOutcome(event.target.value)}> {DECISION_HISTORY_OUTCOME_OPTIONS.map((option) => )} setSort(event.target.value as typeof sort)}> setAgentId(event.target.value)} className="col-span-2">{agents.map((agent) => )} setProjectId(event.target.value)} className="col-span-2">{(projects.data ?? []).map((project) => )} setGroupBy(event.target.value as Group)} className="col-span-2">
{waiting ?

Loading decisions…

: null} {historyView.kind === "error" ? : null} {!waiting && historyView.kind !== "error" && !items.length ?

{scope === "mine" && !search && !outcome && !agentId && !projectId ? "No decisions handled by you or for you yet." : "No decisions match these filters."}

{scope === "mine" ? : null}
: null}
{[...groups].map(([label, entries]) =>
{label ?

{label}

: null}{entries.map((item) =>

)}
)}
{history.hasNextPage ? : null}
; }