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

floating-working-control.tsx

574 lines21.7 KB

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