({
inputValue,
setInputValue,
}: UseExitHandlerOptions)
| 53 | } |
| 54 | |
| 55 | export const useExitHandler = ({ |
| 56 | inputValue, |
| 57 | setInputValue, |
| 58 | }: UseExitHandlerOptions) => { |
| 59 | const [nextCtrlCWillExit, setNextCtrlCWillExit] = useState(false) |
| 60 | const exitWarningTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>( |
| 61 | null, |
| 62 | ) |
| 63 | |
| 64 | useEffect(() => { |
| 65 | setupExitMessageHandler() |
| 66 | }, []) |
| 67 | |
| 68 | const handleCtrlC = useCallback(() => { |
| 69 | if (inputValue) { |
| 70 | setInputValue({ text: '', cursorPosition: 0, lastEditDueToNav: false }) |
| 71 | return true |
| 72 | } |
| 73 | |
| 74 | if (!nextCtrlCWillExit) { |
| 75 | setNextCtrlCWillExit(true) |
| 76 | setTimeout(() => { |
| 77 | setNextCtrlCWillExit(false) |
| 78 | }, 2000) |
| 79 | return true |
| 80 | } |
| 81 | |
| 82 | if (exitWarningTimeoutRef.current) { |
| 83 | clearTimeout(exitWarningTimeoutRef.current) |
| 84 | exitWarningTimeoutRef.current = null |
| 85 | } |
| 86 | |
| 87 | exitCli() |
| 88 | return true |
| 89 | }, [inputValue, setInputValue, nextCtrlCWillExit]) |
| 90 | |
| 91 | useEffect(() => { |
| 92 | const handleSigint = () => { |
| 93 | if (exitWarningTimeoutRef.current) { |
| 94 | clearTimeout(exitWarningTimeoutRef.current) |
| 95 | exitWarningTimeoutRef.current = null |
| 96 | } |
| 97 | |
| 98 | exitCli() |
| 99 | } |
| 100 | |
| 101 | process.on('SIGINT', handleSigint) |
| 102 | return () => { |
| 103 | process.off('SIGINT', handleSigint) |
| 104 | } |
| 105 | }, []) |
| 106 | |
| 107 | return { handleCtrlC, nextCtrlCWillExit } |
| 108 | } |
no test coverage detected