/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { useState, useCallback, useContext } from 'react'; import { SettingScope } from '../../config/settings.js'; import { type HistoryItem, MessageType } from '../types.js'; import { allowEditorTypeInSandbox, checkHasEditorType, EditorType, } from '@google/gemini-cli-core'; import { SettingsContext } from '../contexts/SettingsContext.js'; interface UseEditorSettingsReturn { isEditorDialogOpen: boolean; openEditorDialog: () => void; handleEditorSelect: ( editorType: EditorType | undefined, scope: SettingScope, ) => void; exitEditorDialog: () => void; } export const useEditorSettings = ( setEditorError: (error: string | null) => void, addItem: (item: Omit, timestamp: number) => void, ): UseEditorSettingsReturn => { const [isEditorDialogOpen, setIsEditorDialogOpen] = useState(false); const settingsContext = useContext(SettingsContext); const openEditorDialog = useCallback(() => { setIsEditorDialogOpen(true); }, []); const handleEditorSelect = useCallback( (editorType: EditorType | undefined, scope: SettingScope) => { if ( editorType && (!checkHasEditorType(editorType) || !allowEditorTypeInSandbox(editorType)) ) { return; } try { settingsContext?.settings.setValue( scope, 'preferredEditor', editorType, ); addItem( { type: MessageType.INFO, text: `Editor preference ${editorType ? `set to "${editorType}"` : 'cleared'} in ${scope} settings.`, }, Date.now(), ); setEditorError(null); setIsEditorDialogOpen(false); } catch (error) { setEditorError(`Failed to set editor preference: ${error}`); } }, [settingsContext, setEditorError, addItem], ); const exitEditorDialog = useCallback(() => { setIsEditorDialogOpen(false); }, []); return { isEditorDialogOpen, openEditorDialog, handleEditorSelect, exitEditorDialog, }; };