| 44 | }; |
| 45 | |
| 46 | export function usePrompt(): UsePromptRet { |
| 47 | const client = useQueryClient(); |
| 48 | const { data: prompt } = useQuery({ |
| 49 | queryKey: PROMPT_KEY, |
| 50 | queryFn: () => client.getQueryData<PromptState>(PROMPT_KEY) ?? null, |
| 51 | }); |
| 52 | |
| 53 | const setPrompt = useCallback( |
| 54 | (data: PromptState) => client.setQueryData(PROMPT_KEY, data), |
| 55 | [client], |
| 56 | ); |
| 57 | |
| 58 | const showPrompt = useCallback( |
| 59 | (promptOptions: PromptOptions): Promise<boolean> => |
| 60 | new Promise((resolve) => { |
| 61 | const closeWith = (result: boolean) => { |
| 62 | resolve(result); |
| 63 | setPrompt(null); |
| 64 | }; |
| 65 | const newPrompt: Prompt = { |
| 66 | options: promptOptions, |
| 67 | onFail: () => closeWith(false), |
| 68 | onSuccess: () => closeWith(true), |
| 69 | onError: () => closeWith(false), |
| 70 | }; |
| 71 | setPrompt(newPrompt); |
| 72 | }), |
| 73 | [setPrompt], |
| 74 | ); |
| 75 | |
| 76 | return { showPrompt, prompt: prompt ?? null }; |
| 77 | } |