The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 76 lines 2.95 KB
Raw
1 import { T } from "gt-react-native";
2 import { useId, useLayoutEffect, useRef, useState, type ReactNode } from "react";
3 import {
4 Pressable,
5 View,
6 type ColorValue,
7 type LayoutChangeEvent,
8 type ViewInstance,
9 } from "react-native";
10 import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
11 import { AppText as Text } from "../../components/ui/AppText";
12
13 const FOLDED_HEIGHT = 176;
14
15 // Fold only when opening at least doubles what is on screen, matching web's useTallEnoughToFold.
16 const isTallEnoughToFold = (height: number) => height >= FOLDED_HEIGHT * 2;
17
18 export function CollapsibleUserMessageBody(props: {
19 readonly collapsible: boolean;
20 readonly expanded: boolean;
21 readonly onToggle: () => void;
22 readonly bubbleColor: ColorValue;
23 readonly children: ReactNode;
24 }) {
25 const gradientId = `user-message-fade-${useId().replaceAll(":", "")}`;
26 const contentRef = useRef<ViewInstance>(null);
27 const [tallEnough, setTallEnough] = useState(false);
28 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the fold depends on the rendered height; measuring before paint avoids one unfolded frame
29 useLayoutEffect(() => {
30 const height = contentRef.current?.getBoundingClientRect().height;
31 if (height !== undefined) setTallEnough(isTallEnoughToFold(height));
32 }, []);
33 const canCollapse = props.collapsible && tallEnough;
34 const collapsed = canCollapse && !props.expanded;
35
36 return (
37 <View>
38 <View style={collapsed ? { maxHeight: FOLDED_HEIGHT, overflow: "hidden" } : undefined}>
39 <View
40 ref={contentRef}
41 onLayout={(event: LayoutChangeEvent) => {
42 // Folded, Yoga reports the clipped height, which would read as short and unfold it in a loop.
43 if (!collapsed) setTallEnough(isTallEnoughToFold(event.nativeEvent.layout.height));
44 }}
45 >
46 {props.children}
47 </View>
48 {collapsed ? (
49 <View pointerEvents="none" className="absolute inset-x-0 bottom-0 h-7">
50 <Svg width="100%" height="100%">
51 <Defs>
52 <LinearGradient id={gradientId} x1="0%" x2="0%" y1="0%" y2="100%">
53 <Stop offset={0} stopColor={props.bubbleColor} stopOpacity={0} />
54 <Stop offset={1} stopColor={props.bubbleColor} stopOpacity={1} />
55 </LinearGradient>
56 </Defs>
57 <Rect width="100%" height="100%" fill={`url(#${gradientId})`} />
58 </Svg>
59 </View>
60 ) : null}
61 </View>
62 {canCollapse ? (
63 <Pressable
64 accessibilityRole="button"
65 accessibilityState={{ expanded: props.expanded }}
66 onPress={props.onToggle}
67 className="mt-1.5 min-h-11 items-end justify-center active:opacity-70"
68 >
69 <Text className="font-t3-medium text-xs text-user-bubble-foreground-muted">
70 {props.expanded ? <T>Show less</T> : <T>Show full message</T>}
71 </Text>
72 </Pressable>
73 ) : null}
74 </View>
75 );
76 }