|
| 1 | +import type { ScrollBoxRenderable } from '@opentui/core' |
| 2 | +import { useKeyboard } from '@opentui/react' |
| 3 | +import { useMemo, useRef, useState, type RefObject } from 'react' |
| 4 | + |
| 5 | +import { getFilteredCommands } from './filter-commands' |
| 6 | +import type { Command } from './types' |
| 7 | + |
| 8 | +type UseCommandMenuReturn = { |
| 9 | + showCommandMenu: boolean |
| 10 | + commandQuery: string |
| 11 | + selectedIndex: number |
| 12 | + scrollRef: RefObject<ScrollBoxRenderable | null> |
| 13 | + handleContentChange: (text: string) => void |
| 14 | + resolveCommand: (index: number) => Command | undefined |
| 15 | + setSelectedIndex: (index: number) => void |
| 16 | +} |
| 17 | + |
| 18 | +const useCommandMenu = (): UseCommandMenuReturn => { |
| 19 | + const [textValue, setTextValue] = useState('') |
| 20 | + const [selectedIndex, setSelectedIndex] = useState(0) |
| 21 | + const [showCommandMenu, setShowCommandMenu] = useState(false) |
| 22 | + |
| 23 | + const scrollRef = useRef<ScrollBoxRenderable | null>(null) |
| 24 | + |
| 25 | + const commandQuery = |
| 26 | + showCommandMenu && textValue.startsWith('/') ? textValue.slice(1) : '' |
| 27 | + |
| 28 | + const filteredCommands = useMemo( |
| 29 | + () => getFilteredCommands(commandQuery), |
| 30 | + [commandQuery] |
| 31 | + ) |
| 32 | + |
| 33 | + const handleContentChange = (text: string) => { |
| 34 | + setTextValue(text) |
| 35 | + setSelectedIndex(0) |
| 36 | + |
| 37 | + // jump back to top of list when the user types a new caracter |
| 38 | + const scrollBox = scrollRef.current |
| 39 | + if (scrollBox) scrollBox.scrollTo(0) |
| 40 | + |
| 41 | + const prefixMatch = text.startsWith('/') ? text.slice(1) : null |
| 42 | + if (prefixMatch !== null && !prefixMatch.includes(' ')) { |
| 43 | + setShowCommandMenu(true) |
| 44 | + } else { |
| 45 | + setShowCommandMenu(false) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // resolve a command at a specific index (returns the command, caller, handles execution) |
| 50 | + const resolveCommand = (index: number): Command | undefined => { |
| 51 | + const command = filteredCommands[index] |
| 52 | + |
| 53 | + if (command) setShowCommandMenu(false) |
| 54 | + |
| 55 | + return command |
| 56 | + } |
| 57 | + |
| 58 | + // arrow keys move selection; the list follows along when the highlighted item goes out of view |
| 59 | + useKeyboard(key => { |
| 60 | + if (!showCommandMenu) return |
| 61 | + |
| 62 | + if (key.name === 'scape') { |
| 63 | + key.preventDefault() |
| 64 | + setShowCommandMenu(false) |
| 65 | + } else if (key.name === 'up') { |
| 66 | + key.preventDefault() |
| 67 | + setSelectedIndex(i => { |
| 68 | + const newIndex = Math.max(0, i - 1) |
| 69 | + |
| 70 | + // keep the highlighted item visible when arrowing past the edge |
| 71 | + const sb = scrollRef.current |
| 72 | + if (sb && newIndex < sb.scrollTop) { |
| 73 | + sb.scrollTo(newIndex) |
| 74 | + } |
| 75 | + |
| 76 | + return newIndex |
| 77 | + }) |
| 78 | + } else if (key.name === 'down') { |
| 79 | + key.preventDefault() |
| 80 | + setSelectedIndex(i => { |
| 81 | + if (filteredCommands.length === 0) return 0 |
| 82 | + |
| 83 | + const newIndex = Math.min(filteredCommands.length - 1, i + 1) |
| 84 | + const sb = scrollRef.current |
| 85 | + |
| 86 | + if (sb) { |
| 87 | + const viewportHeight = sb.viewport.height |
| 88 | + const visibleEnd = sb.scrollTop + viewportHeight - 1 |
| 89 | + |
| 90 | + if (newIndex > visibleEnd) { |
| 91 | + sb.scrollTo(newIndex - viewportHeight + 1) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return newIndex |
| 96 | + }) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + return { |
| 101 | + showCommandMenu, |
| 102 | + commandQuery, |
| 103 | + selectedIndex, |
| 104 | + scrollRef, |
| 105 | + handleContentChange, |
| 106 | + resolveCommand, |
| 107 | + setSelectedIndex |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export { useCommandMenu } |
0 commit comments