( loadedSettings: LoadedSettings, setEditorError: (error: string | null) => void, addItem: (item: Omit<HistoryItem, 'id'>, timestamp: number) => void, )
| 25 | } |
| 26 | |
| 27 | export const useEditorSettings = ( |
| 28 | loadedSettings: LoadedSettings, |
| 29 | setEditorError: (error: string | null) => void, |
| 30 | addItem: (item: Omit<HistoryItem, 'id'>, timestamp: number) => void, |
| 31 | ): UseEditorSettingsReturn => { |
| 32 | const [isEditorDialogOpen, setIsEditorDialogOpen] = useState(false); |
| 33 | |
| 34 | const openEditorDialog = useCallback(() => { |
| 35 | setIsEditorDialogOpen(true); |
| 36 | }, []); |
| 37 | |
| 38 | const handleEditorSelect = useCallback( |
| 39 | (editorType: EditorType | undefined, scope: SettingScope) => { |
| 40 | if ( |
| 41 | editorType && |
| 42 | (!checkHasEditorType(editorType) || |
| 43 | !allowEditorTypeInSandbox(editorType)) |
| 44 | ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | loadedSettings.setValue(scope, 'preferredEditor', editorType); |
| 50 | addItem( |
| 51 | { |
| 52 | type: MessageType.INFO, |
| 53 | text: `Editor preference ${editorType ? `set to "${editorType}"` : 'cleared'} in ${scope} settings.`, |
| 54 | }, |
| 55 | Date.now(), |
| 56 | ); |
| 57 | setEditorError(null); |
| 58 | setIsEditorDialogOpen(false); |
| 59 | } catch (error) { |
| 60 | setEditorError(`Failed to set editor preference: ${error}`); |
| 61 | } |
| 62 | }, |
| 63 | [loadedSettings, setEditorError, addItem], |
| 64 | ); |
| 65 | |
| 66 | const exitEditorDialog = useCallback(() => { |
| 67 | setIsEditorDialogOpen(false); |
| 68 | }, []); |
| 69 | |
| 70 | return { |
| 71 | isEditorDialogOpen, |
| 72 | openEditorDialog, |
| 73 | handleEditorSelect, |
| 74 | exitEditorDialog, |
| 75 | }; |
| 76 | }; |
no test coverage detected