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

WorktreeSetupCard.tsx

296 lines9.9 KB

1import {
2 worktreeSetupStageLabel,
3 type WorktreeSetupSnapshot,
4 type WorktreeSetupStage,
5} from "@t3tools/contracts";
6import { formatDuration } from "@t3tools/shared/orchestrationTiming";
7import {
8 CheckIcon,
9 ChevronDownIcon,
10 ChevronRightIcon,
11 CircleAlertIcon,
12 CircleIcon,
13 GitBranchIcon,
14 LaptopIcon,
15 MinusIcon,
16 TerminalIcon,
17 XIcon,
18} from "lucide-react";
19import { useEffect, useState } from "react";
20​
21import { Button } from "~/components/ui/button";
22import { Spinner } from "~/components/ui/spinner";
23import { cn } from "~/lib/utils";
24​
25interface WorktreeSetupCardProps {
26 snapshot: WorktreeSetupSnapshot;
27 /** Interrupts the server-side bootstrap. Hidden once the setup has settled. */
28 onCancel: (() => void) | null;
29 /** Restarts the same message in the project checkout instead of a worktree. */
30 onWorkLocally: (() => void) | null;
31 /** Reveals the setup script terminal tab. Null when no script ran. */
32 onOpenTerminal: (() => void) | null;
34​
35function stageElapsedMs(stage: WorktreeSetupStage, nowMs: number): number | null {
36 if (!stage.startedAt) return null;
37 const start = Date.parse(stage.startedAt);
38 const end = stage.endedAt ? Date.parse(stage.endedAt) : nowMs;
39 if (!Number.isFinite(start) || !Number.isFinite(end)) return null;
40 return Math.max(0, end - start);
42​
43/**
44 * Ticks once a second while any stage runs so elapsed labels stay live
45 * without pushing a React commit through the timeline for every second.
46 */
47function useNowWhile(active: boolean): number {
48 const [nowMs, setNowMs] = useState(() => Date.now());
49 useEffect(() => {
50 if (!active) return;
51 const id = setInterval(() => setNowMs(Date.now()), 1_000);
52 return () => clearInterval(id);
53 }, [active]);
54 return nowMs;
56​
57function StageIcon({ status }: { status: WorktreeSetupStage["status"] }) {
58 const className = "size-4 shrink-0 stroke-[1.8]";
59 switch (status) {
60 case "done":
61 return <CheckIcon aria-hidden className={className} />;
62 case "running":
63 return <Spinner className={className} />;
64 case "failed":
65 return <XIcon aria-hidden className={className} />;
66 case "warning":
67 return <CircleAlertIcon aria-hidden className={className} />;
68 case "skipped":
69 return <MinusIcon aria-hidden className={className} />;
70 case "pending":
71 return <CircleIcon aria-hidden className={className} />;
72 }
74​
75function stageRowClassName(status: WorktreeSetupStage["status"]): string {
76 switch (status) {
77 case "running":
78 return "text-foreground";
79 case "failed":
80 return "text-destructive-foreground";
81 case "warning":
82 return "text-warning-foreground";
83 case "pending":
84 case "skipped":
85 return "text-secondary-label opacity-50";
86 case "done":
87 return "text-secondary-label";
88 }
90​
91function StageRow({
92 stage,
93 nowMs,
94 scriptName,
95}: {
96 stage: WorktreeSetupStage;
97 nowMs: number;
98 scriptName: string | null;
99}) {
100 const elapsed = stageElapsedMs(stage, nowMs);
101 const label =
102 stage.id === "setup-script" && scriptName ? scriptName : worktreeSetupStageLabel(stage.id);
103 const showBar = stage.id === "checkout" && stage.status === "running" && stage.percent !== null;
104 const trailing =
105 stage.status === "pending"
106 ? null
107 : stage.status === "skipped"
108 ? (stage.detail ?? "skipped")
109 : stage.detail;
110​
111 return (
112 <div
113 className={cn(
114 "flex min-h-6 items-center gap-1.5 px-0.5 py-0.5 text-sm leading-relaxed",
115 stageRowClassName(stage.status),
116 )}
117 data-worktree-setup-stage={stage.id}
118 data-worktree-setup-status={stage.status}
119 >
120 <span className="flex size-6 shrink-0 items-center justify-center">
121 <StageIcon status={stage.status} />
122 </span>
123 <span className="min-w-0 flex-1 truncate">{label}</span>
124 <span className="flex min-w-0 items-center gap-2 text-xs text-muted-foreground tabular-nums">
125 {showBar ? (
126 <>
127 <span className="h-1 w-18 overflow-hidden rounded-full bg-input">
128 <span
129 className="block h-full rounded-full bg-primary"
130 style={{ width: `${stage.percent ?? 0}%` }}
131 />
132 </span>
133 <span className="text-foreground">{stage.percent}%</span>
134 </>
135 ) : null}
136 {!showBar && trailing ? <span className="truncate">{trailing}</span> : null}
137 {elapsed !== null && stage.status !== "skipped" && stage.status !== "pending" ? (
138 <span>{formatDuration(elapsed)}</span>
139 ) : null}
140 </span>
141 </div>
142 );
144​
145function OutputTail({ lines, failed }: { lines: ReadonlyArray<string>; failed: boolean }) {
146 if (lines.length === 0) return null;
147 return (
148 <pre
149 className={cn(
150 "mb-1 ml-8 rounded-md border px-2.5 py-1.5 font-mono text-[11px] leading-relaxed whitespace-pre-wrap break-all select-text",
151 failed
152 ? "border-destructive/20 bg-error-surface text-destructive-foreground"
153 : "border-border bg-code text-muted-foreground",
154 )}
155 >
156 {lines.join("\n")}
157 </pre>
158 );
160​
161function headerLabel(snapshot: WorktreeSetupSnapshot): string {
162 switch (snapshot.phase) {
163 case "running":
164 return "Creating worktree";
165 case "done":
166 return snapshot.stages.some((stage) => stage.status === "failed")
167 ? "Worktree ready, setup script failed"
168 : "Worktree ready";
169 case "failed":
170 return "Worktree setup failed";
171 case "cancelled":
172 return "Worktree setup cancelled";
173 }
175​
176export function WorktreeSetupCard({
177 snapshot,
178 onCancel,
179 onWorkLocally,
180 onOpenTerminal,
181}: WorktreeSetupCardProps) {
182 const running = snapshot.phase === "running";
183 const nowMs = useNowWhile(running);
184 const [detailsOpen, setDetailsOpen] = useState(false);
185 const totalElapsed = (() => {
186 const start = Date.parse(snapshot.startedAt);
187 const end = snapshot.endedAt ? Date.parse(snapshot.endedAt) : nowMs;
188 return Number.isFinite(start) && Number.isFinite(end) ? Math.max(0, end - start) : null;
189 })();
190 const setupStage = snapshot.stages.find((stage) => stage.id === "setup-script");
191 const failed = snapshot.phase === "failed";
192 const finishedWithFailedStage =
193 snapshot.phase === "done" && snapshot.stages.some((stage) => stage.status === "failed");
194 const headerClassName = failed
195 ? "text-destructive-foreground"
196 : finishedWithFailedStage
197 ? "text-warning-foreground"
198 : snapshot.phase === "cancelled"
199 ? "text-muted-foreground"
200 : "text-secondary-label";
201​
202 return (
203 <section
204 aria-label="Worktree setup"
205 className="mt-3 rounded-lg border border-border bg-card/60 px-2.5 pt-1.5 pb-2"
206 data-worktree-setup-phase={snapshot.phase}
207 >
208 <div className={cn("flex min-h-6 items-center gap-1.5 px-0.5 text-sm", headerClassName)}>
209 <span className="flex size-6 shrink-0 items-center justify-center text-icon-muted">
210 <GitBranchIcon aria-hidden className="size-4 shrink-0 stroke-[1.8]" />
211 </span>
212 <span className="min-w-0 flex-1 truncate">{headerLabel(snapshot)}</span>
213 {totalElapsed !== null ? (
214 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
215 {formatDuration(totalElapsed)}
216 </span>
217 ) : null}
218 </div>
219​
220 {snapshot.stages.map((stage) => (
221 <div key={stage.id}>
222 <StageRow stage={stage} nowMs={nowMs} scriptName={snapshot.setupScript?.name ?? null} />
223 {stage.id === "setup-script" &&
224 (stage.status === "running" || stage.status === "failed") ? (
225 <OutputTail lines={stage.tail} failed={stage.status === "failed"} />
226 ) : null}
227 </div>
228 ))}
229​
230 {failed && snapshot.error ? (
231 <p className="mt-1 px-0.5 text-xs text-muted-foreground">{snapshot.error}</p>
232 ) : null}
233​
234 {detailsOpen ? (
235 <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">
236 {snapshot.branch ? (
237 <>
238 <dt className="text-foreground/80">Branch</dt>
239 <dd className="truncate font-mono">{snapshot.branch}</dd>
240 </>
241 ) : null}
242 {snapshot.baseRef ? (
243 <>
244 <dt className="text-foreground/80">Base</dt>
245 <dd className="truncate font-mono">{snapshot.baseRef}</dd>
246 </>
247 ) : null}
248 {snapshot.worktreePath ? (
249 <>
250 <dt className="text-foreground/80">Path</dt>
251 <dd className="truncate font-mono">{snapshot.worktreePath}</dd>
252 </>
253 ) : null}
254 {snapshot.setupScript ? (
255 <>
256 <dt className="text-foreground/80">Setup</dt>
257 <dd className="truncate font-mono">{snapshot.setupScript.command}</dd>
258 </>
259 ) : null}
260 </dl>
261 ) : null}
262​
263 <div className="mt-1 flex flex-wrap items-center gap-1.5">
264 <Button
265 type="button"
266 size="xs"
267 variant="ghost-muted"
268 aria-expanded={detailsOpen}
269 onClick={() => setDetailsOpen((open) => !open)}
270 >
271 {detailsOpen ? <ChevronDownIcon aria-hidden /> : <ChevronRightIcon aria-hidden />}
272 Details
273 </Button>
274 <span className="flex-1" />
275 {onOpenTerminal && setupStage && setupStage.status !== "pending" ? (
276 <Button type="button" size="xs" variant="outline" onClick={onOpenTerminal}>
277 <TerminalIcon aria-hidden />
278 Open terminal
279 </Button>
280 ) : null}
281 {onWorkLocally ? (
282 <Button type="button" size="xs" variant="outline" onClick={onWorkLocally}>
283 <LaptopIcon aria-hidden />
284 Work locally
285 </Button>
286 ) : null}
287 {onCancel && running ? (
288 <Button type="button" size="xs" variant="outline" onClick={onCancel}>
289 <XIcon aria-hidden />
290 Cancel
291 </Button>
292 ) : null}
293 </div>
294 </section>
295 );