({
state,
handlers,
disabled = false,
}: UseChatKeyboardOptions)
| 287 | * escape/ctrl-c appropriately. |
| 288 | */ |
| 289 | export function useChatKeyboard({ |
| 290 | state, |
| 291 | handlers, |
| 292 | disabled = false, |
| 293 | }: UseChatKeyboardOptions): void { |
| 294 | const lastKeyboardActivityRef = useRef<number>(0) |
| 295 | |
| 296 | useKeyboard( |
| 297 | useCallback( |
| 298 | (key: KeyEvent) => { |
| 299 | if (disabled) return |
| 300 | |
| 301 | // Report keyboard activity for activity-aware features (throttled) |
| 302 | const now = Date.now() |
| 303 | if (now - lastKeyboardActivityRef.current > KEYBOARD_ACTIVITY_THROTTLE_MS) { |
| 304 | lastKeyboardActivityRef.current = now |
| 305 | reportActivity() |
| 306 | } |
| 307 | |
| 308 | markReturnKeySeenForKey(key) |
| 309 | |
| 310 | const action = resolveChatKeyboardAction(key, state) |
| 311 | const handled = dispatchAction(action, handlers) |
| 312 | |
| 313 | // Prevent default for handled actions |
| 314 | if ( |
| 315 | handled && |
| 316 | 'preventDefault' in key && |
| 317 | typeof key.preventDefault === 'function' |
| 318 | ) { |
| 319 | key.preventDefault() |
| 320 | } |
| 321 | }, |
| 322 | [state, handlers, disabled], |
| 323 | ), |
| 324 | ) |
| 325 | } |
no test coverage detected