First step refactoring InputPrompt (#335)

This commit is contained in:
Jacob Richman
2025-05-13 16:23:14 -07:00
committed by GitHub
parent c4fb1ad04b
commit e665d4f198
2 changed files with 26 additions and 25 deletions

View File

@@ -9,7 +9,6 @@ import { Box, Static, Text, useStdout } from 'ink';
import { StreamingState, type HistoryItem } from './types.js'; import { StreamingState, type HistoryItem } from './types.js';
import { useGeminiStream } from './hooks/useGeminiStream.js'; import { useGeminiStream } from './hooks/useGeminiStream.js';
import { useLoadingIndicator } from './hooks/useLoadingIndicator.js'; import { useLoadingIndicator } from './hooks/useLoadingIndicator.js';
import { useInputHistory } from './hooks/useInputHistory.js';
import { useThemeCommand } from './hooks/useThemeCommand.js'; import { useThemeCommand } from './hooks/useThemeCommand.js';
import { Header } from './components/Header.js'; import { Header } from './components/Header.js';
import { LoadingIndicator } from './components/LoadingIndicator.js'; import { LoadingIndicator } from './components/LoadingIndicator.js';
@@ -121,18 +120,6 @@ export const App = ({ config, settings, cliVersion }: AppProps) => {
slashCommands, slashCommands,
); );
const inputHistory = useInputHistory({
userMessages,
onSubmit: (value) => {
// Adapt onSubmit to use the lifted setQuery
handleFinalSubmit(value);
onChangeAndMoveCursor('');
},
isActive: isInputActive && !completion.showSuggestions,
currentQuery: query,
onChangeAndMoveCursor,
});
// --- Render Logic --- // --- Render Logic ---
// Get terminal width // Get terminal width
@@ -236,12 +223,11 @@ export const App = ({ config, settings, cliVersion }: AppProps) => {
onChange={setQuery} onChange={setQuery}
onChangeAndMoveCursor={onChangeAndMoveCursor} onChangeAndMoveCursor={onChangeAndMoveCursor}
editorState={editorState} editorState={editorState}
onSubmit={inputHistory.handleSubmit} onSubmit={handleFinalSubmit} // Pass handleFinalSubmit directly
showSuggestions={completion.showSuggestions} showSuggestions={completion.showSuggestions}
suggestions={completion.suggestions} suggestions={completion.suggestions}
activeSuggestionIndex={completion.activeSuggestionIndex} activeSuggestionIndex={completion.activeSuggestionIndex}
navigateHistoryUp={inputHistory.navigateUp} userMessages={userMessages} // Pass userMessages
navigateHistoryDown={inputHistory.navigateDown}
navigateSuggestionUp={completion.navigateUp} navigateSuggestionUp={completion.navigateUp}
navigateSuggestionDown={completion.navigateDown} navigateSuggestionDown={completion.navigateDown}
resetCompletion={completion.resetCompletionState} resetCompletion={completion.resetCompletionState}

View File

@@ -9,6 +9,7 @@ import { Text, Box, Key } from 'ink';
import { Colors } from '../colors.js'; import { Colors } from '../colors.js';
import { Suggestion } from './SuggestionsDisplay.js'; import { Suggestion } from './SuggestionsDisplay.js';
import { MultilineTextEditor } from './shared/multiline-editor.js'; import { MultilineTextEditor } from './shared/multiline-editor.js';
import { useInputHistory } from '../hooks/useInputHistory.js';
interface InputPromptProps { interface InputPromptProps {
query: string; query: string;
@@ -20,8 +21,7 @@ interface InputPromptProps {
suggestions: Suggestion[]; suggestions: Suggestion[];
activeSuggestionIndex: number; activeSuggestionIndex: number;
resetCompletion: () => void; resetCompletion: () => void;
navigateHistoryUp: () => void; userMessages: readonly string[];
navigateHistoryDown: () => void;
navigateSuggestionUp: () => void; navigateSuggestionUp: () => void;
navigateSuggestionDown: () => void; navigateSuggestionDown: () => void;
} }
@@ -40,12 +40,27 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
showSuggestions, showSuggestions,
suggestions, suggestions,
activeSuggestionIndex, activeSuggestionIndex,
navigateHistoryUp, userMessages,
navigateHistoryDown,
navigateSuggestionUp, navigateSuggestionUp,
navigateSuggestionDown, navigateSuggestionDown,
resetCompletion, resetCompletion,
}) => { }) => {
const handleSubmit = useCallback(
(submittedValue: string) => {
onSubmit(submittedValue);
onChangeAndMoveCursor(''); // Clear query after submit
},
[onSubmit, onChangeAndMoveCursor],
);
const inputHistory = useInputHistory({
userMessages,
onSubmit: handleSubmit,
isActive: !showSuggestions, // Input history is active when suggestions are not shown
currentQuery: query,
onChangeAndMoveCursor,
});
const handleAutocomplete = useCallback( const handleAutocomplete = useCallback(
(indexToUse: number) => { (indexToUse: number) => {
if (indexToUse < 0 || indexToUse >= suggestions.length) { if (indexToUse < 0 || indexToUse >= suggestions.length) {
@@ -112,7 +127,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
handleAutocomplete(activeSuggestionIndex); handleAutocomplete(activeSuggestionIndex);
} else { } else {
if (query.trim()) { if (query.trim()) {
onSubmit(query); handleSubmit(query);
} }
} }
return true; return true;
@@ -132,7 +147,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
showSuggestions, showSuggestions,
resetCompletion, resetCompletion,
activeSuggestionIndex, activeSuggestionIndex,
onSubmit, handleSubmit,
], ],
); );
@@ -147,8 +162,8 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
onChange={onChange} onChange={onChange}
placeholder="Enter your message or use tools (e.g., @src/file.txt)..." placeholder="Enter your message or use tools (e.g., @src/file.txt)..."
/* Account for width used by the box and &gt; */ /* Account for width used by the box and &gt; */
navigateUp={navigateHistoryUp} navigateUp={inputHistory.navigateUp}
navigateDown={navigateHistoryDown} navigateDown={inputHistory.navigateDown}
inputPreprocessor={inputPreprocessor} inputPreprocessor={inputPreprocessor}
widthUsedByParent={3} widthUsedByParent={3}
widthFraction={0.9} widthFraction={0.9}
@@ -158,7 +173,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
// as inputPreprocessor handles Enter when suggestions are visible. // as inputPreprocessor handles Enter when suggestions are visible.
const trimmedQuery = query.trim(); const trimmedQuery = query.trim();
if (!showSuggestions && trimmedQuery) { if (!showSuggestions && trimmedQuery) {
onSubmit(trimmedQuery); handleSubmit(trimmedQuery);
} }
}} }}
/> />