({
hideThanksAfterMs,
onOpen,
onSelect,
shouldShowTranscriptPrompt,
onTranscriptPromptShown,
onTranscriptSelect
}: UseSurveyStateOptions)
| 12 | onTranscriptSelect?: (appearanceId: string, selected: TranscriptShareResponse, surveyResponse: FeedbackSurveyResponse | null) => boolean | Promise<boolean>; |
| 13 | }; |
| 14 | export function useSurveyState({ |
| 15 | hideThanksAfterMs, |
| 16 | onOpen, |
| 17 | onSelect, |
| 18 | shouldShowTranscriptPrompt, |
| 19 | onTranscriptPromptShown, |
| 20 | onTranscriptSelect |
| 21 | }: UseSurveyStateOptions): { |
| 22 | state: SurveyState; |
| 23 | lastResponse: FeedbackSurveyResponse | null; |
| 24 | open: () => void; |
| 25 | handleSelect: (selected: FeedbackSurveyResponse) => boolean; |
| 26 | handleTranscriptSelect: (selected: TranscriptShareResponse) => void; |
| 27 | } { |
| 28 | const [state, setState] = useState<SurveyState>('closed'); |
| 29 | const [lastResponse, setLastResponse] = useState<FeedbackSurveyResponse | null>(null); |
| 30 | const appearanceId = useRef(randomUUID()); |
| 31 | const lastResponseRef = useRef<FeedbackSurveyResponse | null>(null); |
| 32 | const showThanksThenClose = useCallback(() => { |
| 33 | setState('thanks'); |
| 34 | setTimeout((setState_0, setLastResponse_0) => { |
| 35 | setState_0('closed'); |
| 36 | setLastResponse_0(null); |
| 37 | }, hideThanksAfterMs, setState, setLastResponse); |
| 38 | }, [hideThanksAfterMs]); |
| 39 | const showSubmittedThenClose = useCallback(() => { |
| 40 | setState('submitted'); |
| 41 | setTimeout(setState, hideThanksAfterMs, 'closed'); |
| 42 | }, [hideThanksAfterMs]); |
| 43 | const open = useCallback(() => { |
| 44 | if (state !== 'closed') { |
| 45 | return; |
| 46 | } |
| 47 | setState('open'); |
| 48 | appearanceId.current = randomUUID(); |
| 49 | void onOpen(appearanceId.current); |
| 50 | }, [state, onOpen]); |
| 51 | const handleSelect = useCallback((selected: FeedbackSurveyResponse): boolean => { |
| 52 | setLastResponse(selected); |
| 53 | lastResponseRef.current = selected; |
| 54 | // Always fire the survey response event first |
| 55 | void onSelect(appearanceId.current, selected); |
| 56 | if (selected === 'dismissed') { |
| 57 | setState('closed'); |
| 58 | setLastResponse(null); |
| 59 | } else if (shouldShowTranscriptPrompt?.(selected)) { |
| 60 | setState('transcript_prompt'); |
| 61 | onTranscriptPromptShown?.(appearanceId.current, selected); |
| 62 | return true; |
| 63 | } else { |
| 64 | showThanksThenClose(); |
| 65 | } |
| 66 | return false; |
| 67 | }, [showThanksThenClose, onSelect, shouldShowTranscriptPrompt, onTranscriptPromptShown]); |
| 68 | const handleTranscriptSelect = useCallback((selected_0: TranscriptShareResponse) => { |
| 69 | switch (selected_0) { |
| 70 | case 'yes': |
| 71 | setState('submitting'); |
no test coverage detected