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