({
onSubmit,
disabled,
history,
}: {
onSubmit: (q: string) => void;
disabled: boolean;
history: string[];
})
| 167 | } |
| 168 | |
| 169 | function QuestionInput({ |
| 170 | onSubmit, |
| 171 | disabled, |
| 172 | history, |
| 173 | }: { |
| 174 | onSubmit: (q: string) => void; |
| 175 | disabled: boolean; |
| 176 | history: string[]; |
| 177 | }) { |
| 178 | const [value, setValue] = useState(''); |
| 179 | const [historyIndex, setHistoryIndex] = useState(-1); |
| 180 | |
| 181 | useInput((input, key) => { |
| 182 | if (disabled) return; |
| 183 | |
| 184 | if (key.return) { |
| 185 | if (value.trim()) { |
| 186 | onSubmit(value.trim()); |
| 187 | setValue(''); |
| 188 | setHistoryIndex(-1); |
| 189 | } |
| 190 | } else if (key.upArrow) { |
| 191 | // Navigate history backwards |
| 192 | if (history.length > 0) { |
| 193 | const newIndex = Math.min(historyIndex + 1, history.length - 1); |
| 194 | setHistoryIndex(newIndex); |
| 195 | setValue(history[history.length - 1 - newIndex] || ''); |
| 196 | } |
| 197 | } else if (key.downArrow) { |
| 198 | // Navigate history forwards |
| 199 | if (historyIndex > 0) { |
| 200 | const newIndex = historyIndex - 1; |
| 201 | setHistoryIndex(newIndex); |
| 202 | setValue(history[history.length - 1 - newIndex] || ''); |
| 203 | } else if (historyIndex === 0) { |
| 204 | setHistoryIndex(-1); |
| 205 | setValue(''); |
| 206 | } |
| 207 | } else if (key.backspace || key.delete) { |
| 208 | setValue((v) => v.slice(0, -1)); |
| 209 | setHistoryIndex(-1); |
| 210 | } else if (!key.ctrl && !key.meta && input) { |
| 211 | setValue((v) => v + input); |
| 212 | setHistoryIndex(-1); |
| 213 | } |
| 214 | }); |
| 215 | |
| 216 | return ( |
| 217 | <Box> |
| 218 | <Text bold color="cyan"> |
| 219 | {'❯ '} |
| 220 | </Text> |
| 221 | <Text>{value}</Text> |
| 222 | {!disabled && <Text color="cyan">▌</Text>} |
| 223 | </Box> |
| 224 | ); |
| 225 | } |
| 226 |
nothing calls this directly
no outgoing calls
no test coverage detected