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

worktree-setup-card.tsx

391 lines12.7 KB

1import {
2 worktreeSetupStageLabel,
3 type WorktreeSetupSnapshot,
4 type WorktreeSetupStage,
5} from "@t3tools/contracts";
6import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
7import { formatDuration } from "@t3tools/shared/orchestrationTiming";
8import { useEffect, useState } from "react";
9import { ActivityIndicator, AppState, Pressable, ScrollView, View } from "react-native";
10import { useSafeAreaInsets } from "react-native-safe-area-context";
11import { AppText as Text } from "../../components/AppText";
12import { SymbolView, type AppSymbolName } from "../../components/AppSymbol";
13import { WorktreeSetupSheet } from "./worktree-setup-sheet";
14import { ShimmeringWorkContent } from "./thread-work-log";
15​
16export interface WorktreeSetupCardProps {
17 snapshot: WorktreeSetupSnapshot;
18 turnStarted: boolean;
19 turnStartedAt: string | null;
20 working: boolean;
21 onCancel: () => void;
22 onWorkLocally: (() => void) | null;
24​
25function elapsed(start: string | null, end: string | null, now: number) {
26 const duration = (end ? Date.parse(end) : now) - (start ? Date.parse(start) : NaN);
27 return Number.isFinite(duration) ? formatDuration(Math.max(0, duration)) : null;
29​
30const icons: Record<WorktreeSetupStage["status"], AppSymbolName> = {
31 pending: "circle",
32 running: "clock",
33 done: "checkmark",
34 skipped: "minus",
35 failed: "xmark",
36 warning: "exclamationmark.triangle",
37};
38​
39/** Setup stages collapse into the working header once the agent's turn is live. */
40export function WorktreeSetupCard(props: WorktreeSetupCardProps) {
41 const { snapshot, turnStarted, turnStartedAt, working } = props;
42 const handedOff = turnStarted && worktreeSetupAgentStarted(snapshot);
43 const running = snapshot.phase === "running";
44 const backgroundSetup = handedOff && running;
45 const scriptName = snapshot.setupScript?.name ?? "Setup script";
46 const [detailsOpen, setDetailsOpen] = useState(false);
47 const now = useSetupClock(running || working);
48 const failed =
49 snapshot.phase === "failed" || snapshot.stages.some((stage) => stage.status === "failed");
50 const label =
51 handedOff && working
52 ? `Working for ${elapsed(turnStartedAt, null, now) ?? "0s"}`
53 : running
54 ? handedOff
55 ? "Setup continues…"
56 : "Setting up worktree…"
57 : snapshot.phase === "cancelled"
58 ? "Worktree setup cancelled"
59 : snapshot.phase === "failed"
60 ? "Worktree setup failed"
61 : failed
62 ? "Setup script failed"
63 : "Worktree ready";
64​
65 return (
66 <View accessibilityLabel="Worktree setup" className="py-1">
67 <View className="min-h-11 flex-row items-center gap-2 border-b border-border px-1">
68 <HeaderLabel
69 label={label}
70 active={(running || working) && !detailsOpen}
71 failed={failed && !working}
72 />
73 {!handedOff ? (
74 <Text
75 className="text-2xs text-adaptive-neutral-600-400"
76 style={{ fontVariant: ["tabular-nums"] }}
77 >
78 {elapsed(snapshot.startedAt, snapshot.endedAt, now)}
79 </Text>
80 ) : null}
81 <Pressable
82 accessibilityRole="button"
83 accessibilityLabel={
84 backgroundSetup
85 ? `${scriptName} is still running. Show setup progress.`
86 : "Worktree setup details"
87 }
88 accessibilityState={{ expanded: detailsOpen }}
89 onPress={() => setDetailsOpen(true)}
90 className="min-h-11 max-w-[55%] justify-center px-2"
91 >
92 <View
93 className={
94 backgroundSetup
95 ? "min-w-0 flex-row items-center gap-1 rounded-full border border-border px-2 py-1"
96 : "flex-row items-center gap-1"
97 }
98 >
99 {backgroundSetup ? (
100 <ActivityIndicator
101 size="small"
102 colorClassName="accent-adaptive-neutral-500-400"
103 style={{ width: 12, height: 12, transform: [{ scale: 0.65 }] }}
104 />
105 ) : failed ? (
106 <SymbolView
107 name="exclamationmark.circle"
108 size={12}
109 tintColorClassName="accent-adaptive-rose-700-300"
110 />
111 ) : null}
112 <Text numberOfLines={1} className="shrink text-2xs text-adaptive-neutral-600-400">
113 {backgroundSetup ? scriptName : "Details"}
114 </Text>
115 {!backgroundSetup ? (
116 <SymbolView
117 name="chevron.right"
118 size={10}
119 tintColorClassName="accent-adaptive-neutral-500-400"
120 />
121 ) : null}
122 </View>
123 </Pressable>
124 </View>
125 {!handedOff ? (
126 <View className="pt-1 pb-2">
127 {snapshot.stages
128 .filter((stage) => stage.id !== "agent")
129 .map((stage) => (
130 <StageRow
131 key={stage.id}
132 stage={stage}
133 scriptName={snapshot.setupScript?.name ?? null}
134 now={now}
135 compact
136 animate={!detailsOpen}
137 />
138 ))}
139 </View>
140 ) : null}
141 {detailsOpen ? (
142 <SetupDetailsSheet {...props} now={now} onClose={() => setDetailsOpen(false)} />
143 ) : null}
144 </View>
145 );
147​
148export function WorktreeWorkingHeader({ startedAt }: { startedAt: string }) {
149 const now = useSetupClock(true);
150 return (
151 <View className="py-1">
152 <View className="min-h-11 flex-row items-center border-b border-border px-1">
153 <HeaderLabel label={`Working for ${elapsed(startedAt, null, now) ?? "0s"}`} active />
154 </View>
155 </View>
156 );
158​
159function HeaderLabel({
160 label,
161 active,
162 failed = false,
163}: {
164 label: string;
165 active: boolean;
166 failed?: boolean;
167}) {
168 return active ? (
169 <ShimmeringWorkContent icon="clock" iconSubtleColor="#737373" label={label} showIcon={false} />
170 ) : (
171 <Text
172 numberOfLines={1}
173 className={
174 failed
175 ? "flex-1 text-sm text-adaptive-rose-700-300"
176 : "flex-1 text-sm text-adaptive-neutral-600-400"
177 }
178 style={{ fontVariant: ["tabular-nums"] }}
179 >
180 {label}
181 </Text>
182 );
184​
185function useSetupClock(active: boolean) {
186 const [now, setNow] = useState(Date.now);
187 const [appState, setAppState] = useState(AppState.currentState);
188 useEffect(() => {
189 const subscription = AppState.addEventListener("change", setAppState);
190 return () => subscription.remove();
191 }, []);
192 useEffect(() => {
193 if (!active || appState !== "active") return;
194 const timer = setInterval(() => setNow(Date.now()), 1000);
195 return () => clearInterval(timer);
196 }, [active, appState]);
197 return now;
199​
200function SetupDetailsSheet({
201 snapshot,
202 turnStarted,
203 onCancel,
204 onWorkLocally,
205 onClose,
206 now,
207}: WorktreeSetupCardProps & { onClose: () => void; now: number }) {
208 const insets = useSafeAreaInsets();
209 const [bodyHeight, setBodyHeight] = useState(0);
210 const canCancel = snapshot.phase === "running" && !turnStarted;
211 return (
212 <WorktreeSetupSheet height={bodyHeight} onClose={onClose}>
213 <ScrollView
214 bounces={false}
215 onContentSizeChange={(_width, height) => setBodyHeight(height)}
216 contentContainerStyle={{
217 paddingHorizontal: 20,
218 paddingTop: 12,
219 paddingBottom: Math.max(20, insets.bottom),
220 }}
221 >
222 {/* Agent startup is the header handoff, not a fifth setup step. */}
223 {snapshot.stages
224 .filter((stage) => stage.id !== "agent")
225 .map((stage) => (
226 <View key={stage.id}>
227 <StageRow stage={stage} scriptName={snapshot.setupScript?.name ?? null} now={now} />
228 {stage.id === "setup-script" &&
229 (stage.status === "running" || stage.status === "failed" || stage.tail.length > 0) ? (
230 <OutputTail lines={stage.tail} failed={stage.status === "failed"} />
231 ) : null}
232 </View>
233 ))}
234 {snapshot.phase === "failed" && snapshot.error ? (
235 <Text selectable className="ml-8 mt-2 text-xs text-adaptive-rose-700-300">
236 {snapshot.error}
237 </Text>
238 ) : null}
239 {canCancel ? (
240 <View className="mt-3 flex-row items-center justify-end gap-4 border-t border-border pt-1">
241 <Pressable
242 accessibilityRole="button"
243 accessibilityLabel="Cancel worktree setup"
244 onPress={() => {
245 onClose();
246 onCancel();
247 }}
248 className="min-h-11 justify-center px-2"
249 >
250 <Text className="text-sm text-danger-foreground">Cancel setup</Text>
251 </Pressable>
252 {onWorkLocally ? (
253 <Pressable
254 accessibilityRole="button"
255 onPress={() => {
256 onClose();
257 onWorkLocally();
258 }}
259 className="min-h-11 justify-center px-2"
260 >
261 <Text className="text-sm text-foreground">Work locally</Text>
262 </Pressable>
263 ) : null}
264 </View>
265 ) : null}
266 </ScrollView>
267 </WorktreeSetupSheet>
268 );
270​
271function StageRow({
272 stage,
273 scriptName,
274 now,
275 compact = false,
276 animate = true,
277}: {
278 stage: WorktreeSetupStage;
279 scriptName: string | null;
280 now: number;
281 compact?: boolean;
282 animate?: boolean;
283}) {
284 const label =
285 stage.id === "setup-script"
286 ? (scriptName ?? worktreeSetupStageLabel(stage.id))
287 : worktreeSetupStageLabel(stage.id);
288 const detail =
289 stage.status === "pending"
290 ? null
291 : stage.status === "skipped"
292 ? (stage.detail ?? "skipped")
293 : stage.id === "checkout" && stage.status === "running" && stage.percent !== null
294 ? `${stage.percent}%`
295 : stage.detail;
296 return (
297 <View
298 accessibilityLabel={`${label}, ${stage.status}`}
299 className={compact ? "min-h-8 flex-row items-center" : "min-h-11 flex-row items-center"}
300 style={{ columnGap: 8, opacity: stage.status === "pending" ? 0.4 : 1 }}
301 >
302 <View className="w-6 items-center">
303 {stage.status === "running" && animate ? (
304 <ActivityIndicator
305 size="small"
306 colorClassName="accent-adaptive-neutral-500-400"
307 style={{ transform: [{ scale: 0.75 }] }}
308 />
309 ) : (
310 <SymbolView
311 name={icons[stage.status]}
312 size={14}
313 tintColorClassName={
314 stage.status === "failed"
315 ? "accent-adaptive-rose-700-300"
316 : stage.status === "warning"
317 ? "accent-warning-foreground"
318 : "accent-adaptive-neutral-500-400"
319 }
320 />
321 )}
322 </View>
323 {stage.status === "running" && animate ? (
324 <ShimmeringWorkContent
325 icon="clock"
326 iconSubtleColor="#737373"
327 label={label}
328 showIcon={false}
329 />
330 ) : (
331 <Text
332 numberOfLines={1}
333 className={
334 stage.status === "failed"
335 ? "flex-1 text-sm text-adaptive-rose-700-300"
336 : "flex-1 text-sm text-adaptive-neutral-600-400"
337 }
338 >
339 {label}
340 </Text>
341 )}
342 {detail ? (
343 <Text
344 numberOfLines={1}
345 className="max-w-[25%] shrink text-2xs text-adaptive-neutral-600-400"
346 >
347 {detail}
348 </Text>
349 ) : null}
350 {stage.status !== "pending" && stage.status !== "skipped" ? (
351 <Text
352 className="text-2xs text-adaptive-neutral-600-400"
353 style={{ fontVariant: ["tabular-nums"] }}
354 >
355 {elapsed(stage.startedAt, stage.endedAt, now)}
356 </Text>
357 ) : null}
358 </View>
359 );
361​
362const OUTPUT_TAIL_SLOTS = [0, 1, 2, 3] as const;
363​
364/** Fixed four-line output window, shown only in Details. */
365function OutputTail({ lines, failed }: { lines: ReadonlyArray<string>; failed: boolean }) {
366 return (
367 <View
368 accessibilityLabel="Setup script output"
369 className={
370 failed
371 ? "mb-2 ml-8 rounded-md border border-adaptive-rose-700-300/20 bg-adaptive-rose-50-950 px-3 py-2"
372 : "mb-2 ml-8 rounded-md border border-border bg-adaptive-neutral-100-900 px-3 py-2"
373 }
374 >
375 {OUTPUT_TAIL_SLOTS.map((slot) => (
376 <Text
377 key={slot}
378 selectable
379 numberOfLines={1}
380 className={
381 failed
382 ? "text-2xs leading-5 text-adaptive-rose-700-300 ios:font-[family-name:Menlo] android:font-mono"
383 : "text-2xs leading-5 text-adaptive-neutral-600-400 ios:font-[family-name:Menlo] android:font-mono"
384 }
385 >
386 {lines[lines.length - OUTPUT_TAIL_SLOTS.length + slot] || "\u00a0"}
387 </Text>
388 ))}
389 </View>
390 );