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

floating-working-control.tsx

608 lines21.9 KB

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