Open History to see the last commit
1import type { SubagentPillSegment } from "@t3tools/client-runtime/state/thread-subagents";
2import { formatDuration } from "@t3tools/shared/orchestrationTiming";
3import { GlassContainer, GlassView } from "expo-glass-effect";
4import { type ReactNode, useEffect, useRef, useState } from "react";
5import {
6 ActivityIndicator,
7 type LayoutChangeEvent,
8 Pressable,
9 Text as SystemText,
10 useWindowDimensions,
11 View,
12} from "react-native";
13import Animated, {
14 Easing,
15 FadeIn,
16 FadeOut,
17 ReduceMotion,
18 type SharedValue,
19 useAnimatedStyle,
20 useSharedValue,
21 withTiming,
22} from "react-native-reanimated";
23import { withUniwind } from "uniwind";
24
25import { AppText as Text } from "../../components/AppText";
26import { SymbolView } from "../../components/AppSymbol";
27import { ControlPill } from "../../components/ControlPill";
28import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
29import { BrowserPreviewButton } from "../browser/browser-preview-button";
30import { DevicePreviewButton } from "../devices/device-preview-button";
31import type { FloatingWorkingStatus } from "./floating-working-status";
32import { ShimmeringWorkContent } from "./thread-work-log";
33import { useVisibleSecondClock } from "./use-visible-second-clock";
34
35const CONTROL_HEIGHT = 38.5; // h-11 with the mobile 14px rem
36// The collapsed composer capsule starts 6 below its overlay's top edge, so
37// the pill sits at (gap - 6) above the overlay to leave the same gap to the
38// capsule as the feed's end inset leaves between it and the last row.
39const CONTROL_GAP = 8;
40const COMPOSER_CAPSULE_INSET = 6;
41const GLASS_MERGE_SPACING = 12;
42const CONTROL_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
43const CONTROL_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
44const CONTROL_TIMING = {
45 duration: 240,
46 easing: Easing.out(Easing.cubic),
47 reduceMotion: ReduceMotion.System,
48} as const;
49const CONTROL_SEPARATION = (16 + CONTROL_HEIGHT) / 2;
50// Both rows share the same centered anchor, so the outgoing one clears fast and
51// the incoming one waits for it to be mostly gone before it starts to show.
52const LABEL_ENTERING = FadeIn.duration(160).delay(80).reduceMotion(ReduceMotion.System);
53const LABEL_EXITING = FadeOut.duration(100).reduceMotion(ReduceMotion.System);
54
55// Expo reapplies glass after native layout and window reattachment, when UIKit
56// can otherwise leave the label visible but lose the material behind it.
57const UniwindGlassView = withUniwind(GlassView, {
58 style: { fromClassName: "className" },
59});
60const UniwindGlassContainer = withUniwind(GlassContainer, {
61 style: { fromClassName: "className" },
62});
63const AnimatedGlassView = Animated.createAnimatedComponent(UniwindGlassView);
64
65const CONTROL_OVERLAY_OFFSET = CONTROL_HEIGHT + CONTROL_GAP - COMPOSER_CAPSULE_INSET;
66export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTROL_GAP;
67
68export function FloatingWorkingControl(props: {
69 readonly colorScheme: "light" | "dark";
70 readonly status: FloatingWorkingStatus | null;
71 readonly devicePreview: { readonly count: number; readonly onPress: () => void } | null;
72 readonly browserPreview: { readonly count: number; readonly onPress: () => void } | null;
73 readonly showScrollToEnd: boolean;
74 readonly onScrollToEnd: () => void;
75 readonly agents: SubagentPillSegment | null;
76 readonly onOpenAgents: () => void;
77 readonly queuedCount: number;
78 readonly onOpenQueue: () => void;
79 /** Extra distance to rise above the anchor, e.g. an overlay card's coverage. */
80 readonly lift?: SharedValue<number>;
81}) {
82 const { width: windowWidth } = useWindowDimensions();
83 const [overlayWidth, setOverlayWidth] = useState(windowWidth);
84 const [queueWidth, setQueueWidth] = useState(0);
85 const [agentsWidth, setAgentsWidth] = useState(0);
86 const hasQueue = props.queuedCount > 0;
87 const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
88 const [previewWidth, setPreviewWidth] = useState(0);
89 const agents = props.agents;
90 const hasAgents = agents !== null;
91 // Segments keep their measured width; only the status label absorbs the
92 // remainder, so a long "Working 12m 04s" truncates before a count does.
93 const labelWidth = Math.max(
94 0,
95 Math.min(overlayWidth, windowWidth) -
96 CONTROL_HEIGHT -
97 32 -
98 (hasQueue ? queueWidth : 0) -
99 (hasAgents ? agentsWidth : 0) -
100 (hasPreview ? previewWidth : 0),
101 );
102 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
103
104 useEffect(() => {
105 separationProgress.value = withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING);
106 }, [props.showScrollToEnd, separationProgress]);
107
108 const lift = props.lift;
109 const liftStyle = useAnimatedStyle(() => ({
110 transform: [{ translateY: -(lift?.value ?? 0) }],
111 }));
112 const arrowTransformStyle = useAnimatedStyle(() => ({
113 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
114 }));
115 const arrowContentStyle = useAnimatedStyle(() => ({
116 opacity: separationProgress.value,
117 }));
118
119 // Animate an in-flow sizer so native glass receives real layout updates.
120 // Measure labels in a separate, fixed-width host: measuring against the
121 // animated capsule constrains the incoming text to each intermediate width
122 // and repeatedly retargets the animation as it grows.
123 const capsuleWidth = useSharedValue<number | null>(null);
124 const measuredWidthRef = useRef<number | null>(null);
125 const handleLabelLayout = (event: LayoutChangeEvent) => {
126 const width = event.nativeEvent.layout.width;
127 if (width === measuredWidthRef.current) {
128 return;
129 }
130 const first = measuredWidthRef.current === null;
131 measuredWidthRef.current = width;
132 capsuleWidth.value = first ? width : withTiming(width, CONTROL_TIMING);
133 };
134 // Forget the width while no label is shown so the next one appears at its
135 // own size instead of animating from the previous label's.
136 const hasStatus = props.status !== null;
137 const hasCapsule = hasStatus || hasQueue || hasAgents || hasPreview;
138 useEffect(() => {
139 if (!hasStatus) {
140 measuredWidthRef.current = null;
141 capsuleWidth.value = null;
142 }
143 }, [capsuleWidth, hasStatus]);
144 const capsuleStyle = useAnimatedStyle(() => ({
145 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
146 }));
147 // Zero until the first measurement lands, so the capsule never paints around
148 // a label it has not sized to yet.
149 const capsuleSizerStyle = useAnimatedStyle(() => ({
150 width: capsuleWidth.value ?? 0,
151 }));
152
153 if (!hasCapsule && !props.showScrollToEnd) {
154 return null;
155 }
156
157 // Both preview buttons fit beside each other only as icons.
158 const previewCompact =
159 hasStatus ||
160 hasAgents ||
161 hasQueue ||
162 (props.devicePreview !== null && props.browserPreview !== null);
163 // The queue, agents, questions, and reconnect labels have separate tap targets.
164 const statusInteractive =
165 props.status?.kind === "connection" || props.status?.kind === "child-input";
166 const capsuleInteractive = statusInteractive || hasQueue || hasAgents || hasPreview;
167 // The host stays centered on the capsule, but its measurement constraint
168 // comes from the overlay, independent of the capsule's current width.
169 const statusContent =
170 props.status !== null ? (
171 <View
172 pointerEvents={statusInteractive ? "box-none" : "none"}
173 className="h-11 items-center justify-center"
174 >
175 <Animated.View className="h-11" style={capsuleSizerStyle} />
176 <View
177 pointerEvents={statusInteractive ? "box-none" : "none"}
178 className="absolute h-11 items-center justify-center"
179 style={{ width: labelWidth }}
180 >
181 <FloatingStatusLabel
182 key={
183 props.status.kind === "working" || props.status.kind === "compacting"
184 ? props.status.kind
185 : `${props.status.kind}:${props.status.label}`
186 }
187 status={props.status}
188 onLayout={handleLabelLayout}
189 />
190 </View>
191 </View>
192 ) : null;
193
194 const capsuleContent = (
195 <View className="flex-row items-center">
196 {statusContent}
197 {hasPreview ? (
198 <View
199 className="h-11 flex-row items-center"
200 onLayout={(event) => setPreviewWidth(event.nativeEvent.layout.width)}
201 >
202 {hasStatus ? <View className="h-4 w-px bg-border" /> : null}
203 {props.devicePreview !== null ? (
204 <DevicePreviewButton {...props.devicePreview} compact={previewCompact} />
205 ) : null}
206 {props.browserPreview !== null ? (
207 <BrowserPreviewButton {...props.browserPreview} compact={previewCompact} />
208 ) : null}
209 </View>
210 ) : null}
211 {agents !== null ? (
212 <Pressable
213 accessibilityRole="button"
214 accessibilityLabel={`Open agents, ${agents.accessibilityLabel}`}
215 accessibilityHint="Opens this turn's subagents"
216 onPress={props.onOpenAgents}
217 onLayout={(event) => setAgentsWidth(event.nativeEvent.layout.width)}
218 className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
219 >
220 {hasStatus || hasPreview ? <View className="mr-1 h-4 w-px bg-border" /> : null}
221 <SymbolView name="person.2" size={13} tintColorClassName="accent-foreground-muted" />
222 <Text className="font-t3-medium text-xs tabular-nums" numberOfLines={1}>
223 {agents.label}
224 </Text>
225 </Pressable>
226 ) : null}
227 {hasQueue ? (
228 <Pressable
229 accessibilityRole="button"
230 accessibilityLabel={`Open queue, ${props.queuedCount} messages`}
231 accessibilityHint="Opens queued messages for reordering, steering, or removal"
232 onPress={props.onOpenQueue}
233 onLayout={(event) => setQueueWidth(event.nativeEvent.layout.width)}
234 style={{ maxWidth: Math.min(overlayWidth, windowWidth) * 0.45 }}
235 className="h-11 flex-row items-center gap-2 px-3 active:opacity-70"
236 >
237 {hasStatus || hasPreview || hasAgents ? (
238 <View className="mr-1 h-4 w-px bg-border" />
239 ) : null}
240 <SymbolView name="list.number" size={13} tintColorClassName="accent-foreground-muted" />
241 <Text className="shrink font-t3-medium text-xs tabular-nums" numberOfLines={1}>
242 {props.queuedCount} queued
243 </Text>
244 </Pressable>
245 ) : null}
246 </View>
247 );
248
249 return (
250 <Animated.View
251 pointerEvents="box-none"
252 className="absolute left-0 right-0 z-20 items-center"
253 style={[{ top: -CONTROL_OVERLAY_OFFSET }, liftStyle]}
254 onLayout={(event) => setOverlayWidth(event.nativeEvent.layout.width)}
255 entering={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_ENTERING}
256 exiting={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_EXITING}
257 >
258 {hasCapsule && NATIVE_LIQUID_GLASS_SUPPORTED ? (
259 <UniwindGlassContainer
260 spacing={GLASS_MERGE_SPACING}
261 pointerEvents="box-none"
262 className="flex-row items-center gap-4"
263 >
264 <AnimatedGlassView
265 colorScheme={props.colorScheme}
266 glassEffectStyle="regular"
267 isInteractive={capsuleInteractive}
268 pointerEvents={capsuleInteractive ? "box-none" : "none"}
269 className="h-11 items-center justify-center overflow-hidden rounded-full"
270 style={capsuleStyle}
271 >
272 {capsuleContent}
273 </AnimatedGlassView>
274
275 <AnimatedGlassView
276 colorScheme={props.colorScheme}
277 glassEffectStyle="regular"
278 isInteractive
279 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
280 accessibilityElementsHidden={!props.showScrollToEnd}
281 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
282 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
283 style={arrowTransformStyle}
284 >
285 <Animated.View style={arrowContentStyle}>
286 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
287 </Animated.View>
288 </AnimatedGlassView>
289 </UniwindGlassContainer>
290 ) : hasCapsule ? (
291 <View pointerEvents="box-none" className="flex-row items-center gap-4">
292 <Animated.View
293 pointerEvents={capsuleInteractive ? "box-none" : "none"}
294 className="h-11 items-center justify-center overflow-hidden rounded-full border border-border bg-glass-fallback shadow-md shadow-black/10"
295 style={capsuleStyle}
296 >
297 {capsuleContent}
298 </Animated.View>
299
300 <Animated.View
301 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
302 accessibilityElementsHidden={!props.showScrollToEnd}
303 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
304 style={[arrowTransformStyle, arrowContentStyle]}
305 >
306 <ControlPill
307 accessibilityLabel="Scroll to end"
308 activateOnPressIn
309 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
310 disabled={!props.showScrollToEnd}
311 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
312 onPress={props.onScrollToEnd}
313 />
314 </Animated.View>
315 </View>
316 ) : NATIVE_LIQUID_GLASS_SUPPORTED ? (
317 <UniwindGlassView
318 colorScheme={props.colorScheme}
319 glassEffectStyle="regular"
320 isInteractive
321 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
322 >
323 <ScrollToEndButton onPress={props.onScrollToEnd} />
324 </UniwindGlassView>
325 ) : (
326 <ControlPill
327 accessibilityLabel="Scroll to end"
328 activateOnPressIn
329 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
330 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
331 onPress={props.onScrollToEnd}
332 />
333 )}
334 </Animated.View>
335 );
336}
337
338function CompactingLabel(props: { readonly onLayout: (event: LayoutChangeEvent) => void }) {
339 return (
340 <StatusLabelRow accessibilityLabel="Compacting" className="gap-1.5" onLayout={props.onLayout}>
341 <SymbolView
342 name="arrow.down.right.and.arrow.up.left"
343 size={13}
344 tintColorClassName="foreground"
345 type="monochrome"
346 />
347 <Text className="font-t3-medium text-xs text-foreground">Compacting…</Text>
348 </StatusLabelRow>
349 );
350}
351
352function FloatingStatusLabel(props: {
353 readonly status: FloatingWorkingStatus;
354 readonly onLayout: (event: LayoutChangeEvent) => void;
355}) {
356 // Keyed by kind so a swap mounts a fresh row and the two cross-fade while
357 // the capsule animates to the new row's measured width.
358 if (props.status.kind === "syncing") {
359 return (
360 <StatusLabelRow
361 key="syncing"
362 accessibilityLabel={props.status.label}
363 className="gap-2"
364 onLayout={props.onLayout}
365 >
366 <ActivityIndicator size="small" colorClassName="accent-icon-muted" />
367 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
368 {props.status.label}
369 </Text>
370 </StatusLabelRow>
371 );
372 }
373 if (props.status.kind === "compacting") {
374 return <CompactingLabel key="compacting" onLayout={props.onLayout} />;
375 }
376 if (props.status.kind === "child-input") {
377 return (
378 <StatusLabelRow
379 key="child-input"
380 accessibilityLabel={props.status.accessibilityLabel}
381 accessibilityRole="button"
382 className="gap-2"
383 onLayout={props.onLayout}
384 onPress={props.status.onPress}
385 >
386 <SymbolView name="text.bubble" size={13} tintColorClassName="foreground" />
387 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
388 {props.status.label}
389 </Text>
390 </StatusLabelRow>
391 );
392 }
393 if (props.status.kind === "connection") {
394 return (
395 <StatusLabelRow
396 key="connection"
397 accessibilityLabel={props.status.label}
398 accessibilityRole="button"
399 className="gap-2"
400 onLayout={props.onLayout}
401 onPress={props.status.onPress}
402 >
403 {props.status.tone === "reconnecting" ? (
404 <ActivityIndicator size="small" colorClassName="accent-icon-muted" />
405 ) : (
406 <View className="h-2 w-2 rounded-full bg-red-500" />
407 )}
408 <Text
409 className="max-w-[260px] shrink font-t3-medium text-xs text-foreground"
410 numberOfLines={1}
411 >
412 {props.status.label}
413 </Text>
414 </StatusLabelRow>
415 );
416 }
417 if (props.status.kind === "background") {
418 return (
419 <StatusLabelRow
420 key="background"
421 accessibilityLabel={props.status.accessibilityLabel}
422 className="gap-2"
423 onLayout={props.onLayout}
424 >
425 {/* A dev server can run for hours after the agent is done, so only work
426 that will wake the agent gets the bolt. */}
427 <SymbolView
428 name={props.status.waiting ? { ios: "bolt", android: "bolt" } : "terminal"}
429 size={13}
430 tintColorClassName="foreground"
431 type="monochrome"
432 />
433 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
434 {props.status.label}
435 </Text>
436 </StatusLabelRow>
437 );
438 }
439 if (props.status.kind === "goal") {
440 return (
441 <StatusLabelRow
442 key="goal"
443 accessibilityLabel={props.status.accessibilityLabel}
444 className="gap-2"
445 onLayout={props.onLayout}
446 >
447 <SymbolView name="target" size={13} tintColorClassName="foreground" type="monochrome" />
448 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
449 {props.status.label}
450 </Text>
451 </StatusLabelRow>
452 );
453 }
454 if (props.status.kind === "preparing") {
455 return (
456 <StatusLabelRow
457 key="preparing"
458 accessibilityLabel={props.status.label}
459 className="gap-1.5"
460 onLayout={props.onLayout}
461 >
462 <SymbolView
463 name="arrow.triangle.branch"
464 size={13}
465 tintColorClassName="foreground"
466 type="monochrome"
467 />
468 <ShimmeringWorkContent
469 className="flex-none"
470 textClassName="font-t3-medium"
471 compact
472 icon="arrow.triangle.branch"
473 iconSubtleColor="transparent"
474 label={props.status.label}
475 showIcon={false}
476 />
477 </StatusLabelRow>
478 );
479 }
480 return (
481 <WorkingDuration key="working" startedAt={props.status.startedAt} onLayout={props.onLayout} />
482 );
483}
484
485// Absolute rows cross-fade around the same center without affecting each other.
486function StatusLabelRow(props: {
487 readonly accessibilityLabel?: string;
488 readonly accessibilityRole?: "button";
489 readonly className?: string;
490 readonly children: ReactNode;
491 readonly onLayout: (event: LayoutChangeEvent) => void;
492 readonly onPress?: () => void;
493}) {
494 const rowClassName = `h-11 flex-row items-center px-4 ${props.className ?? ""}`;
495 return (
496 <Animated.View
497 className="absolute max-w-full"
498 entering={LABEL_ENTERING}
499 exiting={LABEL_EXITING}
500 onLayout={props.onLayout}
501 >
502 {props.onPress ? (
503 <Pressable
504 accessibilityLabel={props.accessibilityLabel}
505 accessibilityRole={props.accessibilityRole}
506 className={`${rowClassName} active:opacity-70`}
507 onPress={props.onPress}
508 >
509 {props.children}
510 </Pressable>
511 ) : (
512 <View accessible accessibilityLabel={props.accessibilityLabel} className={rowClassName}>
513 {props.children}
514 </View>
515 )}
516 </Animated.View>
517 );
518}
519
520function WorkingDuration(props: {
521 readonly startedAt: string;
522 readonly onLayout: (event: LayoutChangeEvent) => void;
523}) {
524 return (
525 <StatusLabelRow onLayout={props.onLayout}>
526 <WorkingTimer startedAt={props.startedAt} />
527 </StatusLabelRow>
528 );
529}
530
531export function WorkingTimer(props: { readonly startedAt: string }) {
532 const nowMs = useVisibleSecondClock(true);
533 return (
534 <SystemText
535 className="shrink text-xs text-foreground"
536 numberOfLines={1}
537 style={{ fontVariant: ["tabular-nums"], fontWeight: "500" }}
538 >
539 Working {formatWorkingDuration(props.startedAt, nowMs)}
540 </SystemText>
541 );
542}
543
544function formatWorkingDuration(startedAt: string, nowMs: number): string {
545 const startedAtMs = Date.parse(startedAt);
546 if (!Number.isFinite(startedAtMs) || nowMs <= startedAtMs) {
547 return "0s";
548 }
549
550 const totalSeconds = Math.floor((nowMs - startedAtMs) / 1_000);
551 if (totalSeconds < 60) {
552 return `${totalSeconds}s`;
553 }
554 if (totalSeconds >= 3_600) {
555 return formatDuration(totalSeconds * 1_000);
556 }
557
558 const minutes = Math.floor(totalSeconds / 60);
559 const seconds = String(totalSeconds % 60).padStart(2, "0");
560 return `${minutes}m ${seconds}s`;
561}
562
563function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
564 return (
565 <ControlPill
566 accessibilityLabel="Scroll to end"
567 activateOnPressIn
568 className="h-11 w-11 bg-transparent"
569 disabled={props.disabled}
570 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
571 onPress={props.onPress}
572 />
573 );
574}