Open History to see the last commit
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;
23}
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;
28}
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-foreground-secondary"
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-icon-muted"
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-danger-foreground"
110 />
111 ) : null}
112 <Text numberOfLines={1} className="shrink text-2xs text-foreground-secondary">
113 {backgroundSetup ? scriptName : "Details"}
114 </Text>
115 {!backgroundSetup ? (
116 <SymbolView name="chevron.right" size={10} tintColorClassName="accent-icon-muted" />
117 ) : null}
118 </View>
119 </Pressable>
120 </View>
121 {!handedOff ? (
122 <View className="pt-1 pb-2">
123 {snapshot.stages
124 .filter((stage) => stage.id !== "agent")
125 .map((stage) => (
126 <StageRow
127 key={stage.id}
128 stage={stage}
129 scriptName={snapshot.setupScript?.name ?? null}
130 now={now}
131 compact
132 animate={!detailsOpen}
133 />
134 ))}
135 </View>
136 ) : null}
137 {detailsOpen ? (
138 <SetupDetailsSheet {...props} now={now} onClose={() => setDetailsOpen(false)} />
139 ) : null}
140 </View>
141 );
142}
143
144export function WorktreeWorkingHeader({ startedAt }: { startedAt: string }) {
145 const now = useSetupClock(true);
146 return (
147 <View className="py-1">
148 <View className="min-h-11 flex-row items-center border-b border-border px-1">
149 <HeaderLabel label={`Working for ${elapsed(startedAt, null, now) ?? "0s"}`} active />
150 </View>
151 </View>
152 );
153}
154
155function HeaderLabel({
156 label,
157 active,
158 failed = false,
159}: {
160 label: string;
161 active: boolean;
162 failed?: boolean;
163}) {
164 return active ? (
165 <ShimmeringWorkContent
166 icon="clock"
167 iconSubtleColor="transparent"
168 label={label}
169 showIcon={false}
170 />
171 ) : (
172 <Text
173 numberOfLines={1}
174 className={
175 failed
176 ? "flex-1 text-sm text-danger-foreground"
177 : "flex-1 text-sm text-foreground-secondary"
178 }
179 style={{ fontVariant: ["tabular-nums"] }}
180 >
181 {label}
182 </Text>
183 );
184}
185
186function useSetupClock(active: boolean) {
187 const [now, setNow] = useState(Date.now);
188 const [appState, setAppState] = useState(AppState.currentState);
189 useEffect(() => {
190 const subscription = AppState.addEventListener("change", setAppState);
191 return () => subscription.remove();
192 }, []);
193 useEffect(() => {
194 if (!active || appState !== "active") return;
195 const timer = setInterval(() => setNow(Date.now()), 1000);
196 return () => clearInterval(timer);
197 }, [active, appState]);
198 return now;
199}
200
201function SetupDetailsSheet({
202 snapshot,
203 turnStarted,
204 onCancel,
205 onWorkLocally,
206 onClose,
207 now,
208}: WorktreeSetupCardProps & { onClose: () => void; now: number }) {
209 const insets = useSafeAreaInsets();
210 const [bodyHeight, setBodyHeight] = useState(0);
211 const canCancel = snapshot.phase === "running" && !turnStarted;
212 return (
213 <WorktreeSetupSheet height={bodyHeight} onClose={onClose}>
214 <ScrollView
215 bounces={false}
216 onContentSizeChange={(_width, height) => setBodyHeight(height)}
217 contentContainerStyle={{
218 paddingHorizontal: 20,
219 paddingTop: 12,
220 paddingBottom: Math.max(20, insets.bottom),
221 }}
222 >
223 {/* Agent startup is the header handoff, not a fifth setup step. */}
224 {snapshot.stages
225 .filter((stage) => stage.id !== "agent")
226 .map((stage) => (
227 <View key={stage.id}>
228 <StageRow stage={stage} scriptName={snapshot.setupScript?.name ?? null} now={now} />
229 {stage.id === "setup-script" &&
230 (stage.status === "running" || stage.status === "failed" || stage.tail.length > 0) ? (
231 <OutputTail lines={stage.tail} failed={stage.status === "failed"} />
232 ) : null}
233 </View>
234 ))}
235 {snapshot.phase === "failed" && snapshot.error ? (
236 <Text selectable className="ml-8 mt-2 text-xs text-danger-foreground">
237 {snapshot.error}
238 </Text>
239 ) : null}
240 {canCancel ? (
241 <View className="mt-3 flex-row items-center justify-end gap-4 border-t border-border pt-1">
242 <Pressable
243 accessibilityRole="button"
244 accessibilityLabel="Cancel worktree setup"
245 onPress={() => {
246 onClose();
247 onCancel();
248 }}
249 className="min-h-11 justify-center px-2"
250 >
251 <Text className="text-sm text-danger-foreground">Cancel setup</Text>
252 </Pressable>
253 {onWorkLocally ? (
254 <Pressable
255 accessibilityRole="button"
256 onPress={() => {
257 onClose();
258 onWorkLocally();
259 }}
260 className="min-h-11 justify-center px-2"
261 >
262 <Text className="text-sm text-foreground">Work locally</Text>
263 </Pressable>
264 ) : null}
265 </View>
266 ) : null}
267 </ScrollView>
268 </WorktreeSetupSheet>
269 );
270}
271
272function StageRow({
273 stage,
274 scriptName,
275 now,
276 compact = false,
277 animate = true,
278}: {
279 stage: WorktreeSetupStage;
280 scriptName: string | null;
281 now: number;
282 compact?: boolean;
283 animate?: boolean;
284}) {
285 const label =
286 stage.id === "setup-script"
287 ? (scriptName ?? worktreeSetupStageLabel(stage.id))
288 : worktreeSetupStageLabel(stage.id);
289 const detail =
290 stage.status === "pending"
291 ? null
292 : stage.status === "skipped"
293 ? (stage.detail ?? "skipped")
294 : stage.id === "checkout" && stage.status === "running" && stage.percent !== null
295 ? `${stage.percent}%`
296 : stage.detail;
297 return (
298 <View
299 accessibilityLabel={`${label}, ${stage.status}`}
300 className={compact ? "min-h-8 flex-row items-center" : "min-h-11 flex-row items-center"}
301 style={{ columnGap: 8, opacity: stage.status === "pending" ? 0.4 : 1 }}
302 >
303 <View className="w-6 items-center">
304 {stage.status === "running" && animate ? (
305 <ActivityIndicator
306 size="small"
307 colorClassName="accent-icon-muted"
308 style={{ transform: [{ scale: 0.75 }] }}
309 />
310 ) : (
311 <SymbolView
312 name={icons[stage.status]}
313 size={14}
314 tintColorClassName={
315 stage.status === "failed"
316 ? "accent-danger-foreground"
317 : stage.status === "warning"
318 ? "accent-warning-foreground"
319 : "accent-icon-muted"
320 }
321 />
322 )}
323 </View>
324 {stage.status === "running" && animate ? (
325 <ShimmeringWorkContent
326 icon="clock"
327 iconSubtleColor="transparent"
328 label={label}
329 showIcon={false}
330 />
331 ) : (
332 <Text
333 numberOfLines={1}
334 className={
335 stage.status === "failed"
336 ? "flex-1 text-sm text-danger-foreground"
337 : "flex-1 text-sm text-foreground-secondary"
338 }
339 >
340 {label}
341 </Text>
342 )}
343 {detail ? (
344 <Text numberOfLines={1} className="max-w-[25%] shrink text-2xs text-foreground-secondary">
345 {detail}
346 </Text>
347 ) : null}
348 {stage.status !== "pending" && stage.status !== "skipped" ? (
349 <Text
350 className="text-2xs text-foreground-secondary"
351 style={{ fontVariant: ["tabular-nums"] }}
352 >
353 {elapsed(stage.startedAt, stage.endedAt, now)}
354 </Text>
355 ) : null}
356 </View>
357 );
358}
359
360const OUTPUT_TAIL_SLOTS = [0, 1, 2, 3] as const;
361
362/** Fixed four-line output window, shown only in Details. */
363function OutputTail({ lines, failed }: { lines: ReadonlyArray<string>; failed: boolean }) {
364 return (
365 <View
366 accessibilityLabel="Setup script output"
367 className={
368 failed
369 ? "mb-2 ml-8 rounded-md border border-danger-border bg-danger px-3 py-2"
370 : "mb-2 ml-8 rounded-md border border-border bg-card-alt px-3 py-2"
371 }
372 >
373 {OUTPUT_TAIL_SLOTS.map((slot) => (
374 <Text
375 key={slot}
376 selectable
377 numberOfLines={1}
378 className={
379 failed
380 ? "text-2xs leading-5 text-danger-foreground ios:font-[family-name:Menlo] android:font-mono"
381 : "text-2xs leading-5 text-foreground-secondary ios:font-[family-name:Menlo] android:font-mono"
382 }
383 >
384 {lines[lines.length - OUTPUT_TAIL_SLOTS.length + slot] || "\u00a0"}
385 </Text>
386 ))}
387 </View>
388 );
389}