'use client'; import { useCallback } from 'react'; import { showDialog, dialogStore } from '../store'; import type { DialogOptions, AuthDialogOptions } from '../types'; /** * React hook for using dialog service * * Provides type-safe access to dialogs via zustand store with fallback to native dialogs. * * @example * ```tsx * function MyComponent() { * const { confirm, alert, prompt, auth } = useDialog(); * * const handleDelete = async () => { * const confirmed = await confirm({ * title: 'Delete Item', * message: 'Are you sure?', * variant: 'destructive', * }); * if (confirmed) { * // delete logic * } * }; * * const handleProtected = async () => { * const authed = await auth({ message: 'Please sign in to continue' }); * if (authed) { * // proceed * } * }; * * return ; * } * ``` */ export function useDialog() { const alert = useCallback( (message: string | DialogOptions): Promise => { if (typeof window === 'undefined') { return Promise.resolve(); } if (!window.dialog) { window.alert(typeof message === 'string' ? message : message.message); return Promise.resolve(); } return showDialog('alert', message).then(() => undefined); }, [], ); const confirm = useCallback( (message: string | DialogOptions): Promise => { if (typeof window === 'undefined') { return Promise.resolve(false); } if (!window.dialog) { return Promise.resolve( window.confirm(typeof message === 'string' ? message : message.message), ); } return showDialog('confirm', message) as Promise; }, [], ); const prompt = useCallback( (message: string | DialogOptions): Promise => { if (typeof window === 'undefined') { return Promise.resolve(null); } if (!window.dialog) { const opts = typeof message === 'string' ? { message } : message; return Promise.resolve(window.prompt(opts.message, opts.defaultValue)); } return showDialog('prompt', message) as Promise; }, [], ); const auth = useCallback( (options?: AuthDialogOptions): Promise => { return dialogStore.getState().openAuth(options); }, [], ); return { alert, confirm, prompt, auth }; }