The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { NativeThemeHost } from "./NativeThemeHost.android"; |
| 2 | import { |
| 3 | AlertDialog, |
| 4 | Button, |
| 5 | OutlinedButton, |
| 6 | Icon, |
| 7 | FlowRow, |
| 8 | Column, |
| 9 | OutlinedTextField, |
| 10 | Text, |
| 11 | useNativeState, |
| 12 | } from "@expo/ui/jetpack-compose"; |
| 13 | import { verticalScroll } from "@expo/ui/jetpack-compose/modifiers"; |
| 14 | |
| 15 | import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider"; |
| 16 | import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole"; |
| 17 | |
| 18 | import { msg, useGT } from "gt-react-native"; |
| 19 | import { useSyncExternalStore } from "react"; |
| 20 | import { m } from "../../i18n/messages"; |
| 21 | import { themeColorWithContrast } from "../../lib/mobileTheme"; |
| 22 | import type { Alert, AlertButton } from "react-native"; |
| 23 | import type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.types"; |
| 24 | |
| 25 | export type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.types"; |
| 26 | |
| 27 | type AlertRequest = { |
| 28 | readonly title: Parameters<typeof Alert.alert>[0]; |
| 29 | readonly message: Parameters<typeof Alert.alert>[1]; |
| 30 | readonly buttons: Parameters<typeof Alert.alert>[2]; |
| 31 | readonly options: Parameters<typeof Alert.alert>[3]; |
| 32 | }; |
| 33 | |
| 34 | type DialogRequest = |
| 35 | | { readonly kind: "alert"; readonly request: AlertRequest } |
| 36 | | { readonly kind: "confirm"; readonly request: ConfirmDialogRequest } |
| 37 | | { readonly kind: "text-input"; readonly request: TextInputDialogRequest }; |
| 38 | |
| 39 | type PresentedDialog = DialogRequest & { readonly id: number }; |
| 40 | |
| 41 | // App services raise dialogs before the root mounts, so the request lives in |
| 42 | // the module and the Host renders whatever is current. One dialog shows at a |
| 43 | // time: a new request replaces the presented one. |
| 44 | let presented: PresentedDialog | null = null; |
| 45 | let nextPresentationId = 0; |
| 46 | const listeners = new Set<() => void>(); |
| 47 | |
| 48 | function setPresented(next: PresentedDialog | null) { |
| 49 | presented = next; |
| 50 | for (const listener of listeners) listener(); |
| 51 | } |
| 52 | |
| 53 | function submitRequest(dialog: DialogRequest) { |
| 54 | const replaced = presented; |
| 55 | setPresented({ ...dialog, id: ++nextPresentationId }); |
| 56 | // A replaced dialog closed without an answer, which its owner hears as a dismissal. |
| 57 | if (replaced) closeUnanswered(replaced); |
| 58 | } |
| 59 | |
| 60 | function closeUnanswered(dialog: DialogRequest) { |
| 61 | if (dialog.kind === "alert") dialog.request.options?.onDismiss?.(); |
| 62 | else dialog.request.onCancel?.(); |
| 63 | } |
| 64 | |
| 65 | /** Closes the dialog only if it is still the one the action came from. */ |
| 66 | function dismiss(id: number) { |
| 67 | if (presented?.id !== id) return false; |
| 68 | setPresented(null); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | function subscribe(listener: () => void) { |
| 73 | listeners.add(listener); |
| 74 | return () => { |
| 75 | listeners.delete(listener); |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | function getPresented() { |
| 80 | return presented; |
| 81 | } |
| 82 | |
| 83 | export function alert(...[title, message, buttons, options]: Parameters<typeof Alert.alert>): void { |
| 84 | const actions = buttons?.length ? buttons.slice(0, 3) : [{ text: m(msg("OK")) }]; |
| 85 | const normalized = actions.map((action, index) => |
| 86 | index === actions.length - 1 ? { ...action, text: action.text || m(msg("OK")) } : action, |
| 87 | ); |
| 88 | submitRequest({ |
| 89 | kind: "alert", |
| 90 | request: { title, message, buttons: normalized, options }, |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | export function confirm(request: ConfirmDialogRequest): void { |
| 95 | submitRequest({ kind: "confirm", request }); |
| 96 | } |
| 97 | |
| 98 | export function prompt(request: TextInputDialogRequest): void { |
| 99 | submitRequest({ kind: "text-input", request }); |
| 100 | } |
| 101 | |
| 102 | export function Host() { |
| 103 | const gt = useGT(); |
| 104 | const presented = useSyncExternalStore(subscribe, getPresented, getPresented); |
| 105 | |
| 106 | const handleCancel = () => { |
| 107 | if (!presented || !dismiss(presented.id)) return; |
| 108 | closeUnanswered(presented); |
| 109 | }; |
| 110 | |
| 111 | const handleConfirm = (nativeInputValue?: string) => { |
| 112 | if (!presented || !dismiss(presented.id)) return; |
| 113 | if (presented.kind === "confirm") { |
| 114 | presented.request.onConfirm(); |
| 115 | } else if (presented.kind === "text-input") { |
| 116 | presented.request.onConfirm(nativeInputValue ?? presented.request.initialValue); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | if (presented?.kind === "alert") { |
| 121 | return ( |
| 122 | <DialogPresentation |
| 123 | key={presented.id} |
| 124 | request={{ |
| 125 | title: presented.request.title ?? "", |
| 126 | message: presented.request.message ?? undefined, |
| 127 | confirmText: "", |
| 128 | }} |
| 129 | alertActions={presented.request.buttons ?? [{ text: gt("OK") }]} |
| 130 | dismissable={presented.request.options?.cancelable ?? false} |
| 131 | onAlertAction={(action) => { |
| 132 | dismiss(presented.id); |
| 133 | action.onPress?.(); |
| 134 | }} |
| 135 | onCancel={handleCancel} |
| 136 | onConfirm={handleConfirm} |
| 137 | /> |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | return presented ? ( |
| 142 | <DialogPresentation |
| 143 | key={presented.id} |
| 144 | request={presented.request} |
| 145 | inputInitialValue={ |
| 146 | presented.kind === "text-input" ? presented.request.initialValue : undefined |
| 147 | } |
| 148 | inputPlaceholder={presented.kind === "text-input" ? presented.request.placeholder : undefined} |
| 149 | onCancel={handleCancel} |
| 150 | onConfirm={handleConfirm} |
| 151 | /> |
| 152 | ) : null; |
| 153 | } |
| 154 | |
| 155 | function DialogPresentation(props: DialogPresentationProps) { |
| 156 | const gt = useGT(); |
| 157 | const { themeVariables: colors } = useAppearancePreferences(); |
| 158 | const titleTypography = useScaledTextRole("title"); |
| 159 | const bodyTypography = useScaledTextRole("footnote"); |
| 160 | const inputTypography = useScaledTextRole("body"); |
| 161 | const inputState = useNativeState(props.inputInitialValue ?? ""); |
| 162 | const inputSelection = useNativeState({ start: 0, end: props.inputInitialValue?.length ?? 0 }); |
| 163 | const confirm = () => { |
| 164 | const value = props.inputInitialValue === undefined ? undefined : inputState.get(); |
| 165 | props.onConfirm(value); |
| 166 | }; |
| 167 | const actions = props.alertActions ?? [ |
| 168 | { text: props.request.cancelText ?? gt("Cancel"), onPress: props.onCancel }, |
| 169 | { |
| 170 | text: props.request.confirmText, |
| 171 | style: props.request.destructive ? ("destructive" as const) : ("default" as const), |
| 172 | onPress: confirm, |
| 173 | }, |
| 174 | ]; |
| 175 | const destructive = actions.some((action) => action.style === "destructive"); |
| 176 | // Error text colors can be pastel in dark themes. Filled actions need a white-label contrast pair. |
| 177 | const destructiveFill = themeColorWithContrast(colors["--color-danger-foreground"], "#ffffff"); |
| 178 | const renderAction = (action: AlertButton, index: number) => { |
| 179 | const isDestructive = action.style === "destructive"; |
| 180 | const isPrimary = isDestructive || index === actions.length - 1; |
| 181 | const Component = isPrimary ? Button : OutlinedButton; |
| 182 | return ( |
| 183 | <Component |
| 184 | key={index} |
| 185 | onClick={() => (props.alertActions ? props.onAlertAction?.(action) : action.onPress?.())} |
| 186 | colors={{ |
| 187 | containerColor: isDestructive |
| 188 | ? destructiveFill |
| 189 | : isPrimary |
| 190 | ? colors["--color-prominent-action"] |
| 191 | : "#00000000", |
| 192 | contentColor: isDestructive |
| 193 | ? "#ffffff" |
| 194 | : isPrimary |
| 195 | ? colors["--color-prominent-action-foreground"] |
| 196 | : colors["--color-foreground"], |
| 197 | }} |
| 198 | > |
| 199 | <Text style={bodyTypography}>{action.text ?? ""}</Text> |
| 200 | </Component> |
| 201 | ); |
| 202 | }; |
| 203 | return ( |
| 204 | <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ height: 0, width: 0 }}> |
| 205 | <AlertDialog |
| 206 | onDismissRequest={props.onCancel} |
| 207 | properties={{ |
| 208 | dismissOnBackPress: props.dismissable ?? true, |
| 209 | dismissOnClickOutside: props.dismissable ?? true, |
| 210 | }} |
| 211 | tonalElevation={0} |
| 212 | colors={{ |
| 213 | containerColor: colors["--color-card"], |
| 214 | titleContentColor: colors["--color-foreground"], |
| 215 | textContentColor: colors["--color-foreground-secondary"], |
| 216 | }} |
| 217 | > |
| 218 | <AlertDialog.Title> |
| 219 | <Column verticalArrangement={{ spacedBy: 16 }}> |
| 220 | {destructive ? ( |
| 221 | <Icon |
| 222 | source={require("../../../assets/icons/delete.xml")} |
| 223 | tint={colors["--color-danger-foreground"]} |
| 224 | size={24} |
| 225 | /> |
| 226 | ) : null} |
| 227 | <Text style={titleTypography}>{props.request.title}</Text> |
| 228 | </Column> |
| 229 | </AlertDialog.Title> |
| 230 | {props.inputInitialValue !== undefined ? ( |
| 231 | <AlertDialog.Text> |
| 232 | <Column verticalArrangement={{ spacedBy: 16 }}> |
| 233 | {props.request.message ? ( |
| 234 | <Text style={bodyTypography}>{props.request.message}</Text> |
| 235 | ) : null} |
| 236 | <OutlinedTextField |
| 237 | autoFocus |
| 238 | singleLine |
| 239 | value={inputState} |
| 240 | selection={inputSelection} |
| 241 | textStyle={inputTypography} |
| 242 | keyboardOptions={{ imeAction: "done" }} |
| 243 | keyboardActions={{ |
| 244 | onDone: confirm, |
| 245 | }} |
| 246 | colors={{ |
| 247 | focusedTextColor: colors["--color-foreground"], |
| 248 | unfocusedTextColor: colors["--color-foreground"], |
| 249 | focusedIndicatorColor: colors["--color-focus"], |
| 250 | unfocusedIndicatorColor: colors["--color-border"], |
| 251 | cursorColor: colors["--color-focus"], |
| 252 | }} |
| 253 | > |
| 254 | <OutlinedTextField.Label> |
| 255 | <Text style={bodyTypography}>{props.request.title}</Text> |
| 256 | </OutlinedTextField.Label> |
| 257 | {props.inputPlaceholder ? ( |
| 258 | <OutlinedTextField.Placeholder> |
| 259 | <Text style={bodyTypography}>{props.inputPlaceholder}</Text> |
| 260 | </OutlinedTextField.Placeholder> |
| 261 | ) : null} |
| 262 | </OutlinedTextField> |
| 263 | </Column> |
| 264 | </AlertDialog.Text> |
| 265 | ) : props.request.message ? ( |
| 266 | <AlertDialog.Text> |
| 267 | <Text modifiers={[verticalScroll()]} style={bodyTypography}> |
| 268 | {props.request.message} |
| 269 | </Text> |
| 270 | </AlertDialog.Text> |
| 271 | ) : null} |
| 272 | {actions.length > 1 ? ( |
| 273 | <AlertDialog.DismissButton> |
| 274 | <FlowRow>{actions.slice(0, -1).map(renderAction)}</FlowRow> |
| 275 | </AlertDialog.DismissButton> |
| 276 | ) : null} |
| 277 | {actions.length > 0 ? ( |
| 278 | <AlertDialog.ConfirmButton> |
| 279 | {renderAction(actions[actions.length - 1]!, actions.length - 1)} |
| 280 | </AlertDialog.ConfirmButton> |
| 281 | ) : null} |
| 282 | </AlertDialog> |
| 283 | </NativeThemeHost> |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | interface DialogPresentationProps { |
| 288 | readonly alertActions?: readonly AlertButton[]; |
| 289 | readonly onAlertAction?: (action: AlertButton) => void; |
| 290 | readonly dismissable?: boolean; |
| 291 | readonly request: Pick< |
| 292 | ConfirmDialogRequest, |
| 293 | "title" | "message" | "cancelText" | "confirmText" | "destructive" |
| 294 | >; |
| 295 | readonly inputInitialValue?: string; |
| 296 | readonly inputPlaceholder?: string; |
| 297 | readonly onCancel: () => void; |
| 298 | readonly onConfirm: (inputValue?: string) => void; |
| 299 | } |