Refactor: Memoize hook callbacks, update dependencies, and fix lint errors (#268)

Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
Allen Hutchison 2025-05-06 14:48:49 -07:00 committed by GitHub
parent e26c436d5c
commit adeda6a5b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 54 additions and 41 deletions

View File

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
import { useCallback } from 'react'; import { useCallback, useMemo } from 'react';
import { type PartListUnion } from '@google/genai'; import { type PartListUnion } from '@google/genai';
import { HistoryItem } from '../types.js'; import { HistoryItem } from '../types.js';
import { getCommandFromQuery } from '../utils/commandUtils.js'; import { getCommandFromQuery } from '../utils/commandUtils.js';
@ -35,44 +35,54 @@ export const useSlashCommandProcessor = (
getNextMessageId: (baseTimestamp: number) => number, getNextMessageId: (baseTimestamp: number) => number,
openThemeDialog: () => void, openThemeDialog: () => void,
) => { ) => {
const slashCommands: SlashCommand[] = [ const slashCommands: SlashCommand[] = useMemo(
{ () => [
name: 'help', {
altName: '?', name: 'help',
description: 'for help on gemini-code', altName: '?',
action: (_value: PartListUnion) => { description: 'for help on gemini-code',
setDebugMessage('Opening help.'); action: (_value: PartListUnion) => {
setShowHelp(true); setDebugMessage('Opening help.');
setShowHelp(true);
},
}, },
}, {
{ name: 'clear',
name: 'clear', description: 'clear the screen',
description: 'clear the screen', action: (_value: PartListUnion) => {
action: (_value: PartListUnion) => { // This just clears the *UI* history, not the model history.
// This just clears the *UI* history, not the model history. setDebugMessage('Clearing terminal.');
setDebugMessage('Clearing terminal.'); setHistory((_) => []);
setHistory((_) => []); refreshStatic();
refreshStatic(); },
}, },
}, {
{ name: 'theme',
name: 'theme', description: 'change the theme',
description: 'change the theme', action: (_value: PartListUnion) => {
action: (_value: PartListUnion) => { openThemeDialog();
openThemeDialog(); },
}, },
}, {
{ name: 'quit',
name: 'quit', altName: 'exit',
altName: 'exit', description: '',
description: '', action: (_value: PartListUnion) => {
action: (_value: PartListUnion) => { setDebugMessage('Quitting. Good-bye.');
setDebugMessage('Quitting. Good-bye.'); getNextMessageId(Date.now());
getNextMessageId(Date.now()); process.exit(0);
process.exit(0); },
}, },
}, ],
]; [
setDebugMessage,
setShowHelp,
setHistory,
refreshStatic,
openThemeDialog,
getNextMessageId,
],
);
// Checks if the query is a slash command and executes the command if it is. // Checks if the query is a slash command and executes the command if it is.
const handleSlashCommand = useCallback( const handleSlashCommand = useCallback(
@ -109,7 +119,7 @@ export const useSlashCommandProcessor = (
return false; // Not a recognized slash command return false; // Not a recognized slash command
}, },
[setDebugMessage, setHistory, getNextMessageId, slashCommands], [setHistory, slashCommands],
); );
return { handleSlashCommand, slashCommands }; return { handleSlashCommand, slashCommands };

View File

@ -220,7 +220,7 @@ export function useCompletion(
isMounted = false; isMounted = false;
clearTimeout(debounceTimeout); clearTimeout(debounceTimeout);
}; };
}, [query, cwd, isActive, resetCompletionState]); }, [query, cwd, isActive, resetCompletionState, slashCommands]);
return { return {
suggestions, suggestions,

View File

@ -101,7 +101,7 @@ export const useGeminiStream = (
); );
} }
} }
}, [config.getApiKey(), config.getModel()]); }, [config]);
// Input Handling Effect (remains the same) // Input Handling Effect (remains the same)
useInput((input, key) => { useInput((input, key) => {
@ -532,10 +532,13 @@ export const useGeminiStream = (
getNextMessageId, getNextMessageId,
updateGeminiMessage, updateGeminiMessage,
handleSlashCommand, handleSlashCommand,
handleShellCommand,
// handleAtCommand is implicitly included via its direct call // handleAtCommand is implicitly included via its direct call
setDebugMessage, // Added dependency for handleAtCommand & passthrough setDebugMessage, // Added dependency for handleAtCommand & passthrough
setStreamingState, // Added dependency for handlePassthroughCommand setStreamingState, // Added dependency for handlePassthroughCommand
updateAndAddGeminiMessageContent, updateAndAddGeminiMessageContent,
setShowHelp,
toolRegistry,
], ],
); );

View File

@ -35,14 +35,14 @@ export const useThemeCommand = (
setIsThemeDialogOpen(true); setIsThemeDialogOpen(true);
}, []); }, []);
function applyTheme(themeName: string | undefined) { const applyTheme = useCallback((themeName: string | undefined) => {
try { try {
themeManager.setActiveTheme(themeName); themeManager.setActiveTheme(themeName);
setForceRender((v) => v + 1); // Trigger potential re-render setForceRender((v) => v + 1); // Trigger potential re-render
} catch (error) { } catch (error) {
console.error(`Error setting theme: ${error}`); console.error(`Error setting theme: ${error}`);
} }
} }, []);
const handleThemeHighlight = useCallback( const handleThemeHighlight = useCallback(
(themeName: string | undefined) => { (themeName: string | undefined) => {
@ -61,7 +61,7 @@ export const useThemeCommand = (
setIsThemeDialogOpen(false); // Close the dialog setIsThemeDialogOpen(false); // Close the dialog
} }
}, },
[applyTheme], // Added applyTheme to dependencies [applyTheme, loadedSettings],
); );
return { return {