@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 299 lines 10 KB
Raw
1 import { useGT } from "gt-react-native";
2 import { type RefObject, use, useRef, useState } from "react";
3 import { Keyboard, type TextInputInstance, View } from "react-native";
4 import { useHardwareBack } from "../../lib/backHandler";
5 import { useToolbarLayout } from "./useToolbarLayout";
6 import { BottomSheetContext } from "./BottomSheetContext";
7 import { RoutedFormSheetContext } from "./RoutedFormSheetContext";
8 import { NativeStackScreenOptions } from "../../native/StackHeader";
9 import { useSidebarToggle } from "../../features/layout/useSidebarToggle";
10 import { useAdaptiveWorkspaceLayout } from "../../features/layout/adaptive-workspace-context";
11 import { InspectorCloseContext } from "../../features/layout/inspector-bar-store";
12 import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
13 import { AndroidHeaderIconButton, AndroidScreenHeader } from "./AndroidScreenHeader.android";
14 import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
15 import { Menu } from "./Menu";
16 import { Button } from "./Button";
17 import { SearchField } from "./SearchField";
18 import { findAppBarMenuItem, menuActionsFromAppBarItems } from "./menuActionItems";
19 import type { AppBarMenuItem, AppBarProps, AppBarSearch } from "./AppBar.types";
20 import { useHasWorkspaceAppBar, WorkspaceAppBar } from "./WorkspaceAppBar.android";
21
22 function selectMenuItem(items: ReadonlyArray<AppBarMenuItem>, id: string) {
23 const action = findAppBarMenuItem(items, id);
24 if (action && !action.disabled) action.onPress();
25 }
26
27 interface SearchState {
28 readonly searching: boolean;
29 readonly open: () => void;
30 readonly close: () => void;
31 readonly inputRef: RefObject<TextInputInstance | null>;
32 }
33
34 export function AppBar(barProps: AppBarProps) {
35 const { layout } = useAdaptiveWorkspaceLayout();
36 const props = {
37 ...barProps,
38 onBack:
39 barProps.onBack ??
40 (layout.usesSplitView ? barProps.backInSplitView?.onPress : barProps.onBackToList),
41 };
42 return props.search ? (
43 <SearchableAppBar {...props} search={props.search} />
44 ) : (
45 <AppBarView {...props} />
46 );
47 }
48
49 // Only a bar with search registers for hardware back: its focus check needs a navigation
50 // context, and sheet headers such as Arrange threads render outside one.
51 function SearchableAppBar(props: AppBarProps & { readonly search: AppBarSearch }) {
52 const { search } = props;
53 const [searchOpen, setSearchOpen] = useState(false);
54 const inputRef = useRef<TextInputInstance>(null);
55 const inline = search.mode === "inline";
56 const controlled = search.active !== undefined;
57 const searching =
58 inline || (controlled ? search.active === true : searchOpen || search.value.length > 0);
59 const open = () => {
60 if (!controlled) setSearchOpen(true);
61 search.onOpen?.();
62 inputRef.current?.focus();
63 };
64 const close = () => {
65 if (!controlled) {
66 search.onChangeText("");
67 setSearchOpen(false);
68 Keyboard.dismiss();
69 }
70 search.onClose?.();
71 };
72 useHardwareBack(searching && !inline && !controlled, () => {
73 close();
74 return true;
75 });
76 return <AppBarView {...props} searchState={{ searching, open, close, inputRef }} />;
77 }
78
79 function AppBarView(props: AppBarProps & { readonly searchState?: SearchState }) {
80 const gt = useGT();
81 const { search, searchState } = props;
82 const inFormSheet = use(RoutedFormSheetContext);
83 const { paddingTop, paddingBottom } = useToolbarLayout(props.sheet || inFormSheet);
84 const { themeVariables } = useAppearancePreferences();
85 const inWorkspace = useHasWorkspaceAppBar();
86 const sidebarToggle = useSidebarToggle();
87 const closeInspector = use(InspectorCloseContext);
88 // A bottom sheet owns the pull at its top, so refresh needs a button there.
89 const inBottomSheet = use(BottomSheetContext);
90 const inline = search?.mode === "inline";
91 const searching = searchState?.searching === true;
92 const actions = props.refresh
93 ? [
94 ...(props.actions ?? []),
95 { ...props.refresh, icon: "arrow.clockwise" as const, overflowOnly: !inBottomSheet },
96 ]
97 : props.actions;
98 const openSearch = () => searchState?.open();
99 const closeSearch = () => searchState?.close();
100
101 const searchLabel = search?.accessibilityLabel ?? search?.placeholder ?? "";
102 const searchField =
103 search && searchState ? (
104 <SearchField
105 inputRef={searchState.inputRef}
106 accessibilityLabel={searchLabel}
107 clearAccessibilityLabel={search.clearAccessibilityLabel ?? gt("Clear search")}
108 placeholder={
109 search.compactToolbar
110 ? (search.compactPlaceholder ?? search.placeholder)
111 : search.placeholder
112 }
113 value={search.value}
114 onChangeText={search.onChangeText}
115 autoFocus={!inline}
116 />
117 ) : null;
118
119 if (inWorkspace) {
120 const menu = props.menus?.[0];
121 const icon = typeof menu?.icon === "string" ? menu.icon : menu?.icon.android;
122 return (
123 <WorkspaceAppBar
124 title={props.title}
125 titleContent={props.titleContent}
126 search={
127 search
128 ? {
129 accessibilityLabel: searchLabel,
130 active: searching,
131 field: searchField,
132 onOpen: openSearch,
133 onClose: closeSearch,
134 }
135 : undefined
136 }
137 filter={
138 menu
139 ? {
140 accessibilityLabel: menu.title,
141 customized: icon?.endsWith(".fill") === true,
142 actions: menuActionsFromAppBarItems(menu.items),
143 onSelect: (id) => selectMenuItem(menu.items, id),
144 }
145 : undefined
146 }
147 />
148 );
149 }
150
151 const menuView = props.menus?.map((menu) => (
152 <Menu
153 key={menu.title}
154 actions={menuActionsFromAppBarItems(menu.items)}
155 isAnchoredToRight
156 title={menu.status ?? menu.title}
157 onPressAction={({ nativeEvent }) => selectMenuItem(menu.items, nativeEvent.event)}
158 >
159 <ScreenHeaderButton accessibilityLabel={menu.title} icon={menu.icon} />
160 </Menu>
161 ));
162 const confirmButton = props.confirm ? (
163 <View className="self-center">
164 <Button
165 label={props.confirm.label}
166 tone="text"
167 loading={props.confirm.loading}
168 disabled={props.confirm.disabled}
169 onPress={props.confirm.onPress}
170 />
171 </View>
172 ) : null;
173 const options =
174 props.embedded || props.sheet ? null : (
175 <NativeStackScreenOptions
176 options={{
177 ...(props.matchSearchSurface
178 ? { contentStyle: { backgroundColor: themeVariables["--color-header"] } }
179 : undefined),
180 headerShown: false,
181 title: props.title,
182 }}
183 optionsVersion={props.optionsVersion}
184 />
185 );
186 const header = (
187 <AndroidScreenHeader
188 title={props.title}
189 titleContent={props.titleContent}
190 subtitle={props.subtitle}
191 onBack={props.onClose ? undefined : props.onBack}
192 leading={
193 (props.onClose ? (
194 <AndroidHeaderIconButton
195 accessibilityLabel={props.closeAccessibilityLabel ?? gt("Close")}
196 icon="xmark"
197 onPress={props.onClose}
198 />
199 ) : undefined) ??
200 props.leading ??
201 // Only a real button: any leading content takes the bar's navigation slot.
202 (sidebarToggle && !props.sheet ? (
203 <AndroidHeaderIconButton
204 accessibilityLabel={sidebarToggle.accessibilityLabel}
205 icon={sidebarToggle.icon}
206 onPress={sidebarToggle.onPress}
207 />
208 ) : undefined)
209 }
210 embedded={props.sheet}
211 hideBottomBorder={props.hideBottomBorder}
212 actions={
213 search && !inline
214 ? [
215 {
216 accessibilityLabel: searchLabel,
217 icon: "magnifyingglass",
218 onPress: openSearch,
219 },
220 ...(actions ?? []),
221 ]
222 : actions
223 }
224 trailing={
225 menuView?.length || props.trailing || props.confirm || closeInspector ? (
226 <>
227 {menuView}
228 {props.trailing}
229 {confirmButton}
230 {closeInspector ? (
231 <AndroidHeaderIconButton
232 accessibilityLabel={gt("Close inspector")}
233 icon="xmark"
234 onPress={closeInspector}
235 />
236 ) : null}
237 </>
238 ) : undefined
239 }
240 />
241 );
242 if (!search) {
243 return (
244 <>
245 {options}
246 {header}
247 </>
248 );
249 }
250 return (
251 <>
252 {options}
253 <View>
254 <View
255 pointerEvents={searching ? "none" : "auto"}
256 accessibilityElementsHidden={searching}
257 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
258 style={{ opacity: searching ? 0 : 1 }}
259 >
260 {header}
261 </View>
262 {searching ? (
263 <View
264 className="absolute inset-0 bg-header"
265 // Match Material TopAppBar's 4dp icon inset so buttons stay where the bar drew them.
266 style={{ paddingTop, paddingBottom, paddingHorizontal: 4 }}
267 >
268 <View className="flex-1 flex-row items-center gap-1">
269 {inline ? (
270 props.onClose ? (
271 <ScreenHeaderButton
272 accessibilityLabel={props.closeAccessibilityLabel ?? gt("Close")}
273 icon="xmark"
274 onPress={props.onClose}
275 />
276 ) : props.onBack ? (
277 <ScreenHeaderButton
278 accessibilityLabel={gt("Navigate up")}
279 icon="arrow.left"
280 onPress={props.onBack}
281 />
282 ) : null
283 ) : (
284 <ScreenHeaderButton
285 accessibilityLabel={search.closeAccessibilityLabel ?? gt("Close search")}
286 icon="arrow.left"
287 onPress={closeSearch}
288 />
289 )}
290 {searchField}
291 {inline ? menuView : null}
292 {inline ? confirmButton : null}
293 </View>
294 </View>
295 ) : null}
296 </View>
297 </>
298 );
299 }