qwen-code/packages/cli/src/ui/hooks/useEditorSettings.ts

81 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-06-12 02:21:54 +01:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { useState, useCallback, useContext } from 'react';
import { SettingScope } from '../../config/settings.js';
2025-06-12 02:21:54 +01:00
import { type HistoryItem, MessageType } from '../types.js';
import {
allowEditorTypeInSandbox,
checkHasEditorType,
EditorType,
} from '@google/gemini-cli-core';
import { SettingsContext } from '../contexts/SettingsContext.js';
2025-06-12 02:21:54 +01:00
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<HistoryItem, 'id'>, timestamp: number) => void,
): UseEditorSettingsReturn => {
const [isEditorDialogOpen, setIsEditorDialogOpen] = useState(false);
const settingsContext = useContext(SettingsContext);
2025-06-12 02:21:54 +01:00
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,
);
2025-06-12 02:21:54 +01:00
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],
2025-06-12 02:21:54 +01:00
);
const exitEditorDialog = useCallback(() => {
setIsEditorDialogOpen(false);
}, []);
return {
isEditorDialogOpen,
openEditorDialog,
handleEditorSelect,
exitEditorDialog,
};
};