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

115 lines
2.8 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
*/
import { useCallback, useState } from 'react';
2025-04-19 14:31:59 +01:00
import { useInput } from 'ink';
interface UseInputHistoryProps {
2025-04-30 08:31:32 -07:00
userMessages: readonly string[];
onSubmit: (value: string) => void;
isActive: boolean;
query: string;
setQuery: React.Dispatch<React.SetStateAction<string>>;
2025-04-19 14:31:59 +01:00
}
interface UseInputHistoryReturn {
2025-04-30 08:31:32 -07:00
query: string;
setQuery: React.Dispatch<React.SetStateAction<string>>;
handleSubmit: (value: string) => void;
inputKey: number;
setInputKey: React.Dispatch<React.SetStateAction<number>>;
2025-04-19 14:31:59 +01:00
}
export function useInputHistory({
userMessages,
onSubmit,
isActive,
query,
setQuery,
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>('');
2025-04-30 08:31:32 -07:00
const [inputKey, setInputKey] = useState<number>(0);
2025-04-19 14:31:59 +01:00
const resetHistoryNav = useCallback(() => {
setHistoryIndex(-1);
setOriginalQueryBeforeNav('');
}, []);
const handleSubmit = useCallback(
(value: string) => {
const trimmedValue = value.trim();
if (trimmedValue) {
onSubmit(trimmedValue); // This will call handleFinalSubmit, which then calls setQuery('') from App.tsx
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],
);
useInput(
(input, key) => {
if (!isActive) {
return;
}
let didNavigate = false;
if (key.upArrow) {
if (userMessages.length === 0) return;
let nextIndex = historyIndex;
if (historyIndex === -1) {
setOriginalQueryBeforeNav(query);
2025-04-30 08:31:32 -07:00
nextIndex = 0;
2025-04-19 14:31:59 +01:00
} else if (historyIndex < userMessages.length - 1) {
nextIndex = historyIndex + 1;
} else {
2025-04-30 08:31:32 -07:00
return;
2025-04-19 14:31:59 +01:00
}
if (nextIndex !== historyIndex) {
setHistoryIndex(nextIndex);
const newValue = userMessages[userMessages.length - 1 - nextIndex];
setQuery(newValue);
2025-04-30 08:31:32 -07:00
setInputKey((k) => k + 1);
2025-04-19 14:31:59 +01:00
didNavigate = true;
}
} else if (key.downArrow) {
2025-04-30 08:31:32 -07:00
if (historyIndex === -1) return;
2025-04-19 14:31:59 +01:00
2025-04-30 08:31:32 -07:00
const nextIndex = historyIndex - 1;
2025-04-19 14:31:59 +01:00
setHistoryIndex(nextIndex);
if (nextIndex === -1) {
setQuery(originalQueryBeforeNav);
} else {
const newValue = userMessages[userMessages.length - 1 - nextIndex];
setQuery(newValue);
}
2025-04-30 08:31:32 -07:00
setInputKey((k) => k + 1);
2025-04-19 14:31:59 +01:00
didNavigate = true;
} else {
if (historyIndex !== -1 && !didNavigate) {
if (input || key.backspace || key.delete) {
resetHistoryNav();
}
}
}
},
2025-04-30 08:31:32 -07:00
{ isActive },
2025-04-19 14:31:59 +01:00
);
return {
query,
2025-04-30 08:31:32 -07:00
setQuery,
handleSubmit,
inputKey,
setInputKey,
2025-04-19 14:31:59 +01:00
};
}