Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

TaskHud.tsx

245 lines8.6 KB

1import type { EnvironmentId, ScopedThreadRef } from "@v12/contracts";
2import { scopedThreadKey } from "@v12/client-runtime/environment";
3import type { TimestampFormat } from "@v12/contracts/settings";
4import { CheckIcon, ListTodoIcon, Trash2Icon, XIcon } from "lucide-react";
5import { memo, useCallback, useMemo } from "react";
6​
7import type { ActivePlanState, LatestProposedPlanState } from "../session-logic";
8import {
9 applyTaskHudOverrides,
10 taskHudPlanKey,
11 useTaskHudStore,
12 type ContextualTask,
13} from "../taskHudState";
14import PlanSidebar from "./PlanSidebar";
15import { Button } from "./ui/button";
16import { Toggle } from "./ui/toggle";
17import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
18​
19interface TaskHudProps {
20 readonly activePlan: ActivePlanState | null;
21 readonly activeProposedPlan: LatestProposedPlanState | null;
22 readonly label: string;
23 readonly environmentId: EnvironmentId;
24 readonly threadRef: ScopedThreadRef;
25 readonly markdownCwd: string | undefined;
26 readonly workspaceRoot: string | undefined;
27 readonly timestampFormat: TimestampFormat;
28 readonly mode?: "trigger" | "toolbar" | "panel";
29 readonly open: boolean;
30 readonly onOpenChange: (open: boolean) => void;
31 readonly onOpenTaskSource: (task: ContextualTask) => void;
33​
34const EMPTY_CONTEXT_TASKS: readonly ContextualTask[] = Object.freeze([]);
35​
36export const TaskHud = memo(function TaskHud({
37 activePlan,
38 activeProposedPlan,
39 label,
40 environmentId,
41 threadRef,
42 markdownCwd,
43 workspaceRoot,
44 timestampFormat,
45 mode = "trigger",
46 open,
47 onOpenChange,
48 onOpenTaskSource,
49}: TaskHudProps) {
50 const threadKey = scopedThreadKey(threadRef);
51 const contextTasks = useTaskHudStore(
52 (state) => state.contextTasksByThreadKey[threadKey] ?? EMPTY_CONTEXT_TASKS,
53 );
54 const planKey = activePlan ? taskHudPlanKey(threadRef, activePlan) : null;
55 const overrides = useTaskHudStore((state) =>
56 planKey === null ? undefined : state.overridesByPlanKey[planKey],
57 );
58 const steps = useMemo(
59 () => applyTaskHudOverrides(activePlan?.steps ?? [], overrides),
60 [activePlan?.steps, overrides],
61 );
62 const displayedPlan = useMemo<ActivePlanState | null>(
63 () => (activePlan ? { ...activePlan, steps } : null),
64 [activePlan, steps],
65 );
66 const completedCount =
67 steps.filter((step) => step.status === "completed").length +
68 contextTasks.filter((task) => task.completed).length;
69 const totalCount = steps.length + contextTasks.length;
70​
71 const handleToggleStep = useCallback(
72 (index: number) => {
73 const task = steps[index];
74 if (!planKey || !task) return;
75 useTaskHudStore.getState().setTaskCompleted(planKey, task.id, task.status !== "completed");
76 },
77 [planKey, steps],
78 );
79 const handleRemoveStep = useCallback(
80 (index: number) => {
81 const task = steps[index];
82 if (!planKey || !task) return;
83 useTaskHudStore.getState().removeTask(planKey, task.id);
84 },
85 [planKey, steps],
86 );
87 const handleReorderStep = useCallback(
88 (fromIndex: number, toIndex: number) => {
89 if (!planKey) return;
90 if (fromIndex < 0 || toIndex < 0 || fromIndex >= steps.length || toIndex >= steps.length) {
91 return;
92 }
93 const nextOrder = steps.map((step) => step.id);
94 const [moved] = nextOrder.splice(fromIndex, 1);
95 if (!moved) return;
96 nextOrder.splice(toIndex, 0, moved);
97 useTaskHudStore.getState().setTaskOrder(planKey, nextOrder);
98 },
99 [planKey, steps],
100 );
101​
102 if (!activePlan && !activeProposedPlan && contextTasks.length === 0) return null;
103​
104 if (mode === "toolbar") {
105 const accessibleLabel = `${label}: ${completedCount} of ${totalCount} tasks complete`;
106 return (
107 <Tooltip>
108 <TooltipTrigger
109 render={
110 <Toggle
111 className="shrink-0 [-webkit-app-region:no-drag]"
112 pressed={open}
113 onPressedChange={onOpenChange}
114 aria-label={accessibleLabel}
115 variant="ghost"
116 size="sm"
117 >
118 <ListTodoIcon className="size-3.5" />
119 </Toggle>
120 }
121 />
122 <TooltipPopup side="bottom">
123 {label} {completedCount}/{totalCount}
124 </TooltipPopup>
125 </Tooltip>
126 );
127 }
128​
129 if (mode === "trigger") {
130 return (
131 <Button
132 size="sm"
133 variant="outline"
134 aria-label={`${label}: ${completedCount} of ${totalCount} tasks complete`}
135 aria-expanded={open}
136 className="pointer-events-auto h-6 gap-1 rounded-full bg-background/90 px-2 text-[11px] shadow-sm backdrop-blur-sm"
137 onClick={() => onOpenChange(!open)}
138 >
139 <ListTodoIcon className="size-3" />
140 <span className="font-medium">{label}</span>
141 <span className="tabular-nums text-muted-foreground">
142 {completedCount}/{totalCount}
143 </span>
144 </Button>
145 );
146 }
147​
148 return (
149 <aside
150 aria-label={`${label} summary`}
151 className="max-h-[calc(100vh-8rem)] w-full select-none overflow-hidden rounded-2xl border border-border/70 bg-card/95 shadow-lg backdrop-blur-sm"
152 >
153 <header className="flex h-10 items-center justify-between px-3">
154 <div className="flex min-w-0 items-center gap-2">
155 <ListTodoIcon className="size-3.5 text-muted-foreground" />
156 <span className="text-xs font-medium">{label}</span>
157 <span className="text-[11px] tabular-nums text-muted-foreground">
158 {completedCount}/{totalCount}
159 </span>
160 </div>
161 <Button
162 size="icon-xs"
163 variant="ghost"
164 aria-label={`Close ${label}`}
165 onClick={() => onOpenChange(false)}
166 >
167 <XIcon className="size-3.5" />
168 </Button>
169 </header>
170 <div className="max-h-[calc(100vh-10.5rem)] overflow-y-auto">
171 {contextTasks.length > 0 ? (
172 <section
173 className={
174 activePlan || activeProposedPlan
175 ? "border-y border-border/60 p-2.5"
176 : "border-t border-border/60 p-2.5"
177 }
178 aria-label="Conversation tasks"
179 >
180 <p className="mb-1.5 text-xs font-medium text-muted-foreground">From conversation</p>
181 <div className="max-h-48 space-y-1 overflow-auto">
182 {contextTasks.map((task) => (
183 <div
184 key={task.id}
185 className="group flex items-start gap-2 rounded-md p-1.5 hover:bg-accent/30"
186 >
187 <Button
188 size="icon-xs"
189 variant="ghost"
190 aria-label={task.completed ? "Mark task incomplete" : "Mark task complete"}
191 onClick={() =>
192 useTaskHudStore
193 .getState()
194 .setContextTaskCompleted(threadKey, task.id, !task.completed)
195 }
196 >
197 <CheckIcon className={task.completed ? "size-3" : "size-3 opacity-20"} />
198 </Button>
199 <button
200 type="button"
201 className="min-w-0 flex-1 text-left"
202 onClick={() => onOpenTaskSource(task)}
203 >
204 <p className={task.completed ? "text-xs line-through opacity-60" : "text-xs"}>
205 {task.instruction}
206 </p>
207 {task.quote !== task.instruction ? (
208 <p className="truncate text-[11px] text-muted-foreground">“{task.quote}”</p>
209 ) : null}
210 </button>
211 <Button
212 size="icon-xs"
213 variant="ghost"
214 aria-label="Remove task"
215 onClick={() => useTaskHudStore.getState().removeContextTask(threadKey, task.id)}
216 >
217 <Trash2Icon className="size-3" />
218 </Button>
219 </div>
220 ))}
221 </div>
222 </section>
223 ) : null}
224 {activePlan || activeProposedPlan ? (
225 <div className="min-h-0">
226 <PlanSidebar
227 activePlan={displayedPlan}
228 activeProposedPlan={activeProposedPlan}
229 label={label}
230 environmentId={environmentId}
231 threadRef={threadRef}
232 markdownCwd={markdownCwd}
233 workspaceRoot={workspaceRoot}
234 timestampFormat={timestampFormat}
235 mode="summary"
236 onToggleStep={handleToggleStep}
237 onRemoveStep={handleRemoveStep}
238 onReorderStep={handleReorderStep}
239 />
240 </div>
241 ) : null}
242 </div>
243 </aside>
244 );
245});