@finos/legend-studio
Version:
96 lines • 9.28 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
* 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 { ModelLoaderState, MODEL_UPDATER_INPUT_TYPE, } from '../../../stores/editor-state/ModelLoaderState.js';
import { prettyCONSTName } from '@finos/legend-shared';
import { DropdownMenu, MenuContent, MenuContentItem, CaretDownIcon, CheckSquareIcon, TruckLoadingIcon, EmptySquareIcon, } from '@finos/legend-art';
import { flowResult } from 'mobx';
import { useEditorStore } from '../EditorStoreProvider.js';
import { ActionAlertType, ActionAlertActionType, useApplicationStore, EDITOR_LANGUAGE, useApplicationNavigationContext, } from '@finos/legend-application';
import { StudioTextInputEditor } from '../../shared/StudioTextInputEditor.js';
import { LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY } from '../../../stores/LegendStudioApplicationNavigationContext.js';
export const ModelLoader = observer(() => {
const editorStore = useEditorStore();
const applicationStore = useApplicationStore();
const modelLoaderState = editorStore.getCurrentEditorState(ModelLoaderState);
const nativeInputTypes = Object.values(MODEL_UPDATER_INPUT_TYPE);
const externalFormatInputTypes = modelLoaderState.modelImportDescriptions;
const extraModelLoaderExtensionsConfigs = modelLoaderState.modelLoaderExtensionConfigurations;
// input type
const currentModelLoadType = modelLoaderState.currentModelLoadType;
// replace flag
const replace = modelLoaderState.replace;
const toggleReplace = () => modelLoaderState.setReplaceFlag(!replace);
// actions
const loadCurrentProjectEntities = applicationStore.guardUnhandledError(() => flowResult(modelLoaderState.loadCurrentProjectEntities()));
const setCurrentInputType = (inputType) => () => {
modelLoaderState.setCurrentModelLoadType(inputType);
};
const loaderExtensionConfig = modelLoaderState.getLoaderExtensionConfiguration(currentModelLoadType);
const label = modelLoaderState.getImportConfigurationDescription(currentModelLoadType)
?.label ??
loaderExtensionConfig?.modelGenerationConfig.label ??
loaderExtensionConfig?.modelGenerationConfig.key ??
currentModelLoadType;
const allowHardReplace = !loaderExtensionConfig || loaderExtensionConfig.allowHardReplace;
const isNativeInput = Object.values(MODEL_UPDATER_INPUT_TYPE).includes(currentModelLoadType);
const loadModel = () => {
if (loaderExtensionConfig) {
loaderExtensionConfig
.load(editorStore)
.catch(applicationStore.alertUnhandledError);
}
else if (editorStore.hasUnpushedChanges) {
editorStore.setActionAlertInfo({
message: 'You have unpushed changes',
prompt: 'This action will discard these changes and refresh the application',
type: ActionAlertType.CAUTION,
onEnter: () => editorStore.setBlockGlobalHotkeys(true),
onClose: () => editorStore.setBlockGlobalHotkeys(false),
actions: [
{
label: 'Proceed to load model',
type: ActionAlertActionType.PROCEED_WITH_CAUTION,
handler: () => {
editorStore.setIgnoreNavigationBlocking(true);
flowResult(modelLoaderState.loadModel()).catch(applicationStore.alertUnhandledError);
},
},
{
label: 'Abort',
type: ActionAlertActionType.PROCEED,
default: true,
},
],
});
}
else {
flowResult(modelLoaderState.loadModel()).catch(applicationStore.alertUnhandledError);
}
};
const updateModel = (val) => modelLoaderState.setModelText(val);
useApplicationNavigationContext(LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY.MODEL_LOADER);
return (_jsxs("div", { className: "panel model-loader", children: [_jsxs("div", { className: "panel__header model-loader__header", children: [_jsxs("div", { className: "model-loader__header__configs", children: [_jsx(DropdownMenu, { content: _jsxs(MenuContent, { className: "model-loader__header__configs__type__menu", children: [_jsxs("div", { className: "model-loader__header__configs__type-option__group model-loader__header__configs__type-option__group--native", children: [_jsx("div", { className: "model-loader__header__configs__type-option__group__name", children: "native" }), _jsx("div", { className: "model-loader__header__configs__type-option__group__options", children: nativeInputTypes.map((inputType) => (_jsx(MenuContentItem, { className: "model-loader__header__configs__type-option__group__option", onClick: setCurrentInputType(inputType), children: prettyCONSTName(inputType) }, inputType))) })] }), Boolean(externalFormatInputTypes) && (_jsxs(_Fragment, { children: [_jsx("div", { className: "model-loader__header__configs__type-option__group__separator" }), _jsxs("div", { className: "model-loader__header__configs__type-option__group model-loader__header__configs__type-option__group--external", children: [_jsx("div", { className: "model-loader__header__configs__type-option__group__name", children: "external" }), _jsx("div", { className: "model-loader__header__configs__type-option__group__options", children: externalFormatInputTypes.map((inputType) => (_jsx(MenuContentItem, { className: "model-loader__header__configs__type-option__group__option", onClick: setCurrentInputType(inputType.key), children: inputType.label }, inputType.key))) })] })] })), Boolean(extraModelLoaderExtensionsConfigs.length > 0) && (_jsxs(_Fragment, { children: [_jsx("div", { className: "model-loader__header__configs__type-option__group__separator" }), _jsxs("div", { className: "model-loader__header__configs__type-option__group model-loader__header__configs__type-option__group--extension", children: [_jsx("div", { className: "model-loader__header__configs__type-option__group__name", children: "extensions" }), _jsx("div", { className: "model-loader__header__configs__type-option__group__options", children: extraModelLoaderExtensionsConfigs.map((config) => (_jsx(MenuContentItem, { className: "model-loader__header__configs__type-option__group__option", onClick: setCurrentInputType(config.modelGenerationConfig.key), children: config.modelGenerationConfig.label ??
prettyCONSTName(config.modelGenerationConfig.key) }, config.modelGenerationConfig.key))) })] })] }))] }), menuProps: {
anchorOrigin: { vertical: 'bottom', horizontal: 'right' },
transformOrigin: { vertical: 'top', horizontal: 'right' },
}, children: _jsxs("div", { className: "model-loader__header__configs__type", children: [_jsx("div", { className: "model-loader__header__configs__type__label", children: prettyCONSTName(label) }), _jsx("div", { className: "model-loader__header__configs__type__icon", children: _jsx(CaretDownIcon, {}) })] }) }), allowHardReplace && (_jsxs("div", { className: "model-loader__header__configs__edit-mode", onClick: toggleReplace, children: [_jsx("div", { className: "model-loader__header__configs__edit-mode__icon", children: replace ? _jsx(CheckSquareIcon, {}) : _jsx(EmptySquareIcon, {}) }), _jsx("div", { className: "model-loader__header__configs__edit-mode__label", children: "replace" })] })), isNativeInput && (_jsx("button", { className: "model-loader__header__configs__load-project-entities-btn", tabIndex: -1, onClick: loadCurrentProjectEntities, title: "Load current project entities", children: _jsx(TruckLoadingIcon, {}) }))] }), _jsx("div", { className: "model-loader__header__action", children: _jsx("button", { className: "btn--dark model-loader__header__load-btn", onClick: loadModel, disabled: modelLoaderState.isLoadingModel, tabIndex: -1, title: "Load model", children: "Load" }) })] }), _jsx("div", { className: "panel__content model-loader__editor", children: loaderExtensionConfig?.renderer(editorStore) ?? (_jsx(StudioTextInputEditor, { language: modelLoaderState.currentModelLoadType ===
MODEL_UPDATER_INPUT_TYPE.PURE_GRAMMAR
? EDITOR_LANGUAGE.PURE
: EDITOR_LANGUAGE.JSON, inputValue: modelLoaderState.modelText, updateInput: updateModel, showMiniMap: true })) })] }));
});
//# sourceMappingURL=ModelLoader.js.map