diff --git a/apps/android/src/ui/alert.tsx b/apps/android/src/ui/alert.tsx new file mode 100644 index 0000000..29d9c73 --- /dev/null +++ b/apps/android/src/ui/alert.tsx @@ -0,0 +1,78 @@ +import { useRef, useSyncExternalStore } from 'react' +import { ConfirmDialog, type ConfirmDialogCloseCause } from '../screens/chat/confirm-dialog' + +/** + * Drop-in replacement for React Native's `Alert.alert`: same call signature, + * but the dialog renders through the app-themed `ConfirmDialog` instead of the + * system AlertDialog, so colors, button styling and wording follow the theme. + * `AlertHost` must be mounted once near the app root. + */ + +export interface AlertButton { + text: string + style?: 'default' | 'cancel' | 'destructive' + onPress?: () => void +} + +interface AlertRequest { + title: string + message?: string + buttons: AlertButton[] + onDismiss?: () => void +} + +const store = { request: undefined as AlertRequest | undefined } +let notify: (() => void) | undefined + +function subscribe(callback: () => void): () => void { + notify = callback + return () => { notify = undefined } +} + +export const Alert = { + alert( + title: string, + message?: string, + buttons?: AlertButton[], + options?: { cancelable?: boolean; onDismiss?: () => void }, + ): void { + store.request = { + title, + ...(message !== undefined ? { message } : {}), + buttons: buttons && buttons.length > 0 ? buttons : [{ text: 'OK' }], + ...(options?.onDismiss !== undefined ? { onDismiss: options.onDismiss } : {}), + } + notify?.() + }, +} + +export function AlertHost() { + const request = useSyncExternalStore(subscribe, () => store.request) + // Keep the last request around so the dialog fades out with its content + // instead of blanking the moment the request is cleared. + const lastRef = useRef(undefined) + if (request !== undefined) lastRef.current = request + const shown = request ?? lastRef.current + // React Native's Alert only calls `onDismiss` when the alert is closed without + // picking a button (backdrop tap or back). Button presses must not settle a + // caller's "cancelled" promise before its own action runs. + const dismiss = (cause: ConfirmDialogCloseCause) => { + const current = store.request + store.request = undefined + if (cause === 'dismiss') current?.onDismiss?.() + notify?.() + } + return ( + ({ + label: button.text, + danger: button.style === 'destructive', + onPress: button.onPress, + }))} + /> + ) +}