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

main
tsx 235 lines 8.32 KB
Raw
1 import { useGT } from "gt-react-native";
2 import { Box, Icon, IconButton, RNHostView } from "@expo/ui/jetpack-compose";
3 import { size } from "@expo/ui/jetpack-compose/modifiers";
4 import type { MenuAction } from "@react-native-menu/menu";
5 import {
6 createContext,
7 use,
8 useLayoutEffect,
9 useState,
10 useSyncExternalStore,
11 type ReactNode,
12 } from "react";
13 import { Pressable, View } from "react-native";
14
15 import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu.android";
16 import { AndroidHeaderIconButton } from "./AndroidScreenHeader.android";
17 import { AppText } from "./AppText";
18 import { NativeAppBar } from "./NativeAppBar.android";
19 import { useControlTheme } from "./useControlTheme";
20 import { useToolbarLayout } from "./useToolbarLayout";
21
22 const SEARCH_ICON = require("../../../assets/icons/search.xml");
23 const FILTER_ICON = require("../../../assets/icons/filter.xml");
24 const FILTER_FILLED_ICON = require("../../../assets/icons/filter-filled.xml");
25 // Material TopAppBar: 48dp icon buttons, the last one 4dp from the end.
26 const ACTION_SIZE = 48;
27 const ACTION_END_INSET = 4;
28
29 export interface WorkspaceAppBarProps {
30 readonly title: string;
31 readonly titleContent?: ReactNode;
32 readonly search?: {
33 readonly accessibilityLabel: string;
34 readonly active: boolean;
35 readonly field: ReactNode;
36 readonly onOpen: () => void;
37 readonly onClose: () => void;
38 };
39 /** Stays trailing while searching, so it never moves and still narrows the results. */
40 readonly filter?: {
41 readonly accessibilityLabel: string;
42 readonly customized: boolean;
43 readonly actions: MenuAction[];
44 readonly onSelect: (id: string) => void;
45 };
46 }
47
48 function createWorkspaceAppBarStore() {
49 let owner: object | null = null;
50 let content: WorkspaceAppBarProps | null = null;
51 const listeners = new Set<() => void>();
52 const emit = () => {
53 for (const listener of listeners) listener();
54 };
55 return {
56 subscribe: (listener: () => void) => {
57 listeners.add(listener);
58 return () => {
59 listeners.delete(listener);
60 };
61 },
62 get: () => content,
63 set: (nextOwner: object, next: WorkspaceAppBarProps) => {
64 owner = nextOwner;
65 content = next;
66 emit();
67 },
68 clear: (previousOwner: object) => {
69 if (owner !== previousOwner) return;
70 owner = null;
71 content = null;
72 emit();
73 },
74 };
75 }
76
77 const WorkspaceAppBarStoreContext = createContext<ReturnType<
78 typeof createWorkspaceAppBarStore
79 > | null>(null);
80
81 /**
82 * Hosts one app bar above a set of workspace tab panes. Hidden panes lay out at zero size, and a
83 * Compose bar inside one takes frames to measure again when it returns, so the bar lives here and
84 * the visible tab projects its contents into it.
85 */
86 export function WorkspaceAppBarScope(props: {
87 readonly children: ReactNode;
88 /** Off, panes draw their own bars; the children keep their place so toggling keeps their state. */
89 readonly enabled?: boolean;
90 }) {
91 const [store] = useState(createWorkspaceAppBarStore);
92 const enabled = props.enabled ?? true;
93 return (
94 <WorkspaceAppBarStoreContext value={enabled ? store : null}>
95 {enabled ? <WorkspaceAppBarHost /> : null}
96 {props.children}
97 </WorkspaceAppBarStoreContext>
98 );
99 }
100
101 function WorkspaceAppBarHost() {
102 const store = use(WorkspaceAppBarStoreContext)!;
103 const content = useSyncExternalStore(store.subscribe, store.get, store.get);
104 return <WorkspaceAppBarView {...(content ?? { title: "" })} />;
105 }
106
107 export function useHasWorkspaceAppBar() {
108 return use(WorkspaceAppBarStoreContext) !== null;
109 }
110
111 /** Shows a tab's header in the enclosing scope's bar, or inline outside one. */
112 export function WorkspaceAppBar(props: WorkspaceAppBarProps) {
113 const store = use(WorkspaceAppBarStoreContext);
114 const [owner] = useState(() => ({}));
115 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- projects this tab's header into the out-of-tree workspace bar after every commit
116 useLayoutEffect(() => {
117 store?.set(owner, props);
118 });
119 // Hidden panes destroy their effects, so the tab leaving the screen releases the bar.
120 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- releases the workspace bar when this tab hides or unmounts
121 useLayoutEffect(() => (store ? () => store.clear(owner) : undefined), [store, owner]);
122 return store ? null : <WorkspaceAppBarView {...props} />;
123 }
124
125 /**
126 * Every tab draws the same bar, so a switch only changes its contents. The actions are Compose nodes
127 * and the title host keeps one height: React Native content hosted in Compose lags Compose's layout
128 * by a frame whenever its size changes, which showed as the filter jumping between tabs.
129 */
130 function WorkspaceAppBarView(props: WorkspaceAppBarProps) {
131 const gt = useGT();
132 const { search, filter } = props;
133 const searching = search?.active === true;
134 const { colors } = useControlTheme();
135 const tint = colors["--color-header-foreground"];
136 const { height, paddingTop, paddingBottom } = useToolbarLayout();
137 const filterMenu = filter ? (
138 <AndroidAnchoredMenu
139 actions={filter.actions}
140 onPressAction={({ nativeEvent }) => filter.onSelect(nativeEvent.event)}
141 >
142 {(open) =>
143 searching ? (
144 <AndroidHeaderIconButton
145 accessibilityLabel={filter.accessibilityLabel}
146 icon={
147 filter.customized
148 ? "line.3.horizontal.decrease.circle.fill"
149 : "line.3.horizontal.decrease.circle"
150 }
151 onPress={open}
152 />
153 ) : (
154 // The bar draws the icon; this takes the touch so the menu can anchor to the slot.
155 <Pressable
156 accessibilityLabel={filter.accessibilityLabel}
157 accessibilityRole="button"
158 android_ripple={{ borderless: true, radius: 20 }}
159 onPress={open}
160 style={{ width: ACTION_SIZE, height: ACTION_SIZE }}
161 />
162 )
163 }
164 </AndroidAnchoredMenu>
165 ) : null;
166 return (
167 <View className="bg-header" style={{ paddingTop, paddingBottom }}>
168 <View
169 pointerEvents={searching ? "none" : "auto"}
170 accessibilityElementsHidden={searching}
171 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
172 style={{ opacity: searching ? 0 : 1 }}
173 >
174 <NativeAppBar
175 title={
176 <RNHostView matchContents={{ vertical: true }}>
177 <View collapsable={false} className="justify-center" style={{ height: ACTION_SIZE }}>
178 {props.titleContent ?? (
179 <AppText textRole="title" numberOfLines={1} className="text-header-foreground">
180 {props.title}
181 </AppText>
182 )}
183 </View>
184 </RNHostView>
185 }
186 // Both slots always exist: Expo UI applies a child insert or removal a frame before the
187 // other children's new props, so changing the slot count drew the wrong glyph mid-switch.
188 nativeActions={
189 <>
190 <IconButton enabled={search !== undefined} onClick={search?.onOpen}>
191 <Icon
192 source={SEARCH_ICON}
193 size={24}
194 tint={search ? tint : "transparent"}
195 contentDescription={search?.accessibilityLabel}
196 />
197 </IconButton>
198 <Box contentAlignment="center" modifiers={[size(ACTION_SIZE, ACTION_SIZE)]}>
199 <Icon
200 source={filter?.customized ? FILTER_FILLED_ICON : FILTER_ICON}
201 size={24}
202 tint={filter ? tint : "transparent"}
203 />
204 </Box>
205 </>
206 }
207 />
208 {filter && !searching ? (
209 <View
210 className="absolute"
211 style={{ right: ACTION_END_INSET, top: (height - ACTION_SIZE) / 2 }}
212 >
213 {filterMenu}
214 </View>
215 ) : null}
216 </View>
217 {searching ? (
218 <View
219 className="absolute inset-0 bg-header"
220 style={{ paddingTop, paddingBottom, paddingHorizontal: ACTION_END_INSET }}
221 >
222 <View className="flex-1 flex-row items-center gap-1">
223 <AndroidHeaderIconButton
224 accessibilityLabel={gt("Close search")}
225 icon="arrow.left"
226 onPress={search.onClose}
227 />
228 {search.field}
229 {filterMenu}
230 </View>
231 </View>
232 ) : null}
233 </View>
234 );
235 }