|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + EDITOR_DISPLAY_NAMES, |
| 9 | + type EditorType, |
| 10 | + coreEvents, |
| 11 | + isEditorAvailable, |
| 12 | +} from '@google/gemini-cli-core'; |
| 13 | +import { useCallback, useEffect, useMemo, useState } from 'react'; |
| 14 | +import { |
| 15 | + SettingScope, |
| 16 | + type LoadableSettingScope, |
| 17 | + type LoadedSettings, |
| 18 | +} from '../../config/settings.js'; |
| 19 | +import { |
| 20 | + editorSettingsManager, |
| 21 | + type EditorDisplay, |
| 22 | +} from '../editors/editorSettingsManager.js'; |
| 23 | +import { useKeypress } from './useKeypress.js'; |
| 24 | + |
| 25 | +type FocusedSection = 'editor' | 'scope'; |
| 26 | + |
| 27 | +const SCOPE_ITEMS: Array<{ |
| 28 | + label: string; |
| 29 | + value: LoadableSettingScope; |
| 30 | + key: string; |
| 31 | +}> = [ |
| 32 | + { |
| 33 | + label: 'User Settings', |
| 34 | + value: SettingScope.User, |
| 35 | + key: SettingScope.User, |
| 36 | + }, |
| 37 | + { |
| 38 | + label: 'Workspace Settings', |
| 39 | + value: SettingScope.Workspace, |
| 40 | + key: SettingScope.Workspace, |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +export function useEditorSettingsDialog( |
| 45 | + settings: LoadedSettings, |
| 46 | + onSelect: ( |
| 47 | + editorType: EditorType | undefined, |
| 48 | + scope: LoadableSettingScope, |
| 49 | + ) => void, |
| 50 | + onExit: () => void, |
| 51 | +) { |
| 52 | + const [selectedScope, setSelectedScope] = useState<LoadableSettingScope>( |
| 53 | + SettingScope.User, |
| 54 | + ); |
| 55 | + const [focusedSection, setFocusedSection] = |
| 56 | + useState<FocusedSection>('editor'); |
| 57 | + |
| 58 | + useKeypress( |
| 59 | + (key) => { |
| 60 | + if (key.name === 'tab') { |
| 61 | + setFocusedSection((prev) => (prev === 'editor' ? 'scope' : 'editor')); |
| 62 | + } |
| 63 | + if (key.name === 'escape') { |
| 64 | + onExit(); |
| 65 | + } |
| 66 | + }, |
| 67 | + { isActive: true }, |
| 68 | + ); |
| 69 | + |
| 70 | + const editorItems: EditorDisplay[] = useMemo( |
| 71 | + () => editorSettingsManager.getAvailableEditorDisplays(), |
| 72 | + [], |
| 73 | + ); |
| 74 | + |
| 75 | + const currentPreference = |
| 76 | + settings.forScope(selectedScope).settings.general?.preferredEditor; |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + if (!currentPreference) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const isInvalid = |
| 84 | + editorItems.findIndex( |
| 85 | + (item: EditorDisplay) => item.type === currentPreference, |
| 86 | + ) === -1; |
| 87 | + |
| 88 | + if (isInvalid) { |
| 89 | + coreEvents.emitFeedback( |
| 90 | + 'error', |
| 91 | + `Editor is not supported: ${currentPreference}`, |
| 92 | + ); |
| 93 | + onSelect(undefined, selectedScope); |
| 94 | + } |
| 95 | + }, [currentPreference, editorItems, onSelect, selectedScope]); |
| 96 | + |
| 97 | + const editorIndex = useMemo( |
| 98 | + () => |
| 99 | + Math.max( |
| 100 | + 0, |
| 101 | + currentPreference |
| 102 | + ? editorItems.findIndex( |
| 103 | + (item: EditorDisplay) => item.type === currentPreference, |
| 104 | + ) |
| 105 | + : 0, |
| 106 | + ), |
| 107 | + [currentPreference, editorItems], |
| 108 | + ); |
| 109 | + |
| 110 | + const handleEditorSelect = useCallback( |
| 111 | + (editorType: EditorType | 'not_set') => { |
| 112 | + if (editorType === 'not_set') { |
| 113 | + onSelect(undefined, selectedScope); |
| 114 | + return; |
| 115 | + } |
| 116 | + onSelect(editorType, selectedScope); |
| 117 | + }, |
| 118 | + [onSelect, selectedScope], |
| 119 | + ); |
| 120 | + |
| 121 | + const handleScopeSelect = useCallback((scope: LoadableSettingScope) => { |
| 122 | + setSelectedScope(scope); |
| 123 | + setFocusedSection('editor'); |
| 124 | + }, []); |
| 125 | + |
| 126 | + const otherScopeModifiedMessage = useMemo(() => { |
| 127 | + const otherScope = |
| 128 | + selectedScope === SettingScope.User |
| 129 | + ? SettingScope.Workspace |
| 130 | + : SettingScope.User; |
| 131 | + if ( |
| 132 | + settings.forScope(otherScope).settings.general?.preferredEditor !== |
| 133 | + undefined |
| 134 | + ) { |
| 135 | + return settings.forScope(selectedScope).settings.general |
| 136 | + ?.preferredEditor !== undefined |
| 137 | + ? `(Also modified in ${otherScope})` |
| 138 | + : `(Modified in ${otherScope})`; |
| 139 | + } |
| 140 | + return ''; |
| 141 | + }, [selectedScope, settings]); |
| 142 | + |
| 143 | + const mergedEditorName = useMemo(() => { |
| 144 | + const { preferredEditor } = settings.merged.general ?? {}; |
| 145 | + if (preferredEditor && isEditorAvailable(preferredEditor)) { |
| 146 | + return EDITOR_DISPLAY_NAMES[preferredEditor as EditorType]; |
| 147 | + } |
| 148 | + return 'None'; |
| 149 | + }, [settings.merged.general]); |
| 150 | + |
| 151 | + return { |
| 152 | + selectedScope, |
| 153 | + focusedSection, |
| 154 | + editorItems, |
| 155 | + editorIndex, |
| 156 | + scopeItems: SCOPE_ITEMS, |
| 157 | + handleEditorSelect, |
| 158 | + handleScopeSelect, |
| 159 | + otherScopeModifiedMessage, |
| 160 | + mergedEditorName, |
| 161 | + }; |
| 162 | +} |
0 commit comments