Files
gemini-cli/packages/cli/src/ui/hooks/useInputHistory.ts
T

112 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-04-19 14:31:59 +01:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
2025-05-13 11:24:04 -07:00
import { useState, useCallback } from 'react';
2025-04-19 14:31:59 +01:00
interface UseInputHistoryProps {
2025-04-30 08:31:32 -07:00
userMessages: readonly string[];
onSubmit: (value: string) => void;
isActive: boolean;
2025-05-13 11:24:04 -07:00
currentQuery: string; // Renamed from query to avoid confusion
onChange: (value: string) => void;
2025-04-19 14:31:59 +01:00
}
export interface UseInputHistoryReturn {
2025-04-30 08:31:32 -07:00
handleSubmit: (value: string) => void;
2025-05-13 11:24:04 -07:00
navigateUp: () => boolean;
navigateDown: () => boolean;
2025-04-19 14:31:59 +01:00
}
export function useInputHistory({
userMessages,
onSubmit,
isActive,
2025-05-13 11:24:04 -07:00
currentQuery,
onChange,
2025-04-19 14:31:59 +01:00
}: UseInputHistoryProps): UseInputHistoryReturn {
2025-04-30 08:31:32 -07:00
const [historyIndex, setHistoryIndex] = useState<number>(-1);
2025-04-19 14:31:59 +01:00
const [originalQueryBeforeNav, setOriginalQueryBeforeNav] =
useState<string>('');
const resetHistoryNav = useCallback(() => {
setHistoryIndex(-1);
setOriginalQueryBeforeNav('');
}, []);
const handleSubmit = useCallback(
(value: string) => {
const trimmedValue = value.trim();
if (trimmedValue) {
2025-05-13 11:24:04 -07:00
onSubmit(trimmedValue); // Parent handles clearing the query
2025-04-19 14:31:59 +01:00
}
2025-04-30 08:31:32 -07:00
resetHistoryNav();
2025-04-19 14:31:59 +01:00
},
[onSubmit, resetHistoryNav],
);
2025-05-13 11:24:04 -07:00
const navigateUp = useCallback(() => {
if (!isActive) return false;
if (userMessages.length === 0) return false;
2025-04-19 14:31:59 +01:00
2025-05-13 11:24:04 -07:00
let nextIndex = historyIndex;
if (historyIndex === -1) {
// Store the current query from the parent before navigating
setOriginalQueryBeforeNav(currentQuery);
nextIndex = 0;
} else if (historyIndex < userMessages.length - 1) {
nextIndex = historyIndex + 1;
} else {
return false; // Already at the oldest message
}
2025-04-19 14:31:59 +01:00
2025-05-13 11:24:04 -07:00
if (nextIndex !== historyIndex) {
setHistoryIndex(nextIndex);
const newValue = userMessages[userMessages.length - 1 - nextIndex];
onChange(newValue);
2025-05-13 11:24:04 -07:00
return true;
}
return false;
}, [
historyIndex,
setHistoryIndex,
onChange,
2025-05-13 11:24:04 -07:00
userMessages,
isActive,
currentQuery, // Use currentQuery from props
setOriginalQueryBeforeNav,
]);
2025-04-19 14:31:59 +01:00
2025-05-13 11:24:04 -07:00
const navigateDown = useCallback(() => {
if (!isActive) return false;
if (historyIndex === -1) return false; // Not currently navigating history
2025-04-19 14:31:59 +01:00
2025-05-13 11:24:04 -07:00
const nextIndex = historyIndex - 1;
setHistoryIndex(nextIndex);
2025-04-19 14:31:59 +01:00
2025-05-13 11:24:04 -07:00
if (nextIndex === -1) {
// Reached the end of history navigation, restore original query
onChange(originalQueryBeforeNav);
2025-05-13 11:24:04 -07:00
} else {
const newValue = userMessages[userMessages.length - 1 - nextIndex];
onChange(newValue);
2025-05-13 11:24:04 -07:00
}
return true;
}, [
historyIndex,
setHistoryIndex,
originalQueryBeforeNav,
onChange,
2025-05-13 11:24:04 -07:00
userMessages,
isActive,
]);
2025-04-19 14:31:59 +01:00
return {
2025-04-30 08:31:32 -07:00
handleSubmit,
2025-05-13 11:24:04 -07:00
navigateUp,
navigateDown,
2025-04-19 14:31:59 +01:00
};
}