MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / useExitHandler

Function useExitHandler

cli/src/hooks/use-exit-handler.ts:55–108  ·  view source on GitHub ↗
({
  inputValue,
  setInputValue,
}: UseExitHandlerOptions)

Source from the content-addressed store, hash-verified

53}
54
55export 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}

Callers 1

useChatStreamingFunction · 0.90

Calls 5

setupExitMessageHandlerFunction · 0.85
setInputValueFunction · 0.85
setTimeoutFunction · 0.85
exitCliFunction · 0.85
onMethod · 0.65

Tested by

no test coverage detected