// components/DialogProvider.tsx import React, { createContext, useContext, useState, useRef, useCallback, } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle, DialogDescription, DialogClose, } from '../ui/dialog'; import { View } from 'react-native'; import { Button } from '../ui/button'; import { Text } from '../ui/text'; import { Input } from '../ui/input'; type DialogOptions = | { type: 'confirm'; title: string; message?: string } | { type: 'prompt'; title: string; message?: string; defaultValue?: string }; type DialogContextType = { prompt: ( title: string, message?: string, defaultValue?: string, ) => Promise; confirm: (title: string, message?: string) => Promise; showDialog: (options: DialogOptions) => Promise; }; const DialogContext = createContext(null); export const useDialog = () => { const ctx = useContext(DialogContext); if (!ctx) throw new Error('useDialog must be used within DialogProvider'); return ctx; }; export const DialogProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const [visible, setVisible] = useState(false); const [options, setOptions] = useState(null); const [inputValue, setInputValue] = useState(''); const resolver = useRef<(val: boolean | string) => void>(() => {}); const showDialog = useCallback( (opts: DialogOptions): Promise => { return new Promise((resolve) => { resolver.current = resolve; setOptions(opts); setInputValue(opts.type === 'prompt' ? (opts.defaultValue ?? '') : ''); setVisible(true); }); }, [], ); const prompt = useCallback( async ( title: string, message?: string, defaultValue?: string, ): Promise => { return showDialog({ type: 'prompt', title, message, defaultValue, }) as Promise; }, [showDialog], ); const confirm = useCallback( async (title: string, message?: string): Promise => { return showDialog({ type: 'confirm', title, message, }) as Promise; }, [showDialog], ); const handleCancel = () => { setVisible(false); resolver.current?.(options?.type === 'confirm' ? false : ''); }; const handleConfirm = () => { setVisible(false); resolver.current?.(options?.type === 'confirm' ? true : inputValue); }; return ( {children} {options?.title} {options?.message && ( {options.message} )} {options?.type === 'prompt' && ( )} ); };