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