Skip to content

Commit a3e2f76

Browse files
committed
add types to key
1 parent 2130977 commit a3e2f76

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

cli/src/chat.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { routeUserPrompt } from './commands/router'
55
import { AgentModeToggle } from './components/agent-mode-toggle'
66
import { BuildModeButtons } from './components/build-mode-buttons'
77
import { LoginModal } from './components/login-modal'
8+
import { MessageRenderer } from './components/message-renderer'
89
import {
910
MultilineInput,
1011
type MultilineInputHandle,
@@ -20,7 +21,6 @@ import { useElapsedTime } from './hooks/use-elapsed-time'
2021
import { useInputHistory } from './hooks/use-input-history'
2122
import { useKeyboardHandlers } from './hooks/use-keyboard-handlers'
2223
import { useMessageQueue } from './hooks/use-message-queue'
23-
import { MessageRenderer } from './components/message-renderer'
2424
import { useChatScrollbox } from './hooks/use-scroll-management'
2525
import { useSendMessage } from './hooks/use-send-message'
2626
import { useSuggestionEngine } from './hooks/use-suggestion-engine'
@@ -39,7 +39,7 @@ import { BORDER_CHARS } from './utils/ui-constants'
3939
import type { SendMessageTimerEvent } from './hooks/use-send-message'
4040
import type { ContentBlock } from './types/chat'
4141
import type { SendMessageFn } from './types/contracts/send-message'
42-
import type { ScrollBoxRenderable } from '@opentui/core'
42+
import type { KeyEvent, ScrollBoxRenderable } from '@opentui/core'
4343

4444
const MAX_VIRTUALIZED_TOP_LEVEL = 60
4545
const VIRTUAL_OVERSCAN = 12
@@ -387,14 +387,12 @@ export const Chat = ({
387387
}, [agentMatches.length, agentSelectedIndex])
388388

389389
const handleSlashMenuKey = useCallback(
390-
(
391-
key: any,
392-
): boolean => {
390+
(key: KeyEvent): boolean => {
393391
if (!slashContext.active || slashMatches.length === 0) {
394392
return false
395393
}
396394

397-
const hasModifier = Boolean(key.ctrl || key.meta || key.alt || key.option)
395+
const hasModifier = Boolean(key.ctrl || key.meta || key.option)
398396

399397
function selectCurrent(): boolean {
400398
const selected = slashMatches[slashSelectedIndex] ?? slashMatches[0]
@@ -412,7 +410,11 @@ export const Chat = ({
412410
)
413411
const replacement = `/${selected.id} `
414412
const newValue = before + replacement + after
415-
setInputValue({text: newValue, cursorPosition: before.length + replacement.length, lastEditDueToNav: false})
413+
setInputValue({
414+
text: newValue,
415+
cursorPosition: before.length + replacement.length,
416+
lastEditDueToNav: false,
417+
})
416418
setSlashSelectedIndex(0)
417419
return true
418420
}
@@ -468,14 +470,12 @@ export const Chat = ({
468470
)
469471

470472
const handleAgentMenuKey = useCallback(
471-
(
472-
key: any,
473-
): boolean => {
473+
(key: KeyEvent): boolean => {
474474
if (!mentionContext.active || agentMatches.length === 0) {
475475
return false
476476
}
477477

478-
const hasModifier = Boolean(key.ctrl || key.meta || key.alt || key.option)
478+
const hasModifier = Boolean(key.ctrl || key.meta || key.option)
479479

480480
function selectCurrent(): boolean {
481481
const selected = agentMatches[agentSelectedIndex] ?? agentMatches[0]
@@ -494,7 +494,8 @@ export const Chat = ({
494494
)
495495
const replacement = `@${selected.displayName} `
496496
const newValue = before + replacement + after
497-
setInputValue({text: newValue,
497+
setInputValue({
498+
text: newValue,
498499
cursorPosition: before.length + replacement.length,
499500
lastEditDueToNav: false,
500501
})
@@ -553,9 +554,7 @@ export const Chat = ({
553554
)
554555

555556
const handleSuggestionMenuKey = useCallback(
556-
(
557-
key: any,
558-
): boolean => {
557+
(key: KeyEvent): boolean => {
559558
if (handleSlashMenuKey(key)) {
560559
return true
561560
}
@@ -748,8 +747,6 @@ export const Chat = ({
748747
topLevelMessages.length - virtualTopLevelMessages.length,
749748
)
750749

751-
752-
753750
const virtualizationNotice =
754751
shouldVirtualize && hiddenTopLevelCount > 0 ? (
755752
<text

cli/src/components/multiline-input.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useTheme } from '../hooks/use-theme'
1515
import { computeInputLayoutMetrics } from '../utils/text-layout'
1616

1717
import type { InputValue } from '../state/chat-store'
18-
import type { PasteEvent, ScrollBoxRenderable } from '@opentui/core'
18+
import type { KeyEvent, PasteEvent, ScrollBoxRenderable } from '@opentui/core'
1919

2020
// Helper functions for text manipulation
2121
function findLineStart(text: string, cursor: number): number {
@@ -83,7 +83,7 @@ interface MultilineInputProps {
8383
value: string
8484
onChange: (value: InputValue | ((prev: InputValue) => InputValue)) => void
8585
onSubmit: () => void
86-
onKeyIntercept?: (key: any) => boolean
86+
onKeyIntercept?: (key: KeyEvent) => boolean
8787
placeholder?: string
8888
focused?: boolean
8989
maxHeight?: number
@@ -196,7 +196,7 @@ export const MultilineInput = forwardRef<
196196
// Handle all keyboard input with advanced shortcuts
197197
useKeyboard(
198198
useCallback(
199-
(key: any) => {
199+
(key: KeyEvent) => {
200200
if (!focused) return
201201

202202
if (onKeyIntercept) {
@@ -229,7 +229,6 @@ export const MultilineInput = forwardRef<
229229
!key.shift &&
230230
!key.ctrl &&
231231
!key.meta &&
232-
!key.alt &&
233232
!key.option &&
234233
!isAltLikeModifier &&
235234
!hasEscapePrefix &&
@@ -243,7 +242,6 @@ export const MultilineInput = forwardRef<
243242
key.ctrl &&
244243
!key.meta &&
245244
!key.option &&
246-
!key.alt &&
247245
(lowerKeyName === 'j' || isEnterKey)
248246
const isBackslashEnter = isEnterKey && hasBackslashBeforeCursor
249247

@@ -432,7 +430,7 @@ export const MultilineInput = forwardRef<
432430
}
433431

434432
// Basic Backspace (no modifiers)
435-
if (key.name === 'backspace' && !key.ctrl && !key.meta && !key.alt) {
433+
if (key.name === 'backspace' && !key.ctrl && !key.meta && !key.option) {
436434
preventKeyDefault(key)
437435
if (cursorPosition > 0) {
438436
const newValue =
@@ -447,7 +445,7 @@ export const MultilineInput = forwardRef<
447445
}
448446

449447
// Basic Delete (no modifiers)
450-
if (key.name === 'delete' && !key.ctrl && !key.meta && !key.alt) {
448+
if (key.name === 'delete' && !key.ctrl && !key.meta && !key.option) {
451449
preventKeyDefault(key)
452450
if (cursorPosition < value.length) {
453451
const newValue =
@@ -540,27 +538,27 @@ export const MultilineInput = forwardRef<
540538
}
541539

542540
// Left arrow (no modifiers)
543-
if (key.name === 'left' && !key.ctrl && !key.meta && !key.alt) {
541+
if (key.name === 'left' && !key.ctrl && !key.meta && !key.option) {
544542
preventKeyDefault(key)
545543
setCursorPosition(Math.max(0, cursorPosition - 1))
546544
return
547545
}
548546

549547
// Right arrow (no modifiers)
550-
if (key.name === 'right' && !key.ctrl && !key.meta && !key.alt) {
548+
if (key.name === 'right' && !key.ctrl && !key.meta && !key.option) {
551549
preventKeyDefault(key)
552550
setCursorPosition(Math.min(value.length, cursorPosition + 1))
553551
return
554552
}
555553

556554
// Up arrow (no modifiers)
557-
if (key.name === 'up' && !key.ctrl && !key.meta && !key.alt) {
555+
if (key.name === 'up' && !key.ctrl && !key.meta && !key.option) {
558556
preventKeyDefault(key)
559557
setCursorPosition(cursorPosition - getEffectiveCols())
560558
}
561559

562560
// Down arrow (no modifiers)
563-
if (key.name === 'down' && !key.ctrl && !key.meta && !key.alt) {
561+
if (key.name === 'down' && !key.ctrl && !key.meta && !key.option) {
564562
preventKeyDefault(key)
565563
setCursorPosition(cursorPosition + getEffectiveCols())
566564
}
@@ -571,7 +569,7 @@ export const MultilineInput = forwardRef<
571569
key.sequence.length === 1 &&
572570
!key.ctrl &&
573571
!key.meta &&
574-
!key.alt
572+
!key.option
575573
) {
576574
preventKeyDefault(key)
577575
const newValue =

cli/src/hooks/use-login-keyboard-handlers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useKeyboard } from '@opentui/react'
22
import { useCallback } from 'react'
33

4+
import type { KeyEvent } from '@opentui/core'
5+
46
interface UseLoginKeyboardHandlersParams {
57
loginUrl: string | null
68
hasOpenedBrowser: boolean
@@ -23,7 +25,7 @@ export function useLoginKeyboardHandlers({
2325
}: UseLoginKeyboardHandlersParams) {
2426
useKeyboard(
2527
useCallback(
26-
(key: any) => {
28+
(key: KeyEvent) => {
2729
const isEnter =
2830
(key.name === 'return' || key.name === 'enter') &&
2931
!key.ctrl &&

0 commit comments

Comments
 (0)