import { useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import type { Agent, ResponsibilityDashboardState, ResponsibilityDashboardVersionList, ResponsibilityDashboardVersionSummary, } from "@paperclipai/shared"; import { Loader2, X } from "lucide-react"; import { describeError } from "@/api/errors"; import { QueryErrorState, useQueryView } from "@/components/QueryView"; import { responsibilityDashboardApi } from "@/api/responsibility-dashboard"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { queryKeys } from "@/lib/queryKeys"; import { dashboardChangeLine, dashboardVersionWho, readDismissedUndo, writeDismissedUndo, } from "@/lib/responsibility-dashboard"; import { Link, useNavigate } from "@/lib/router"; import { formatDateTime } from "@/lib/utils"; import { DashboardChangesCard } from "./DashboardChangesCard"; /** * The Dashboard's edit module, in the Outline v2 view (experimental * `enableResponsibilities`): a person's way to have an agent draft their * dashboard, change it, and go back to an earlier version. * * - Empty dashboard: "Create your custom dashboard" with one button that * starts it with the named agent, and a quiet way to add an area by hand * (the only way when no agent can do it). * - Populated: one quiet row below the areas, "Edit your dashboard", with * "Versions" beside it. * - While a conversation is open: a line with the way into it. With changes * proposed: the changes, right here, to apply. * - Right after a change: what changed, with Undo. */ export interface DashboardEditModuleProps { companyId: string; ownerUserId: string; /** No active responsibility and none proposed. */ empty: boolean; agentsById: ReadonlyMap; onAddArea: () => void; } /** The module's read and actions: start (or reopen) the conversation, restore a version. */ export function useDashboardConversation(companyId: string, ownerUserId: string) { const queryClient = useQueryClient(); const navigate = useNavigate(); const key = queryKeys.responsibilities.dashboard(companyId, ownerUserId); const query = useQuery({ queryKey: key, queryFn: () => responsibilityDashboardApi.state(companyId, ownerUserId), }); const start = useMutation({ mutationFn: (mode: "create" | "edit") => responsibilityDashboardApi.start(companyId, { mode, ownerUserId }), onSuccess: (state) => { queryClient.setQueryData(key, state); if (state.conversation) navigate(`/issues/${state.conversation.identifier ?? state.conversation.issueId}`); }, }); const restore = useMutation({ mutationFn: (versionId: string) => responsibilityDashboardApi.restore(versionId), onSuccess: () => queryClient.invalidateQueries({ queryKey: queryKeys.responsibilities.all }), }); return { query, state: query.data ?? null, start, restore }; } /** "Added 2 areas and 5 responsibilities. Undo", until it is dismissed or another change replaces it. */ function LatestChange({ change, busy, onUndo, }: { change: ResponsibilityDashboardVersionSummary; busy: boolean; onUndo: () => void; }) { const [dismissed, setDismissed] = useState(() => readDismissedUndo()); if (dismissed === change.id) return null; return (

{dashboardChangeLine(change)}

); } /** The saved versions, newest first: when, who, what changed, and Restore. */ export function DashboardVersionList({ list, agentsById, restoringId, onRestore, }: { list: ResponsibilityDashboardVersionList; agentsById: ReadonlyMap; restoringId: string | null; onRestore: (versionId: string) => void; }) { if (list.versions.length === 0) return

No versions yet.

; const agentName = (id: string) => agentsById.get(id)?.name; return ( ); } function VersionsDialog({ companyId, ownerUserId, open, onOpenChange, agentsById, restore, }: { companyId: string; ownerUserId: string; open: boolean; onOpenChange: (open: boolean) => void; agentsById: ReadonlyMap; restore: ReturnType["restore"]; }) { const query = useQuery({ queryKey: queryKeys.responsibilities.dashboardVersions(companyId, ownerUserId), queryFn: () => responsibilityDashboardApi.versions(companyId, ownerUserId), enabled: open, // Opening the list saves changes made by hand first, so read it fresh each time. staleTime: 0, }); const versions = useQueryView(query); return ( Previous versions Saved versions of your dashboard, newest first. {versions.kind === "error" ? ( ) : versions.data ? ( restore.mutate(versionId)} /> ) : ( )} {restore.error ? (

Couldn’t restore it. {describeError(restore.error).body}

) : null}
); } /** What the module shows, from the state alone, so stories and tests can render each one. */ export function DashboardEditModuleView({ empty, state, agentsById, starting, startError, undoing, undoError, onStart, onUndo, onAddArea, onOpenVersions, }: { empty: boolean; state: ResponsibilityDashboardState | null; agentsById: ReadonlyMap; starting: boolean; startError: unknown; undoing: boolean; undoError?: unknown; onStart: () => void; onUndo: (versionId: string) => void; onAddArea: () => void; onOpenVersions: () => void; }) { const nameOf = (agentId: string | null | undefined) => (agentId ? agentsById.get(agentId)?.name : undefined); const proposal = state?.proposal ?? null; const talking = !proposal && state?.state === "discussing" && state.conversation ? state.conversation : null; const loaded = state !== null; const noAgent = loaded && !talking && !proposal && state.defaultAgentId === null; const agentName = nameOf(state?.defaultAgentId) ?? "an agent"; const talkingWith = nameOf(talking?.agentId) ?? "an agent"; const latest = state?.latestChange ?? null; const conversationLink = talking ? ( Open the conversation ) : null; const error = startError ? (

Couldn’t start it. {describeError(startError).body}

) : undoError ? (

Couldn’t undo it. {describeError(undoError).body}

) : null; const undo = latest?.previousVersionId ? ( onUndo(latest.previousVersionId!)} /> ) : null; const card = proposal ? ( ) : null; if (empty) { return (

Create your custom dashboard

Have an agent research and interview you to generate a custom dashboard of areas and responsibilities that matter to you.

{card} {talking ? (

Talking it through with {talkingWith}. {conversationLink}

) : null} {!card && !talking ? ( noAgent ? (

No agent is free to do this. Add your areas yourself.

) : (
) ) : null} {error} {undo}
); } return (
{undo} {card} {error}
{talking ? (

Editing it with {talkingWith}. {conversationLink}

) : noAgent ? (

No agent is free to edit your dashboard.

) : ( )}
); } export function DashboardEditModule({ companyId, ownerUserId, empty, agentsById, onAddArea }: DashboardEditModuleProps) { const { query, state, start, restore } = useDashboardConversation(companyId, ownerUserId); const stateView = useQueryView(query); const [versionsOpen, setVersionsOpen] = useState(false); if (stateView.kind === "error") { return (
); } return ( <> start.mutate(empty ? "create" : "edit")} onUndo={(versionId) => restore.mutate(versionId)} onAddArea={onAddArea} onOpenVersions={() => { restore.reset(); setVersionsOpen(true); }} /> ); }