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 type LayoutChangeEvent,
7 Pressable,
8 Text as SystemText,
9 useWindowDimensions,
10 View,
11} from "react-native";
12import Animated, {
13 Easing,
14 FadeIn,
15 FadeOut,
16 ReduceMotion,
17 type SharedValue,
18 useAnimatedStyle,
19 useSharedValue,
20 withTiming,
21} from "react-native-reanimated";
22import { withUniwind } from "uniwind";
23
24import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
25import { AppText as Text } from "../../components/ui/AppText";
26import { SymbolView } from "../../components/ui/AppSymbol";
27import { ControlPill } from "../../components/ui/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;
36const CONTROL_GAP = 8;
37const COMPOSER_CAPSULE_INSET = 6;
38const GLASS_MERGE_SPACING = 12;
39const CONTROL_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
40const CONTROL_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
41const CONTROL_TIMING = {
42 duration: 240,
43 easing: Easing.out(Easing.cubic),
44 reduceMotion: ReduceMotion.System,
45} as const;
46const CONTROL_ROW_GAP = 14;
47const CONTROL_SEPARATION = (CONTROL_ROW_GAP + CONTROL_HEIGHT) / 2;
48const LABEL_ENTERING = FadeIn.duration(160).delay(80).reduceMotion(ReduceMotion.System);
49const LABEL_EXITING = FadeOut.duration(100).reduceMotion(ReduceMotion.System);
50
51// Expo reapplies glass after layout or reattachment, restoring material UIKit can lose behind visible labels.
52const UniwindGlassView = withUniwind(GlassView, {
53 style: { fromClassName: "className" },
54});
55const UniwindGlassContainer = withUniwind(GlassContainer, {
56 style: { fromClassName: "className" },
57});
58const AnimatedGlassView = Animated.createAnimatedComponent(UniwindGlassView);
59
60// Segment widths feed back into labelWidth; ignore sub-point changes to avoid rounding-driven ellipsis loops.
61function settleWidth(next: number) {
62 return (current: number) => (Math.abs(current - next) < 1 ? current : next);
63}
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 readonly lift?: SharedValue<number>;
80}) {
81 const { width: windowWidth } = useWindowDimensions();
82 const [overlayWidth, setOverlayWidth] = useState(windowWidth);
83 const [queueWidth, setQueueWidth] = useState(0);
84 const [actionWidth, setActionWidth] = useState(0);
85 const action =
86 props.status?.kind === "background" ||
87 props.status?.kind === "lifecycle" ||
88 props.status?.kind === "stop"
89 ? props.status.action
90 : null;
91 const [agentsWidth, setAgentsWidth] = useState(0);
92 const hasQueue = props.queuedCount > 0;
93 const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
94 const [previewWidth, setPreviewWidth] = useState(0);
95 const agents = props.agents;
96 const hasAgents = agents !== null;
97 const labelWidth = Math.max(
98 0,
99 Math.min(overlayWidth, windowWidth) -
100 CONTROL_HEIGHT -
101 32 -
102 (action !== null ? actionWidth : 0) -
103 (hasQueue ? queueWidth : 0) -
104 (hasAgents ? agentsWidth : 0) -
105 (hasPreview ? previewWidth : 0),
106 );
107 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
108
109 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated separation value when scroll-to-end visibility changes
110 useEffect(() => {
111 separationProgress.set(withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING));
112 }, [props.showScrollToEnd, separationProgress]);
113
114 const lift = props.lift;
115 const liftStyle = useAnimatedStyle(() => ({
116 transform: [{ translateY: -(lift?.value ?? 0) }],
117 }));
118 const arrowTransformStyle = useAnimatedStyle(() => ({
119 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
120 }));
121 const arrowContentStyle = useAnimatedStyle(() => ({
122 opacity: separationProgress.value,
123 }));
124
125 // Animate an in-flow sizer for native glass layout. Measure labels at fixed width to avoid repeated animation retargeting.
126 const capsuleWidth = useSharedValue<number | null>(null);
127 const measuredWidthRef = useRef<number | null>(null);
128 const handleLabelLayout = (event: LayoutChangeEvent) => {
129 const width = event.nativeEvent.layout.width;
130 if (width === measuredWidthRef.current) {
131 return;
132 }
133 const first = measuredWidthRef.current === null;
134 measuredWidthRef.current = width;
135 capsuleWidth.set(first ? width : withTiming(width, CONTROL_TIMING));
136 };
137 const hasStatus = props.status !== null;
138 const hasCapsule = hasStatus || hasQueue || hasAgents || hasPreview;
139 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the Reanimated capsule width value and measured width when the status goes away
140 useEffect(() => {
141 if (!hasStatus) {
142 measuredWidthRef.current = null;
143 capsuleWidth.set(null);
144 }
145 }, [capsuleWidth, hasStatus]);
146 const capsuleStyle = useAnimatedStyle(() => ({
147 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
148 }));
149 const capsuleSizerStyle = useAnimatedStyle(() => ({
150 width: capsuleWidth.value ?? 0,
151 }));
152
153 if (!hasCapsule && !props.showScrollToEnd) {
154 return null;
155 }
156
157 const previewCompact =
158 hasStatus ||
159 hasAgents ||
160 hasQueue ||
161 (props.devicePreview !== null && props.browserPreview !== null);
162 const statusInteractive =
163 props.status?.kind === "connection" ||
164 props.status?.kind === "child-input" ||
165 props.status?.kind === "background";
166 const capsuleInteractive =
167 statusInteractive || action !== null || hasQueue || hasAgents || hasPreview;
168 const statusContent =
169 props.status !== null ? (
170 <View
171 pointerEvents={statusInteractive ? "box-none" : "none"}
172 className="h-11 items-center justify-center"
173 >
174 <Animated.View className="h-11" style={capsuleSizerStyle} />
175 <View
176 pointerEvents={statusInteractive ? "box-none" : "none"}
177 className="absolute h-11 items-center justify-center"
178 style={{ width: labelWidth }}
179 >
180 <FloatingStatusLabel
181 key={
182 props.status.kind === "working" || props.status.kind === "compacting"
183 ? props.status.kind
184 : `${props.status.kind}:${props.status.label}`
185 }
186 status={props.status}
187 onLayout={handleLabelLayout}
188 />
189 </View>
190 </View>
191 ) : null;
192
193 const capsuleContent = (
194 <View className="flex-row items-center">
195 {statusContent}
196 {action !== null ? (
197 <View
198 onLayout={(event) => setActionWidth(settleWidth(event.nativeEvent.layout.width))}
199 className="h-11 flex-row items-center"
200 >
201 <View className="h-4 w-px bg-border" />
202 <Pressable
203 accessibilityRole="button"
204 accessibilityLabel={action.accessibilityLabel}
205 accessibilityState={{ disabled: action.disabled }}
206 disabled={action.disabled}
207 onPress={action.onPress}
208 className={`h-11 items-center justify-center px-3 active:opacity-70 ${action.disabled ? "opacity-50" : ""}`}
209 >
210 <Text className="font-t3-medium text-xs text-foreground" numberOfLines={1}>
211 {action.label}
212 </Text>
213 </Pressable>
214 </View>
215 ) : null}
216 {hasPreview ? (
217 <View
218 className="h-11 flex-row items-center"
219 onLayout={(event) => setPreviewWidth(settleWidth(event.nativeEvent.layout.width))}
220 >
221 {hasStatus ? <View className="h-4 w-px bg-border" /> : null}
222 {props.devicePreview !== null ? (
223 <DevicePreviewButton {...props.devicePreview} compact={previewCompact} />
224 ) : null}
225 {props.browserPreview !== null ? (
226 <BrowserPreviewButton {...props.browserPreview} compact={previewCompact} />
227 ) : null}
228 </View>
229 ) : null}
230 {agents !== null ? (
231 <Pressable
232 accessibilityRole="button"
233 accessibilityLabel={`Open agents, ${agents.accessibilityLabel}`}
234 accessibilityHint="Opens this turn's subagents"
235 onPress={props.onOpenAgents}
236 onLayout={(event) => setAgentsWidth(settleWidth(event.nativeEvent.layout.width))}
237 className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
238 >
239 {hasStatus || hasPreview ? <View className="absolute left-0 h-4 w-px bg-border" /> : null}
240 <SymbolView name="person.2" size={13} tintColorClassName="accent-foreground-muted" />
241 <Text className="font-t3-medium text-xs tabular-nums" numberOfLines={1}>
242 {agents.label}
243 </Text>
244 </Pressable>
245 ) : null}
246 {hasQueue ? (
247 <Pressable
248 accessibilityRole="button"
249 accessibilityLabel={`Open queue, ${props.queuedCount} messages`}
250 accessibilityHint="Opens queued messages to send now, edit, or remove"
251 onPress={props.onOpenQueue}
252 onLayout={(event) => setQueueWidth(settleWidth(event.nativeEvent.layout.width))}
253 style={{ maxWidth: Math.min(overlayWidth, windowWidth) * 0.45 }}
254 className="h-11 flex-row items-center gap-2 px-3 active:opacity-70"
255 >
256 {hasStatus || hasPreview || hasAgents ? (
257 <View className="absolute left-0 h-4 w-px bg-border" />
258 ) : null}
259 <SymbolView name="list.number" size={13} tintColorClassName="accent-foreground-muted" />
260 <Text className="shrink font-t3-medium text-xs tabular-nums" numberOfLines={1}>
261 {props.queuedCount} queued
262 </Text>
263 </Pressable>
264 ) : null}
265 </View>
266 );
267
268 return (
269 <Animated.View
270 pointerEvents="box-none"
271 className="absolute left-0 right-0 z-20 items-center"
272 style={[{ top: -CONTROL_OVERLAY_OFFSET }, liftStyle]}
273 onLayout={(event) => setOverlayWidth(event.nativeEvent.layout.width)}
274 entering={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_ENTERING}
275 exiting={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_EXITING}
276 >
277 {hasCapsule && NATIVE_LIQUID_GLASS_SUPPORTED ? (
278 <UniwindGlassContainer
279 spacing={GLASS_MERGE_SPACING}
280 pointerEvents="box-none"
281 className="flex-row items-center gap-4"
282 >
283 <AnimatedGlassView
284 colorScheme={props.colorScheme}
285 glassEffectStyle="regular"
286 isInteractive={capsuleInteractive}
287 pointerEvents={capsuleInteractive ? "box-none" : "none"}
288 className="h-11 items-center justify-center overflow-hidden rounded-full"
289 style={capsuleStyle}
290 >
291 {capsuleContent}
292 </AnimatedGlassView>
293
294 <AnimatedGlassView
295 colorScheme={props.colorScheme}
296 glassEffectStyle={{ style: props.showScrollToEnd ? "regular" : "none", animate: true }}
297 isInteractive
298 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
299 accessibilityElementsHidden={!props.showScrollToEnd}
300 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
301 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
302 style={arrowTransformStyle}
303 >
304 <Animated.View style={arrowContentStyle}>
305 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
306 </Animated.View>
307 </AnimatedGlassView>
308 </UniwindGlassContainer>
309 ) : hasCapsule ? (
310 <View pointerEvents="box-none" className="flex-row items-center gap-4">
311 <Animated.View
312 pointerEvents={capsuleInteractive ? "box-none" : "none"}
313 className="h-11 items-center justify-center overflow-hidden rounded-full border border-border bg-glass-fallback shadow-md shadow-black/10"
314 style={capsuleStyle}
315 >
316 {capsuleContent}
317 </Animated.View>
318
319 <Animated.View
320 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
321 accessibilityElementsHidden={!props.showScrollToEnd}
322 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
323 style={[arrowTransformStyle, arrowContentStyle]}
324 >
325 <ControlPill
326 accessibilityLabel="Scroll to end"
327 activateOnPressIn
328 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
329 disabled={!props.showScrollToEnd}
330 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
331 onPress={props.onScrollToEnd}
332 />
333 </Animated.View>
334 </View>
335 ) : NATIVE_LIQUID_GLASS_SUPPORTED ? (
336 <UniwindGlassView
337 colorScheme={props.colorScheme}
338 glassEffectStyle="regular"
339 isInteractive
340 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
341 >
342 <ScrollToEndButton onPress={props.onScrollToEnd} />
343 </UniwindGlassView>
344 ) : (
345 <ControlPill
346 accessibilityLabel="Scroll to end"
347 activateOnPressIn
348 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
349 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
350 onPress={props.onScrollToEnd}
351 />
352 )}
353 </Animated.View>
354 );
355}
356
357function CompactingLabel(props: { readonly onLayout: (event: LayoutChangeEvent) => void }) {
358 return (
359 <StatusLabelRow accessibilityLabel="Compacting" className="gap-1.5" onLayout={props.onLayout}>
360 <SymbolView
361 name="arrow.down.right.and.arrow.up.left"
362 size={13}
363 tintColorClassName="foreground"
364 type="monochrome"
365 />
366 <Text className="font-t3-medium text-xs text-foreground">Compacting…</Text>
367 </StatusLabelRow>
368 );
369}
370
371function FloatingStatusLabel(props: {
372 readonly status: FloatingWorkingStatus;
373 readonly onLayout: (event: LayoutChangeEvent) => void;
374}) {
375 if (props.status.kind === "syncing") {
376 return (
377 <StatusLabelRow
378 key="syncing"
379 accessibilityLabel={props.status.label}
380 className="gap-2"
381 onLayout={props.onLayout}
382 >
383 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
384 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
385 {props.status.label}
386 </Text>
387 </StatusLabelRow>
388 );
389 }
390 if (props.status.kind === "compacting") {
391 return <CompactingLabel key="compacting" onLayout={props.onLayout} />;
392 }
393 if (props.status.kind === "child-input") {
394 return (
395 <StatusLabelRow
396 key="child-input"
397 accessibilityLabel={props.status.accessibilityLabel}
398 accessibilityRole="button"
399 className="gap-2"
400 onLayout={props.onLayout}
401 onPress={props.status.onPress}
402 >
403 <SymbolView name="text.bubble" size={13} tintColorClassName="foreground" />
404 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
405 {props.status.label}
406 </Text>
407 </StatusLabelRow>
408 );
409 }
410 if (props.status.kind === "connection") {
411 return (
412 <StatusLabelRow
413 key="connection"
414 accessibilityLabel={props.status.label}
415 accessibilityRole="button"
416 className="gap-2"
417 onLayout={props.onLayout}
418 onPress={props.status.onPress}
419 >
420 {props.status.tone === "reconnecting" ? (
421 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
422 ) : (
423 <View className="h-2 w-2 rounded-full bg-red-500" />
424 )}
425 <Text
426 className="max-w-[260px] shrink font-t3-medium text-xs text-foreground"
427 numberOfLines={1}
428 >
429 {props.status.label}
430 </Text>
431 </StatusLabelRow>
432 );
433 }
434 if (props.status.kind === "lifecycle") {
435 return (
436 <StatusLabelRow
437 key="lifecycle"
438 accessibilityLabel={props.status.label}
439 className="gap-2"
440 onLayout={props.onLayout}
441 >
442 <SymbolView
443 name={props.status.state === "settled" ? "checkmark.circle" : "clock"}
444 size={13}
445 tintColorClassName="foreground"
446 type="monochrome"
447 />
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 === "background") {
455 return (
456 <StatusLabelRow
457 key="background"
458 accessibilityLabel={props.status.accessibilityLabel}
459 accessibilityRole="button"
460 onPress={props.status.onPress}
461 className="gap-2"
462 onLayout={props.onLayout}
463 >
464 <SymbolView
465 name={props.status.waiting ? "person.2" : "terminal"}
466 size={13}
467 tintColorClassName="foreground"
468 type="monochrome"
469 />
470 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
471 {props.status.label}
472 </Text>
473 </StatusLabelRow>
474 );
475 }
476 if (props.status.kind === "stop") {
477 return (
478 <StatusLabelRow
479 key="stop"
480 accessibilityLabel={props.status.label}
481 className="gap-2"
482 onLayout={props.onLayout}
483 >
484 {props.status.unresponsive ? (
485 <SymbolView
486 name="exclamationmark.triangle"
487 size={13}
488 tintColorClassName="accent-warning-foreground"
489 type="monochrome"
490 />
491 ) : (
492 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
493 )}
494 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
495 {props.status.label}
496 </Text>
497 </StatusLabelRow>
498 );
499 }
500 if (props.status.kind === "goal") {
501 return (
502 <StatusLabelRow
503 key="goal"
504 accessibilityLabel={props.status.accessibilityLabel}
505 className="gap-2"
506 onLayout={props.onLayout}
507 >
508 <SymbolView name="target" size={13} tintColorClassName="foreground" type="monochrome" />
509 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
510 {props.status.label}
511 </Text>
512 </StatusLabelRow>
513 );
514 }
515 if (props.status.kind === "preparing") {
516 return (
517 <StatusLabelRow
518 key="preparing"
519 accessibilityLabel={props.status.label}
520 className="gap-1.5"
521 onLayout={props.onLayout}
522 >
523 <SymbolView
524 name="arrow.triangle.branch"
525 size={13}
526 tintColorClassName="foreground"
527 type="monochrome"
528 />
529 <ShimmeringWorkContent
530 className="flex-none"
531 textClassName="font-t3-medium"
532 compact
533 icon="arrow.triangle.branch"
534 iconSubtleColor="transparent"
535 label={props.status.label}
536 showIcon={false}
537 />
538 </StatusLabelRow>
539 );
540 }
541 return (
542 <WorkingDuration key="working" startedAt={props.status.startedAt} onLayout={props.onLayout} />
543 );
544}
545
546function StatusLabelRow(props: {
547 readonly accessibilityLabel?: string;
548 readonly accessibilityRole?: "button";
549 readonly className?: string;
550 readonly children: ReactNode;
551 readonly onLayout: (event: LayoutChangeEvent) => void;
552 readonly onPress?: () => void;
553}) {
554 const rowClassName = `h-11 flex-row items-center px-4 ${props.className ?? ""}`;
555 return (
556 <Animated.View
557 className="absolute max-w-full"
558 entering={LABEL_ENTERING}
559 exiting={LABEL_EXITING}
560 onLayout={props.onLayout}
561 >
562 {props.onPress ? (
563 <Pressable
564 accessibilityLabel={props.accessibilityLabel}
565 accessibilityRole={props.accessibilityRole}
566 className={`${rowClassName} active:opacity-70`}
567 onPress={props.onPress}
568 >
569 {props.children}
570 </Pressable>
571 ) : (
572 <View accessible accessibilityLabel={props.accessibilityLabel} className={rowClassName}>
573 {props.children}
574 </View>
575 )}
576 </Animated.View>
577 );
578}
579
580function WorkingDuration(props: {
581 readonly startedAt: string;
582 readonly onLayout: (event: LayoutChangeEvent) => void;
583}) {
584 return (
585 <StatusLabelRow onLayout={props.onLayout}>
586 <WorkingTimer startedAt={props.startedAt} />
587 </StatusLabelRow>
588 );
589}
590
591function WorkingTimer(props: { readonly startedAt: string }) {
592 const nowMs = useVisibleSecondClock(true);
593 return (
594 <SystemText
595 className="shrink text-xs text-foreground"
596 numberOfLines={1}
597 style={{ fontVariant: ["tabular-nums"], fontWeight: "500" }}
598 >
599 Working {formatWorkingDuration(props.startedAt, nowMs)}
600 </SystemText>
601 );
602}
603
604function formatWorkingDuration(startedAt: string, nowMs: number): string {
605 const startedAtMs = Date.parse(startedAt);
606 if (!Number.isFinite(startedAtMs) || nowMs <= startedAtMs) {
607 return "0s";
608 }
609
610 const totalSeconds = Math.floor((nowMs - startedAtMs) / 1_000);
611 if (totalSeconds < 60) {
612 return `${totalSeconds}s`;
613 }
614 if (totalSeconds >= 3_600) {
615 return formatDuration(totalSeconds * 1_000);
616 }
617
618 const minutes = Math.floor(totalSeconds / 60);
619 const seconds = String(totalSeconds % 60).padStart(2, "0");
620 return `${minutes}m ${seconds}s`;
621}
622
623function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
624 return (
625 <ControlPill
626 accessibilityLabel="Scroll to end"
627 activateOnPressIn
628 className="h-11 w-11 bg-transparent"
629 disabled={props.disabled}
630 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
631 onPress={props.onPress}
632 />
633 );
634}