Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import {ActionSheetIOS, Alert} from 'react-native'
import {cleanError} from '@/lib/errors'import {hapticWarning} from '@/lib/haptics'
// the native prompt shapes every screen repeats, behind one typed api so// no call site hand-maintains button indices or alert arrays
// mutation failure alert carrying the error's friendly copyexport const alertError = (err: unknown): void => { Alert.alert( 'Something went wrong', cleanError(err, 'Please try again.'), )}
// plain acknowledgement alert for a completed actionexport const alertInfo = (title: string, message?: string): void => { Alert.alert(title, message)}
// destructive confirmation: resolves true when the user confirmsexport const confirmDestructive = ({ title, message, actionLabel, cancelLabel = 'Cancel',}: { title: string message: string actionLabel: string cancelLabel?: string}): Promise<boolean> => new Promise((resolve) => { Alert.alert(title, message, [ { text: cancelLabel, style: 'cancel', onPress: () => resolve(false), }, { text: actionLabel, style: 'destructive', onPress: () => { hapticWarning() resolve(true) }, }, ]) })
// single text-field prompt: resolves the trimmed value, null on cancel/emptyexport const promptText = ({ title, message, defaultValue, actionLabel = 'Save',}: { title: string message?: string defaultValue?: string actionLabel?: string}): Promise<string | null> => new Promise((resolve) => { Alert.prompt( title, message, [ { text: 'Cancel', style: 'cancel', onPress: () => resolve(null), }, { text: actionLabel, onPress: (value?: string) => resolve(value?.trim() || null), }, ], 'plain-text', defaultValue, ) })
export type SheetAction = { label: string destructive?: boolean onPress: () => void}
// action sheet with derived indices: cancel is always present, destructive// styling follows the action, handlers are attached to actions directlyexport const showActionSheet = ( actions: SheetAction[], {title}: {title?: string} = {},): void => { const destructiveIndexes = actions.flatMap((action, i) => action.destructive ? [i + 1] : [], ) ActionSheetIOS.showActionSheetWithOptions( { title, options: ['Cancel', ...actions.map((a) => a.label)], cancelButtonIndex: 0, ...(destructiveIndexes.length && { destructiveButtonIndex: destructiveIndexes, }), }, (index) => { if (index > 0) actions[index - 1]?.onPress() }, )}