/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { observer } from 'mobx-react-lite'; import { type ModelImporterEditorState, ModelImporterState, ExtensionModelImporterEditorState, MODEL_IMPORT_NATIVE_INPUT_TYPE, NativeModelImporterEditorState, ExternalFormatModelImporterState, ExecuteInputDebugModelImporterEditorState, } from '../../../stores/editor/editor-state/ModelImporterState.js'; import { prettyCONSTName } from '@finos/legend-shared'; import { ControlledDropdownMenu, MenuContent, MenuContentItem, CaretDownIcon, CheckSquareIcon, TruckLoadingIcon, EmptySquareIcon, BlankPanelContent, PanelLoadingIndicator, clsx, Button, Panel, PanelHeader, PanelContent, } from '@finos/legend-art'; import { flowResult } from 'mobx'; import { useEditorStore } from '../EditorStoreProvider.js'; import { useApplicationStore, useApplicationNavigationContext, } from '@finos/legend-application'; import type { ModelImporterExtensionConfiguration } from '../../../stores/LegendStudioApplicationPlugin.js'; import { LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY } from '../../../__lib__/LegendStudioApplicationNavigationContext.js'; import { SCHEMA_SET_TAB_TYPE } from '../../../stores/editor/editor-state/element-editor-state/external-format/DSL_ExternalFormat_SchemaSetEditorState.js'; import { SchemaSetModelGenerationEditor } from './external-format-editor/DSL_ExternalFormat_SchemaSetModelGenerationEditor.js'; import { SchemaSetGeneralEditor } from './external-format-editor/DSL_ExternalFormat_SchemaSetElementEditor.js'; import { CODE_EDITOR_LANGUAGE } from '@finos/legend-code-editor'; import { CodeEditor } from '@finos/legend-lego/code-editor'; const ExternalFormatModelImporterEditor = observer( (props: { externalFormatState: ExternalFormatModelImporterState }) => { const { externalFormatState } = props; const schemaSetEditorState = externalFormatState.schemaSetEditorState; const editorStore = schemaSetEditorState.editorStore; const schemaSet = schemaSetEditorState.schemaSet; const currentTab = schemaSetEditorState.selectedTab; const isFetchingDescriptions = editorStore.graphState.graphGenerationState.externalFormatState .fetchingDescriptionsState.isInProgress; const changeTab = (tab: SCHEMA_SET_TAB_TYPE): (() => void) => (): void => schemaSetEditorState.setSelectedTab(tab); const renderMainEditPanel = (): React.ReactNode => { if (isFetchingDescriptions) { return ( Fetching format descriptions ); } if (currentTab === SCHEMA_SET_TAB_TYPE.SCHEMAS) { return ( ); } const supportsModelGeneraiton = schemaSetEditorState.schemaSetModelGenerationState.description ?.supportsModelGeneration; return supportsModelGeneraiton ? ( ) : ( Format {schemaSet.format} does not support Model Generation ); }; useApplicationNavigationContext( LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY.MODEL_LOADER_EXTERNAL_FORMAT_IMPORTER, ); return (
{Object.values(SCHEMA_SET_TAB_TYPE).map((tab) => (
{prettyCONSTName(tab)}
))}
{currentTab === SCHEMA_SET_TAB_TYPE.SCHEMAS && ( )} {currentTab === SCHEMA_SET_TAB_TYPE.GENERATE_MODEL && renderMainEditPanel()}
); }, ); export const ModelImporter = observer(() => { const editorStore = useEditorStore(); const applicationStore = useApplicationStore(); const modelImporterState = editorStore.tabManagerState.getCurrentEditorState(ModelImporterState); const nativeInputTypes = Object.values(MODEL_IMPORT_NATIVE_INPUT_TYPE); const extraModelImporterExtensionsConfigs = modelImporterState.extensionConfigs; const externalFormatDescriptions = modelImporterState.editorStore.graphState.graphGenerationState .externalFormatState.externalFormatDescriptionsWithModelGenerationSupport; // replace flag const replace = modelImporterState.replace; const toggleReplace = (): void => modelImporterState.setReplaceFlag(!replace); const label = modelImporterState.modelImportEditorState.label; const modelImportEditorState = modelImporterState.modelImportEditorState; const loadModel = (): void => { editorStore.localChangesState.alertUnsavedChanges((): void => { modelImporterState.modelImportEditorState .loadModel() .catch(applicationStore.alertUnhandledError); }); }; useApplicationNavigationContext( LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY.MODEL_LOADER, ); const renderExtraActions = (): React.ReactNode => { if (modelImportEditorState instanceof NativeModelImporterEditorState) { // actions const loadCurrentProjectEntities = applicationStore.guardUnhandledError( () => flowResult(modelImportEditorState.loadCurrentProjectEntities()), ); return (
Load from Graph
); } return null; }; const renderModelImporterEditor = (): React.ReactNode => { if (modelImportEditorState instanceof ExtensionModelImporterEditorState) { return modelImportEditorState.config.renderer( modelImportEditorState.rendererState, ); } else if ( modelImportEditorState instanceof NativeModelImporterEditorState ) { const updateModel = (val: string): void => modelImportEditorState.setModelText(val); return ( ); } else if ( modelImportEditorState instanceof ExternalFormatModelImporterState ) { return ( ); } else if ( modelImportEditorState instanceof ExecuteInputDebugModelImporterEditorState ) { return (
Debug Executable:
modelImportEditorState.setExecutablePath(event.target.value) } />
modelImportEditorState.setExecuteInput(val)} />
); } return null; }; return (
native
{nativeInputTypes.map((inputType) => ( modelImporterState.setNativeImportType(inputType) } > {prettyCONSTName(inputType)} ))} modelImporterState.setExecuteInputDebugModelImporter() } > {`Execute Input (DEBUG)`}
{Boolean(externalFormatDescriptions.length) && ( <>
external
{externalFormatDescriptions.map((inputType) => ( modelImporterState.setExternalFormatImportFormat( inputType, ) } > {inputType.name} ))}
)} {Boolean(extraModelImporterExtensionsConfigs.length > 0) && ( <>
extensions
{extraModelImporterExtensionsConfigs.map( (config: ModelImporterExtensionConfiguration) => ( modelImporterState.setModelImporterExtension( config, ) } > {config.label ?? prettyCONSTName(config.key)} ), )}
)} } menuProps={{ anchorOrigin: { vertical: 'bottom', horizontal: 'right' }, transformOrigin: { vertical: 'top', horizontal: 'right' }, }} >
{prettyCONSTName(label)}
{modelImportEditorState.allowHardReplace && (
{replace ? : }
replace
)} {renderExtraActions()}
{renderModelImporterEditor()} ); });