@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 408 lines 13.3 KB
Raw
1 import { T, msg, useGT, useMessages } from "gt-react";
2 import { type MessageResolver } from "~/i18n/messages";
3 import { type WorktreeSetupSnapshot, type WorktreeSetupStage } from "@t3tools/contracts";
4 import { worktreeSetupStageLabel } from "@t3tools/client-runtime/contract-labels";
5 import { formatDuration } from "@t3tools/shared/orchestrationTimingCopy";
6 import {
7 CheckIcon,
8 CircleAlertIcon,
9 CircleIcon,
10 LaptopIcon,
11 MinusIcon,
12 TerminalIcon,
13 XIcon,
14 ChevronDownIcon,
15 ChevronRightIcon,
16 } from "lucide-react";
17 import { useState, type ReactNode } from "react";
18
19 import { Button } from "~/components/ui/button";
20 import { Spinner } from "~/components/ui/spinner";
21 import { MiddleTruncate } from "../ui/middle-truncate";
22 import { observeVisibleAnimation } from "~/lib/visibleAnimation";
23 import { cn } from "~/lib/utils";
24 import { useNowSecond } from "~/hooks/useNowSecond";
25 import { WorkLogRow } from "./WorkLog";
26
27 interface WorktreeSetupCardProps {
28 snapshot: WorktreeSetupSnapshot;
29 onCancel: (() => void) | null;
30 onWorkLocally: (() => void) | null;
31 onOpenTerminal: (() => void) | null;
32 }
33
34 function elapsedMs(startedAt: string | null, endMs: number): number | null {
35 if (!startedAt) return null;
36 const start = Date.parse(startedAt);
37 if (!Number.isFinite(start) || !Number.isFinite(endMs)) return null;
38 return Math.max(0, endMs - start);
39 }
40
41 /** Mounted only while setup runs, so finished setups never subscribe to the tick. */
42 function LiveElapsed({ startedAt }: { startedAt: string }) {
43 const m = useMessages();
44 const elapsed = elapsedMs(startedAt, useNowSecond());
45 return elapsed === null ? null : <>{formatDuration(elapsed, m)}</>;
46 }
47
48 /** A finished duration, or a ticking one while setup is still running. */
49 function Elapsed({
50 startedAt,
51 endedAt,
52 live,
53 }: {
54 startedAt: string | null;
55 endedAt: string | null;
56 live: boolean;
57 }) {
58 const m = useMessages();
59 if (!startedAt) return null;
60 if (endedAt) {
61 const elapsed = elapsedMs(startedAt, Date.parse(endedAt));
62 return elapsed === null ? null : <>{formatDuration(elapsed, m)}</>;
63 }
64 return live ? <LiveElapsed startedAt={startedAt} /> : null;
65 }
66
67 function StageIcon({ status }: { status: WorktreeSetupStage["status"] }) {
68 const className = "size-4 shrink-0 stroke-2";
69 switch (status) {
70 case "done":
71 return <CheckIcon aria-hidden className={className} />;
72 case "running":
73 return <Spinner size="md" className="shrink-0" />;
74 case "failed":
75 return <XIcon aria-hidden className={className} />;
76 case "warning":
77 return <CircleAlertIcon aria-hidden className={className} />;
78 case "skipped":
79 return <MinusIcon aria-hidden className={className} />;
80 case "pending":
81 return <CircleIcon aria-hidden className={className} />;
82 }
83 }
84
85 function stageRowClassName(status: WorktreeSetupStage["status"]): string {
86 switch (status) {
87 case "failed":
88 return "text-destructive-foreground";
89 case "warning":
90 return "text-warning-foreground";
91 case "pending":
92 return "text-secondary-label opacity-40";
93 case "running":
94 case "skipped":
95 case "done":
96 return "text-secondary-label";
97 }
98 }
99
100 function ShimmerOverlay({ children }: { children: ReactNode }) {
101 return (
102 <span
103 aria-hidden
104 className="live-activity-focus pointer-events-none absolute inset-y-0 select-none"
105 >
106 <span className="live-activity-focus-counter block">
107 <span className="live-activity-focus-aligned block text-foreground">{children}</span>
108 </span>
109 </span>
110 );
111 }
112
113 function headerLabel(snapshot: WorktreeSetupSnapshot, resolve: MessageResolver): string {
114 switch (snapshot.phase) {
115 case "running":
116 return resolve(msg("Setting up worktree…"));
117 case "done":
118 return snapshot.stages.some((stage) => stage.status === "failed")
119 ? resolve(msg("Worktree ready, setup script failed"))
120 : resolve(msg("Worktree ready"));
121 case "failed":
122 return resolve(msg("Worktree setup failed"));
123 case "cancelled":
124 return resolve(msg("Worktree setup cancelled"));
125 }
126 }
127
128 function SetupHeaderRow({ snapshot }: { snapshot: WorktreeSetupSnapshot }) {
129 const m = useMessages();
130 const running = snapshot.phase === "running";
131 const failed = snapshot.phase === "failed";
132 const finishedWithFailedStage =
133 snapshot.phase === "done" && snapshot.stages.some((stage) => stage.status === "failed");
134 const text = headerLabel(snapshot, m);
135 const tone = failed
136 ? "text-destructive-foreground"
137 : finishedWithFailedStage
138 ? "text-warning-foreground"
139 : "text-muted-foreground";
140 return (
141 <div className="border-b border-border/60 pb-2 pt-1">
142 <div
143 className={cn(
144 "flex h-6 min-w-0 items-baseline gap-2 px-1 text-sm leading-relaxed tabular-nums",
145 tone,
146 )}
147 >
148 <span
149 ref={running ? observeVisibleAnimation : undefined}
150 className="relative min-w-0 shrink overflow-hidden whitespace-nowrap"
151 >
152 <span className="block truncate">{text}</span>
153 {running ? <ShimmerOverlay>{text}</ShimmerOverlay> : null}
154 </span>
155 <span className="ml-auto shrink-0 text-xs text-muted-foreground empty:hidden">
156 <Elapsed startedAt={snapshot.startedAt} endedAt={snapshot.endedAt} live={running} />
157 </span>
158 </div>
159 </div>
160 );
161 }
162
163 function StageRow({
164 stage,
165 setupRunning,
166 setupEndedAt,
167 scriptName,
168 }: {
169 stage: WorktreeSetupStage;
170 setupRunning: boolean;
171 setupEndedAt: string | null;
172 scriptName: string | null;
173 }) {
174 const gt = useGT();
175 const m = useMessages();
176 const label =
177 stage.id === "setup-script" && scriptName ? scriptName : worktreeSetupStageLabel(stage.id, m);
178 const running = stage.status === "running";
179 const trailing =
180 stage.status === "pending"
181 ? null
182 : stage.status === "skipped"
183 ? (stage.detail ?? gt("skipped", { $context: "worktree setup stage status" }))
184 : stage.id === "checkout" && running && stage.percent !== null
185 ? `${stage.percent}%`
186 : stage.detail;
187 return (
188 <WorkLogRow
189 data-worktree-setup-stage={stage.id}
190 data-worktree-setup-status={stage.status}
191 icon={
192 <span className={cn("text-icon-muted", stage.status === "pending" && "opacity-40")}>
193 <StageIcon status={stage.status} />
194 </span>
195 }
196 label={
197 <span
198 ref={running ? observeVisibleAnimation : undefined}
199 className={cn("relative block truncate", stageRowClassName(stage.status))}
200 >
201 {label}
202 {running ? <ShimmerOverlay>{label}</ShimmerOverlay> : null}
203 </span>
204 }
205 trailing={
206 <>
207 {trailing ? (
208 <span className="max-w-1/2 min-w-0 truncate text-xs text-muted-foreground tabular-nums">
209 {trailing}
210 </span>
211 ) : null}
212 {stage.status !== "skipped" && stage.status !== "pending" ? (
213 <span className="shrink-0 text-xs text-muted-foreground tabular-nums empty:hidden">
214 <Elapsed
215 startedAt={stage.startedAt}
216 // A stage interrupted by the setup ending stops with it.
217 endedAt={stage.endedAt ?? setupEndedAt}
218 live={setupRunning}
219 />
220 </span>
221 ) : null}
222 </>
223 }
224 />
225 );
226 }
227
228 const OUTPUT_TAIL_LINES = 4;
229 const OUTPUT_TAIL_SLOTS = Array.from({ length: OUTPUT_TAIL_LINES }, (_, slot) => slot);
230
231 function OutputTail({ lines, failed }: { lines: ReadonlyArray<string>; failed: boolean }) {
232 const rows = OUTPUT_TAIL_SLOTS.map((slot) => ({
233 slot,
234 line: lines[lines.length - OUTPUT_TAIL_LINES + slot] ?? "",
235 }));
236 return (
237 <pre
238 className={cn(
239 "mb-1 ml-8 overflow-hidden rounded-md border px-2.5 py-1.5 font-mono text-2xs leading-relaxed select-text",
240 failed
241 ? "border-destructive/20 bg-error-surface text-destructive-foreground"
242 : "border-border bg-code text-muted-foreground",
243 )}
244 >
245 {rows.map(({ slot, line }) => (
246 <div key={slot} className="truncate whitespace-pre">
247 {line.length === 0 ? "\u00a0" : line}
248 </div>
249 ))}
250 </pre>
251 );
252 }
253
254 function SetupDetails({ snapshot }: { snapshot: WorktreeSetupSnapshot }) {
255 return (
256 <dl className="mt-1 mb-1.5 ml-8 grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 text-xs text-muted-foreground">
257 {snapshot.branch ? (
258 <>
259 <dt className="text-foreground/80">
260 <T>Branch</T>
261 </dt>
262 <dd className="min-w-0 font-mono">
263 <MiddleTruncate value={snapshot.branch} className="flex" />
264 </dd>
265 </>
266 ) : null}
267 {snapshot.baseRef ? (
268 <>
269 <dt className="text-foreground/80">
270 <T context="git base branch">Base</T>
271 </dt>
272 <dd className="min-w-0 font-mono">
273 <MiddleTruncate value={snapshot.baseRef} className="flex" />
274 </dd>
275 </>
276 ) : null}
277 {snapshot.worktreePath ? (
278 <>
279 <dt className="text-foreground/80">
280 <T>Path</T>
281 </dt>
282 <dd className="min-w-0 font-mono">
283 <MiddleTruncate value={snapshot.worktreePath} className="flex" />
284 </dd>
285 </>
286 ) : null}
287 {snapshot.setupScript ? (
288 <>
289 <dt className="text-foreground/80">
290 <T context="setup script">Setup</T>
291 </dt>
292 <dd className="truncate font-mono">{snapshot.setupScript.command}</dd>
293 </>
294 ) : null}
295 </dl>
296 );
297 }
298
299 function CollapsedSummaryRow({ snapshot }: { snapshot: WorktreeSetupSnapshot }) {
300 const m = useMessages();
301 const status: WorktreeSetupStage["status"] =
302 snapshot.phase === "failed" || snapshot.phase === "cancelled"
303 ? "failed"
304 : snapshot.stages.some((stage) => stage.id === "setup-script" && stage.status === "failed")
305 ? "failed"
306 : "done";
307 const label = headerLabel(snapshot, m);
308 return (
309 <WorkLogRow
310 data-worktree-setup-stage="summary"
311 data-worktree-setup-status={status}
312 icon={
313 <span className="text-icon-muted">
314 <StageIcon status={status} />
315 </span>
316 }
317 label={<span className={stageRowClassName(status)}>{label}</span>}
318 trailing={
319 <span className="shrink-0 text-xs text-muted-foreground tabular-nums empty:hidden">
320 <Elapsed startedAt={snapshot.startedAt} endedAt={snapshot.endedAt} live={false} />
321 </span>
322 }
323 />
324 );
325 }
326
327 export function WorktreeSetupCard({
328 snapshot,
329 onCancel,
330 onWorkLocally,
331 onOpenTerminal,
332 embedded = false,
333 }: WorktreeSetupCardProps & {
334 embedded?: boolean;
335 }) {
336 const gt = useGT();
337 const running = snapshot.phase === "running";
338 const [detailsOpen, setDetailsOpen] = useState(false);
339 const setupStage = snapshot.stages.find((stage) => stage.id === "setup-script");
340 const showTerminal = onOpenTerminal && setupStage && setupStage.status !== "pending";
341 const collapsed = embedded && !running;
342 const showHeader = !embedded && !running && snapshot.phase !== "done";
343 const showTail =
344 setupStage !== undefined &&
345 (setupStage.status === "failed" ||
346 (setupStage.status === "running" && setupStage.tail.length > 0));
347
348 return (
349 <section aria-label={gt("Worktree setup")} data-worktree-setup-phase={snapshot.phase}>
350 {showHeader ? <SetupHeaderRow snapshot={snapshot} /> : null}
351 {collapsed ? (
352 <CollapsedSummaryRow snapshot={snapshot} />
353 ) : (
354 <div className={showHeader ? "pt-1.5" : undefined}>
355 {snapshot.stages.map((stage) => (
356 <div key={stage.id}>
357 <StageRow
358 stage={stage}
359 setupRunning={running}
360 setupEndedAt={snapshot.endedAt}
361 scriptName={snapshot.setupScript?.name ?? null}
362 />
363 {stage.id === "setup-script" && showTail ? (
364 <OutputTail lines={stage.tail} failed={stage.status === "failed"} />
365 ) : null}
366 </div>
367 ))}
368 </div>
369 )}
370
371 {snapshot.phase === "failed" && snapshot.error ? (
372 <p className="mt-1 ml-8 text-xs text-muted-foreground">{snapshot.error}</p>
373 ) : null}
374
375 <div className="mt-0.5 ml-[calc(--spacing(6)+2px-(--spacing(2)-1px))] flex flex-wrap items-center gap-0.5">
376 <Button
377 type="button"
378 size="xs"
379 variant="ghost-muted"
380 aria-expanded={detailsOpen}
381 onClick={() => setDetailsOpen((open) => !open)}
382 >
383 {detailsOpen ? <ChevronDownIcon aria-hidden /> : <ChevronRightIcon aria-hidden />}
384 <T>Details</T>
385 </Button>
386 {showTerminal ? (
387 <Button type="button" size="xs" variant="ghost-muted" onClick={onOpenTerminal}>
388 <TerminalIcon aria-hidden />
389 <T>Open terminal</T>
390 </Button>
391 ) : null}
392 {onWorkLocally ? (
393 <Button type="button" size="xs" variant="ghost-muted" onClick={onWorkLocally}>
394 <LaptopIcon aria-hidden />
395 <T>Work locally</T>
396 </Button>
397 ) : null}
398 {onCancel && running ? (
399 <Button type="button" size="xs" variant="ghost-muted" onClick={onCancel}>
400 <XIcon aria-hidden />
401 <T>Cancel</T>
402 </Button>
403 ) : null}
404 </div>
405 {detailsOpen ? <SetupDetails snapshot={snapshot} /> : null}
406 </section>
407 );
408 }