/** * 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 { DataSpaceExecutableTemplate, type DataSpaceExecutable, DataSpacePackageableElementExecutable, } from '@finos/legend-extension-dsl-data-space/graph'; import { observer } from 'mobx-react-lite'; import { useEffect, useState } from 'react'; import { flowResult } from 'mobx'; import { dataSpace_addExecutable, dataSpace_removeExecutable, dataSpace_setExecutableDescription, dataSpace_setExecutableExecutionContextKey, dataSpace_setExecutableTitle, } from '../../stores/studio/DSL_DataSpace_GraphModifierHelper.js'; import { getElementIcon, RelationElementSampleValuesEditor, useEditorStore, } from '@finos/legend-application-studio'; import { prettyCONSTName, returnUndefOnError } from '@finos/legend-shared'; import { DataSpaceEditorState } from '../../stores/DataSpaceEditorState.js'; import { BlankPanelPlaceholder, clsx, CustomSelectorInput, Dialog, LongArrowRightIcon, ModalTitle, PanelContent, PanelDivider, PanelFormSection, PanelFormTextField, PanelFormValidatedTextField, PanelHeader, PencilEditIcon, PlusIcon, TimesIcon, } from '@finos/legend-art'; import { type PackageableElement, PackageableElementExplicitReference, } from '@finos/legend-graph'; import { InlineLambdaEditor } from '@finos/legend-query-builder'; import { stub_DataSpaceExecutableTemplateQuery, type DataSpaceExecutableTemplateLambdaState, } from '../../stores/DataSpaceExecutableTemplateState.js'; import { editDataSpaceExecutableTemplateInQueryBuilder } from '../DataSpaceExecutableQueryAction.js'; type NewExecutableKind = 'element' | 'inlineQuery'; const NewExecutableModal = observer( (props: { dataSpaceState: DataSpaceEditorState; isOpen: boolean; isReadOnly: boolean; onClose: () => void; onCreated: (executable: DataSpaceExecutable) => void; }) => { const { dataSpaceState, isOpen, isReadOnly, onClose, onCreated } = props; const dataSpace = dataSpaceState.dataSpace; const existingIds = (dataSpace.executables ?? []) .map((executable) => executable.id) .filter((executableId): executableId is string => Boolean(executableId)); const [kind, setKind] = useState('element'); const [selectedElement, setSelectedElement] = useState< PackageableElement | undefined >(undefined); const [title, setTitle] = useState(''); const [isTitleValid, setIsTitleValid] = useState(false); const [id, setId] = useState(''); const [isIdValid, setIsIdValid] = useState(false); const elementOptions = dataSpaceState.getDataSpaceExecutableOptions(); const closeModal = (): void => { setKind('element'); setSelectedElement(undefined); setTitle(''); setId(''); onClose(); }; const validateTitle = (val: string): string | undefined => val ? undefined : `Title can't be empty`; const validateId = (val: string): string | undefined => { if (!val) { return `Id can't be empty`; } if (existingIds.includes(val)) { return `Id '${val}' already exists`; } return undefined; }; const canCreate = kind === 'element' ? Boolean(selectedElement) && isTitleValid : isTitleValid && isIdValid; const create = (): void => { if (isReadOnly || !canCreate) { return; } if (kind === 'element' && selectedElement) { const executablePointer = new DataSpacePackageableElementExecutable(); executablePointer.executable = PackageableElementExplicitReference.create(selectedElement); executablePointer.title = title; dataSpace_addExecutable(dataSpace, executablePointer); onCreated(executablePointer); } else if (kind === 'inlineQuery') { const executableTemplate = new DataSpaceExecutableTemplate(); executableTemplate.id = id; executableTemplate.title = title; executableTemplate.query = stub_DataSpaceExecutableTemplateQuery(); dataSpace_addExecutable(dataSpace, executableTemplate); onCreated(executableTemplate); } closeModal(); }; return (
{ event.preventDefault(); create(); }} className="modal search-modal modal--dark" >
{kind === 'element' && ( <>
Element
{ const element = option?.value; setSelectedElement(element); if (element && !title) { setTitle(element.name); } }} value={ selectedElement ? { label: selectedElement.path, value: selectedElement, } : null } placeholder="Select a function or service..." darkMode={true} />
setTitle(value ?? '')} validate={validateTitle} onValidate={(issue): void => setIsTitleValid(!issue)} isReadOnly={isReadOnly} placeholder="Enter a title" fullWidth={true} /> )} {kind === 'inlineQuery' && ( <> setId(value ?? '')} validate={validateId} onValidate={(issue): void => setIsIdValid(!issue)} isReadOnly={isReadOnly} placeholder="Enter a unique id" fullWidth={true} /> setTitle(value ?? '')} validate={validateTitle} onValidate={(issue): void => setIsTitleValid(!issue)} isReadOnly={isReadOnly} placeholder="Enter a title" fullWidth={true} /> )}
); }, ); const ExecutableTemplateQueryEditor = observer( (props: { dataSpaceState: DataSpaceEditorState; executableTemplate: DataSpaceExecutableTemplate; isReadOnly: boolean; }) => { const { dataSpaceState, executableTemplate, isReadOnly } = props; const editorStore = dataSpaceState.editorStore; const applicationStore = editorStore.applicationStore; const lambdaState: DataSpaceExecutableTemplateLambdaState = dataSpaceState.executableTemplateStates.getLambdaState( executableTemplate, ); useEffect(() => { flowResult( lambdaState.convertLambdaObjectToGrammarString({ pretty: true }), ).catch(applicationStore.alertUnhandledError); }, [lambdaState, applicationStore]); const editInQueryBuilder = applicationStore.guardUnhandledError(async () => editDataSpaceExecutableTemplateInQueryBuilder( dataSpaceState.dataSpace, executableTemplate, lambdaState, editorStore, ), ); return (
Query
); }, ); const ExecutableSampleValuesEditor = observer( (props: { dataSpaceState: DataSpaceEditorState; executable: DataSpaceExecutable; isReadOnly: boolean; }) => { const { dataSpaceState, executable, isReadOnly } = props; const sampleValuesState = dataSpaceState.executableSampleValuesStates.getState(executable); return (
Sample Values
sampleValuesState.addSampleValues()} onDelete={() => sampleValuesState.removeSampleValues()} />
); }, ); const ExecutableConfigurationEditor = observer( (props: { dataSpaceState: DataSpaceEditorState; executable: DataSpaceExecutable; isReadOnly: boolean; }) => { const { dataSpaceState, executable, isReadOnly } = props; const editorStore = dataSpaceState.editorStore; const dataSpace = dataSpaceState.dataSpace; const executionContextOptions = (dataSpace.executionContexts ?? []).map( (context) => ({ label: context.name, value: context.name }), ); const executionContextKeyOption = executable.executionContextKey ? { label: executable.executionContextKey, value: executable.executionContextKey, } : null; return (
General
How this executable is shown to people browsing the data space.
dataSpace_setExecutableTitle(executable, value ?? '') } isReadOnly={isReadOnly} placeholder="Enter title" fullWidth={true} />