2024-07-29 16:55:48 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
import { TabList, TabPanel, Tabs } from 'react-tabs';
|
|
|
|
|
|
|
|
import BadgeHelp from '@/components/info/BadgeHelp';
|
|
|
|
import Modal from '@/components/ui/Modal';
|
|
|
|
import Overlay from '@/components/ui/Overlay';
|
|
|
|
import TabLabel from '@/components/ui/TabLabel';
|
|
|
|
import useRSFormCache from '@/hooks/useRSFormCache';
|
|
|
|
import { HelpTopic } from '@/models/miscellaneous';
|
2024-07-29 22:30:24 +03:00
|
|
|
import {
|
|
|
|
ICstSubstitute,
|
|
|
|
IOperation,
|
|
|
|
IOperationSchema,
|
|
|
|
IOperationUpdateData,
|
|
|
|
OperationID,
|
|
|
|
OperationType
|
|
|
|
} from '@/models/oss';
|
2024-07-29 16:55:48 +03:00
|
|
|
import { PARAMETER } from '@/utils/constants';
|
|
|
|
|
|
|
|
import TabArguments from './TabArguments';
|
|
|
|
import TabOperation from './TabOperation';
|
|
|
|
import TabSynthesis from './TabSynthesis';
|
|
|
|
|
|
|
|
interface DlgEditOperationProps {
|
|
|
|
hideWindow: () => void;
|
|
|
|
oss: IOperationSchema;
|
|
|
|
target: IOperation;
|
2024-07-29 22:30:24 +03:00
|
|
|
onSubmit: (data: IOperationUpdateData) => void;
|
2024-07-29 16:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export enum TabID {
|
|
|
|
CARD = 0,
|
|
|
|
ARGUMENTS = 1,
|
|
|
|
SUBSTITUTION = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
function DlgEditOperation({ hideWindow, oss, target, onSubmit }: DlgEditOperationProps) {
|
|
|
|
const [activeTab, setActiveTab] = useState(TabID.CARD);
|
|
|
|
|
|
|
|
const [alias, setAlias] = useState(target.alias);
|
|
|
|
const [title, setTitle] = useState(target.title);
|
|
|
|
const [comment, setComment] = useState(target.comment);
|
|
|
|
|
|
|
|
const [inputs, setInputs] = useState<OperationID[]>(oss.graph.expandInputs([target.id]));
|
|
|
|
const inputOperations = useMemo(() => inputs.map(id => oss.operationByID.get(id)!), [inputs, oss.operationByID]);
|
|
|
|
const schemasIDs = useMemo(
|
|
|
|
() => inputOperations.map(operation => operation.result).filter(id => id !== null),
|
|
|
|
[inputOperations]
|
|
|
|
);
|
2024-08-01 20:10:58 +03:00
|
|
|
const [substitutions, setSubstitutions] = useState<ICstSubstitute[]>(target.substitutions);
|
2024-07-29 16:55:48 +03:00
|
|
|
const cache = useRSFormCache();
|
2024-07-30 15:59:37 +03:00
|
|
|
const schemas = useMemo(
|
|
|
|
() => schemasIDs.map(id => cache.getSchema(id)).filter(item => item !== undefined),
|
|
|
|
[schemasIDs, cache]
|
|
|
|
);
|
2024-07-29 16:55:48 +03:00
|
|
|
|
|
|
|
const isValid = useMemo(() => alias !== '', [alias]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
cache.preload(schemasIDs);
|
|
|
|
}, [schemasIDs]);
|
|
|
|
|
2024-08-14 21:50:10 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (cache.loading || schemas.length !== schemasIDs.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setSubstitutions(prev =>
|
|
|
|
prev.filter(sub => {
|
|
|
|
const original = cache.getSchemaByCst(sub.original);
|
|
|
|
if (!original || !schemasIDs.includes(original.id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const substitution = cache.getSchemaByCst(sub.substitution);
|
|
|
|
if (!substitution || !schemasIDs.includes(substitution.id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}, [schemasIDs, schemas, cache.loading]);
|
|
|
|
|
2024-07-29 16:55:48 +03:00
|
|
|
const handleSubmit = () => {
|
2024-07-29 22:30:24 +03:00
|
|
|
const data: IOperationUpdateData = {
|
|
|
|
target: target.id,
|
|
|
|
item_data: {
|
|
|
|
alias: alias,
|
|
|
|
title: title,
|
2024-07-30 15:59:37 +03:00
|
|
|
comment: comment
|
2024-07-29 22:30:24 +03:00
|
|
|
},
|
|
|
|
positions: [],
|
|
|
|
arguments: target.operation_type !== OperationType.SYNTHESIS ? undefined : inputs,
|
|
|
|
substitutions: target.operation_type !== OperationType.SYNTHESIS ? undefined : substitutions
|
|
|
|
};
|
|
|
|
onSubmit(data);
|
2024-07-29 16:55:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const cardPanel = useMemo(
|
|
|
|
() => (
|
|
|
|
<TabPanel>
|
|
|
|
<TabOperation
|
|
|
|
alias={alias}
|
|
|
|
setAlias={setAlias}
|
|
|
|
comment={comment}
|
|
|
|
setComment={setComment}
|
|
|
|
title={title}
|
|
|
|
setTitle={setTitle}
|
|
|
|
/>
|
|
|
|
</TabPanel>
|
|
|
|
),
|
2024-08-02 11:17:27 +03:00
|
|
|
[alias, comment, title, setAlias]
|
2024-07-29 16:55:48 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const argumentsPanel = useMemo(
|
|
|
|
() => (
|
|
|
|
<TabPanel>
|
|
|
|
<TabArguments
|
|
|
|
target={target.id} // prettier: split-lines
|
|
|
|
oss={oss}
|
|
|
|
inputs={inputs}
|
|
|
|
setInputs={setInputs}
|
|
|
|
/>
|
|
|
|
</TabPanel>
|
|
|
|
),
|
2024-08-02 11:17:27 +03:00
|
|
|
[oss, target, inputs, setInputs]
|
2024-07-29 16:55:48 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const synthesisPanel = useMemo(
|
|
|
|
() => (
|
|
|
|
<TabPanel>
|
|
|
|
<TabSynthesis
|
2024-07-30 15:59:37 +03:00
|
|
|
schemas={schemas}
|
2024-07-29 16:55:48 +03:00
|
|
|
loading={cache.loading}
|
|
|
|
error={cache.error}
|
|
|
|
substitutions={substitutions}
|
|
|
|
setSubstitutions={setSubstitutions}
|
|
|
|
/>
|
|
|
|
</TabPanel>
|
|
|
|
),
|
2024-07-30 15:59:37 +03:00
|
|
|
[cache.loading, cache.error, substitutions, schemas]
|
2024-07-29 16:55:48 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
header='Редактирование операции'
|
|
|
|
submitText='Сохранить'
|
|
|
|
hideWindow={hideWindow}
|
|
|
|
canSubmit={isValid}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
className='w-[40rem] px-6 min-h-[35rem]'
|
|
|
|
>
|
|
|
|
<Overlay position='top-0 right-0'>
|
|
|
|
<BadgeHelp topic={HelpTopic.CC_OSS} className={clsx(PARAMETER.TOOLTIP_WIDTH, 'sm:max-w-[40rem]')} offset={14} />
|
|
|
|
</Overlay>
|
|
|
|
|
|
|
|
<Tabs
|
|
|
|
selectedTabClassName='clr-selected'
|
|
|
|
className='flex flex-col'
|
|
|
|
selectedIndex={activeTab}
|
|
|
|
onSelect={setActiveTab}
|
|
|
|
>
|
|
|
|
<TabList className={clsx('mb-3 self-center', 'flex', 'border divide-x rounded-none')}>
|
|
|
|
<TabLabel title='Текстовые поля' label='Карточка' className='w-[8rem]' />
|
|
|
|
{target.operation_type === OperationType.SYNTHESIS ? (
|
|
|
|
<TabLabel title='Выбор аргументов операции' label='Аргументы' className='w-[8rem]' />
|
|
|
|
) : null}
|
|
|
|
{target.operation_type === OperationType.SYNTHESIS ? (
|
|
|
|
<TabLabel title='Таблица отождествлений' label='Отождествления' className='w-[8rem]' />
|
|
|
|
) : null}
|
|
|
|
</TabList>
|
|
|
|
|
|
|
|
{cardPanel}
|
|
|
|
{target.operation_type === OperationType.SYNTHESIS ? argumentsPanel : null}
|
|
|
|
{target.operation_type === OperationType.SYNTHESIS ? synthesisPanel : null}
|
|
|
|
</Tabs>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DlgEditOperation;
|