@finos/legend-studio
Version:
118 lines • 5.87 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } 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 { useRef } from 'react';
import { observer } from 'mobx-react-lite';
import { Dialog, CustomSelectorInput, createFilter, } from '@finos/legend-art';
import { getMappingElementSource, } from '../../../../stores/editor-state/element-editor-state/mapping/MappingEditorState.js';
import { Class, RootFlatDataRecordType, Table, DEFAULT_DATABASE_SCHEMA_NAME, TableAlias, TableExplicitReference, ViewExplicitReference, getAllRecordTypes, } from '@finos/legend-graph';
import { UnsupportedOperationError } from '@finos/legend-shared';
import { flowResult } from 'mobx';
import { useEditorStore } from '../../EditorStoreProvider.js';
import { useApplicationStore, buildElementOption, } from '@finos/legend-application';
export const getMappingElementSourceFilterText = (option) => {
const val = option.value;
if (val instanceof Class) {
return val.path;
}
else if (val instanceof RootFlatDataRecordType) {
return val._OWNER.name;
}
else if (val instanceof TableAlias) {
return `${val.relation.ownerReference.value.path}.${val.relation.value.schema.name}.${val.relation.value.name}`;
}
throw new UnsupportedOperationError();
};
export const getSourceElementLabel = (srcElement) => {
let sourceLabel = '(none)';
if (srcElement instanceof Class) {
sourceLabel = srcElement.name;
}
else if (srcElement instanceof RootFlatDataRecordType) {
sourceLabel = srcElement._OWNER.name;
}
else if (srcElement instanceof TableAlias) {
sourceLabel = `${srcElement.relation.ownerReference.value.name}.${srcElement.relation.value.schema.name === DEFAULT_DATABASE_SCHEMA_NAME
? ''
: `${srcElement.relation.value.schema.name}.`}${srcElement.relation.value.name}`;
}
return sourceLabel;
};
// TODO: add more visual cue to the type of source (class vs. flat-data vs. db)
export const buildMappingElementSourceOption = (source) => {
if (source instanceof Class) {
return buildElementOption(source);
}
else if (source instanceof RootFlatDataRecordType) {
return {
label: `${source._OWNER._OWNER.name}.${source._OWNER.name}`,
value: source,
};
}
else if (source instanceof TableAlias) {
return {
label: `${source.relation.ownerReference.value.name}.${source.relation.value.schema.name === DEFAULT_DATABASE_SCHEMA_NAME
? ''
: `${source.relation.value.schema.name}.`}${source.relation.value.name}`,
value: source,
};
}
return null;
};
export const InstanceSetImplementationSourceSelectorModal = observer((props) => {
const { mappingEditorState, setImplementation, closeModal, sourceElementToSelect, } = props;
const editorStore = useEditorStore();
const applicationStore = useApplicationStore();
const options = editorStore.graphManagerState.graph.ownClasses
.concat(editorStore.graphManagerState.graph.dependencyManager
.classes)
.concat(editorStore.graphManagerState.graph.ownFlatDatas.flatMap(getAllRecordTypes))
.concat(editorStore.graphManagerState.graph.ownDatabases
.concat(editorStore.graphManagerState.graph.dependencyManager.databases)
.flatMap((e) => e.schemas.flatMap((schema) => schema.tables.concat(schema.views)))
.map((relation) => {
const mainTableAlias = new TableAlias();
mainTableAlias.relation =
relation instanceof Table
? TableExplicitReference.create(relation)
: ViewExplicitReference.create(relation);
mainTableAlias.name = mainTableAlias.relation.value.name;
return mainTableAlias;
}))
.map(buildMappingElementSourceOption);
const filterOption = createFilter({
ignoreCase: true,
ignoreAccents: false,
stringify: getMappingElementSourceFilterText,
});
const sourceSelectorRef = useRef(null);
const selectedSourceType = buildMappingElementSourceOption(sourceElementToSelect ??
getMappingElementSource(setImplementation, editorStore.pluginManager.getApplicationPlugins()));
const changeSourceType = (val) => flowResult(mappingEditorState.changeClassMappingSourceDriver(setImplementation, val?.value))
.then(() => closeModal())
.catch(applicationStore.alertUnhandledError);
const handleEnter = () => sourceSelectorRef.current?.focus();
return (_jsx(Dialog, { open: true, onClose: closeModal, TransitionProps: {
onEnter: handleEnter,
}, classes: {
container: 'search-modal__container',
}, PaperProps: {
classes: {
root: 'search-modal__inner-container',
},
}, children: _jsxs("div", { className: "modal search-modal", children: [_jsx("div", { className: "modal__title", children: "Choose a Source" }), _jsx(CustomSelectorInput, { ref: sourceSelectorRef, options: options, onChange: changeSourceType, value: selectedSourceType, placeholder: `Select a source...`, isClearable: true, filterOption: filterOption })] }) }));
});
//# sourceMappingURL=InstanceSetImplementationSourceSelectorModal.js.map