import { type Dispatch, type SetStateAction } from 'react'; import { type IConfiguration } from '../../../Interfaces/IConfiguration'; import { FetchConfiguration } from '../../API/api'; import { type IEditorState } from '../../../Interfaces/IEditorState'; import { DISABLE_API, GetDefaultEditorState } from '../../../utils/default'; import { type AppState } from '../../../Enums/AppState'; import { LoadState } from './Load'; export function NewEditor( editorState: IEditorState, setEditorState: (newState: IEditorState) => void, enableLoaded: () => void ): void { if (DISABLE_API) { enableLoaded(); } if (editorState.configuration.APIConfiguration !== undefined) { enableLoaded(); return; } // Fetch the configuration from the API FetchConfiguration() .then((configuration: IConfiguration) => { // Set the editor from the given properties of the API const editorState: IEditorState = GetDefaultEditorState(configuration); setEditorState(editorState); enableLoaded(); }, (error) => { console.debug('[NewEditor] Could not fetch resource from API. Using default.', error); enableLoaded(); }); } export function LoadEditor( files: FileList | null, setEditorState: Dispatch>, setAppState: Dispatch> ): void { if (files === null) { return; } const file = files[0]; const reader = new FileReader(); reader.addEventListener('load', () => { const result = reader.result as string; const editorState: IEditorState = JSON.parse(result); LoadState(editorState, setEditorState, setAppState); }); reader.readAsText(file); }