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

main
tsx 201 lines 6.46 KB
Raw
1 import { useGT } from "gt-react-native";
2 import { Pressable, View } from "react-native";
3
4 import { NativeStackScreenOptions } from "../../native/StackHeader";
5 import { AppActivityIndicator } from "./AppActivityIndicator";
6 import { AppText } from "./AppText";
7 import { SymbolView } from "./AppSymbol";
8 import { IconButton } from "./IconButton";
9 import { Toolbar } from "./Toolbar";
10 import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios";
11 import { useControlTheme } from "./useControlTheme";
12 import type { SheetCloseButtonProps, SheetHeaderProps } from "./SheetHeader.types";
13
14 export function SheetCloseButton(props: SheetCloseButtonProps) {
15 const gt = useGT();
16 return (
17 <IconButton
18 icon="xmark"
19 accessibilityLabel={props.accessibilityLabel ?? (props.onCancel ? gt("Cancel") : gt("Close"))}
20 disabled={props.disabled}
21 onPress={props.onCancel ?? props.onClose}
22 />
23 );
24 }
25
26 export function SheetHeader(props: SheetHeaderProps) {
27 return (
28 <>
29 <SheetDismissGuard dismissDisabled={props.dismissDisabled} />
30 {props.presentation !== "inline" ? (
31 <NativeSheetHeader {...props} />
32 ) : (
33 <InlineSheetHeader {...props} />
34 )}
35 </>
36 );
37 }
38
39 /** A sheet that can't be closed with its button can't be swiped away either. */
40 function SheetDismissGuard(props: { readonly dismissDisabled: boolean | undefined }) {
41 return props.dismissDisabled === undefined ? null : (
42 <NativeStackScreenOptions options={{ gestureEnabled: !props.dismissDisabled }} />
43 );
44 }
45
46 function NativeSheetHeader(props: SheetHeaderProps) {
47 const gt = useGT();
48 const { colors } = useControlTheme();
49 const onDismiss = props.onCancel ?? props.onClose;
50 const dismissLabel = props.accessibilityLabel ?? (props.onCancel ? gt("Cancel") : gt("Close"));
51 const confirmation = props.confirm;
52
53 // Toolbar children are parsed as native items, so keep the actual Button
54 // elements here instead of hiding them behind another component.
55 return (
56 <>
57 <NativeStackScreenOptions
58 options={{
59 title: props.title,
60 headerBackVisible: false,
61 unstable_headerSubtitle: props.subtitle,
62 }}
63 />
64 <Toolbar placement="left">
65 <Toolbar.Button
66 icon="xmark"
67 accessibilityLabel={dismissLabel}
68 onPress={onDismiss}
69 disabled={props.dismissDisabled}
70 />
71 </Toolbar>
72 {confirmation || props.trailing || props.actions?.length || props.menus?.length ? (
73 <Toolbar placement="right">
74 {props.actions?.map((action) => (
75 <Toolbar.Button
76 key={action.accessibilityLabel}
77 icon={typeof action.icon === "string" ? action.icon : action.icon.ios}
78 accessibilityLabel={action.accessibilityLabel}
79 disabled={action.disabled || action.loading}
80 tintColor={action.tintColor}
81 onPress={action.onPress}
82 />
83 ))}
84 {props.menus?.map(createNativeHeaderMenu)}
85 {props.trailing ? (
86 <Toolbar.Custom>
87 <View>{props.trailing}</View>
88 </Toolbar.Custom>
89 ) : null}
90 {confirmation?.loading ? (
91 <Toolbar.Custom>
92 <View
93 className="size-11 items-center justify-center"
94 accessible
95 accessibilityLabel={confirmation.label}
96 accessibilityState={{ busy: true }}
97 >
98 <AppActivityIndicator colorClassName="accent-prominent-action" />
99 </View>
100 </Toolbar.Custom>
101 ) : confirmation ? (
102 <Toolbar.Button
103 icon="checkmark"
104 variant="prominent"
105 visibilityPriority="high"
106 tintColor={colors["--color-prominent-action"]}
107 accessibilityLabel={confirmation.label}
108 disabled={confirmation.disabled}
109 onPress={confirmation.onPress}
110 />
111 ) : null}
112 </Toolbar>
113 ) : null}
114 </>
115 );
116 }
117
118 function InlineConfirmation({
119 confirmation,
120 }: {
121 readonly confirmation: NonNullable<SheetHeaderProps["confirm"]>;
122 }) {
123 const confirmDisabled = confirmation.disabled || confirmation.loading;
124 return (
125 <Pressable
126 accessibilityRole="button"
127 accessibilityLabel={confirmation.label}
128 accessibilityState={{
129 disabled: Boolean(confirmDisabled),
130 busy: Boolean(confirmation.loading),
131 }}
132 disabled={confirmDisabled}
133 onPress={confirmation.onPress}
134 className={
135 confirmDisabled
136 ? "size-11 items-center justify-center rounded-full bg-subtle-strong"
137 : "size-11 items-center justify-center rounded-full bg-prominent-action"
138 }
139 >
140 {confirmation.loading ? (
141 <AppActivityIndicator colorClassName="accent-icon-subtle" />
142 ) : (
143 <SymbolView
144 name="checkmark"
145 size={20}
146 tintColorClassName={
147 confirmDisabled ? "accent-icon-subtle" : "accent-prominent-action-foreground"
148 }
149 />
150 )}
151 </Pressable>
152 );
153 }
154
155 function InlineSheetHeader(props: SheetHeaderProps) {
156 const close = (
157 <SheetCloseButton
158 {...(props.onCancel ? { onCancel: props.onCancel } : { onClose: props.onClose })}
159 accessibilityLabel={props.accessibilityLabel}
160 disabled={props.dismissDisabled}
161 />
162 );
163 const confirm = props.confirm ? <InlineConfirmation confirmation={props.confirm} /> : null;
164 const trailing =
165 props.trailing || confirm || props.actions?.length ? (
166 <View className="flex-row items-center gap-2">
167 {props.actions?.map((action) => (
168 <IconButton
169 key={action.accessibilityLabel}
170 icon={action.icon}
171 accessibilityLabel={action.accessibilityLabel}
172 disabled={action.disabled || action.loading}
173 onPress={action.onPress}
174 />
175 ))}
176 {props.trailing}
177 {confirm}
178 </View>
179 ) : undefined;
180
181 return (
182 <View className="flex-row items-center gap-3 px-4 pt-3 pb-3">
183 {close}
184 <View className="min-w-0 flex-1 gap-1">
185 <AppText numberOfLines={1} className="text-base font-t3-bold text-foreground">
186 {props.title}
187 </AppText>
188 {props.subtitle ? (
189 <AppText
190 numberOfLines={1}
191 ellipsizeMode="middle"
192 className="text-xs text-foreground-muted"
193 >
194 {props.subtitle}
195 </AppText>
196 ) : null}
197 </View>
198 {trailing}
199 </View>
200 );
201 }