Fix bug updating the cursor after navigating history. (#507)

This commit is contained in:
Jacob Richman
2025-05-23 09:40:01 -07:00
committed by GitHub
parent a008d81780
commit a96ff934ea
3 changed files with 269 additions and 16 deletions

View File

@@ -11,7 +11,7 @@ interface UseInputHistoryProps {
onSubmit: (value: string) => void;
isActive: boolean;
currentQuery: string; // Renamed from query to avoid confusion
onChangeAndMoveCursor: (value: string) => void;
onChange: (value: string) => void;
}
interface UseInputHistoryReturn {
@@ -25,7 +25,7 @@ export function useInputHistory({
onSubmit,
isActive,
currentQuery,
onChangeAndMoveCursor: setQueryAndMoveCursor,
onChange,
}: UseInputHistoryProps): UseInputHistoryReturn {
const [historyIndex, setHistoryIndex] = useState<number>(-1);
const [originalQueryBeforeNav, setOriginalQueryBeforeNav] =
@@ -65,14 +65,14 @@ export function useInputHistory({
if (nextIndex !== historyIndex) {
setHistoryIndex(nextIndex);
const newValue = userMessages[userMessages.length - 1 - nextIndex];
setQueryAndMoveCursor(newValue); // Call the prop passed from parent
onChange(newValue);
return true;
}
return false;
}, [
historyIndex,
setHistoryIndex,
setQueryAndMoveCursor,
onChange,
userMessages,
isActive,
currentQuery, // Use currentQuery from props
@@ -88,17 +88,17 @@ export function useInputHistory({
if (nextIndex === -1) {
// Reached the end of history navigation, restore original query
setQueryAndMoveCursor(originalQueryBeforeNav);
onChange(originalQueryBeforeNav);
} else {
const newValue = userMessages[userMessages.length - 1 - nextIndex];
setQueryAndMoveCursor(newValue);
onChange(newValue);
}
return true;
}, [
historyIndex,
setHistoryIndex,
originalQueryBeforeNav,
setQueryAndMoveCursor,
onChange,
userMessages,
isActive,
]);