ConceptPortal-public/rsconcept/frontend/src/dialogs/DlgCreateOperation/DlgCreateOperation.tsx

159 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-07-21 15:19:57 +03:00
'use client';
import clsx from 'clsx';
import { useLayoutEffect, 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 { useLibrary } from '@/context/LibraryContext';
import { LibraryItemID } from '@/models/library';
2024-07-29 16:56:24 +03:00
import { HelpTopic } from '@/models/miscellaneous';
import { IOperationCreateData, IOperationSchema, OperationID, OperationType } from '@/models/oss';
2024-07-21 15:19:57 +03:00
import { PARAMETER } from '@/utils/constants';
import { describeOperationType, labelOperationType } from '@/utils/labels';
import TabInputOperation from './TabInputOperation';
import TabSynthesisOperation from './TabSynthesisOperation';
interface DlgCreateOperationProps {
hideWindow: () => void;
oss: IOperationSchema;
onCreate: (data: IOperationCreateData) => void;
}
export enum TabID {
INPUT = 0,
SYNTHESIS = 1
}
2024-07-29 16:56:24 +03:00
function DlgCreateOperation({ hideWindow, oss, onCreate }: DlgCreateOperationProps) {
2024-07-21 15:19:57 +03:00
const library = useLibrary();
const [activeTab, setActiveTab] = useState(TabID.INPUT);
const [alias, setAlias] = useState('');
const [title, setTitle] = useState('');
const [comment, setComment] = useState('');
const [inputs, setInputs] = useState<OperationID[]>([]);
const [attachedID, setAttachedID] = useState<LibraryItemID | undefined>(undefined);
2024-07-24 22:23:35 +03:00
const [syncText, setSyncText] = useState(true);
2024-07-26 17:31:57 +03:00
const [createSchema, setCreateSchema] = useState(false);
2024-07-21 15:19:57 +03:00
2024-07-26 17:31:57 +03:00
const isValid = useMemo(
() => (alias !== '' && activeTab === TabID.INPUT) || inputs.length != 1,
[alias, activeTab, inputs]
);
2024-07-21 15:19:57 +03:00
useLayoutEffect(() => {
if (attachedID) {
const schema = library.items.find(value => value.id === attachedID);
if (schema) {
setAlias(schema.alias);
setTitle(schema.title);
setComment(schema.comment);
}
}
}, [attachedID, library]);
const handleSubmit = () => {
const data: IOperationCreateData = {
item_data: {
2024-07-29 16:56:24 +03:00
position_x: 0,
position_y: 0,
2024-07-21 15:19:57 +03:00
alias: alias,
title: title,
comment: comment,
2024-07-24 22:23:35 +03:00
sync_text: activeTab === TabID.INPUT ? syncText : true,
2024-07-21 15:19:57 +03:00
operation_type: activeTab === TabID.INPUT ? OperationType.INPUT : OperationType.SYNTHESIS,
result: activeTab === TabID.INPUT ? attachedID ?? null : null
},
2024-07-29 16:56:24 +03:00
positions: [],
2024-07-26 17:31:57 +03:00
arguments: activeTab === TabID.INPUT ? undefined : inputs.length > 0 ? inputs : undefined,
create_schema: createSchema
2024-07-21 15:19:57 +03:00
};
onCreate(data);
};
const inputPanel = useMemo(
() => (
<TabPanel>
<TabInputOperation
2024-07-26 00:34:08 +03:00
oss={oss}
2024-07-21 15:19:57 +03:00
alias={alias}
setAlias={setAlias}
comment={comment}
setComment={setComment}
title={title}
setTitle={setTitle}
attachedID={attachedID}
setAttachedID={setAttachedID}
2024-07-24 22:23:35 +03:00
syncText={syncText}
setSyncText={setSyncText}
2024-07-26 17:31:57 +03:00
createSchema={createSchema}
setCreateSchema={setCreateSchema}
2024-07-21 15:19:57 +03:00
/>
</TabPanel>
),
2024-07-26 17:31:57 +03:00
[alias, comment, title, attachedID, syncText, oss, createSchema]
2024-07-21 15:19:57 +03:00
);
const synthesisPanel = useMemo(
() => (
<TabPanel>
<TabSynthesisOperation
oss={oss}
alias={alias}
setAlias={setAlias}
comment={comment}
setComment={setComment}
title={title}
setTitle={setTitle}
2024-07-26 17:31:57 +03:00
inputs={inputs}
2024-07-21 15:19:57 +03:00
setInputs={setInputs}
/>
</TabPanel>
),
2024-07-26 17:31:57 +03:00
[oss, alias, comment, title, inputs]
2024-07-21 15:19:57 +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={describeOperationType(OperationType.INPUT)}
label={labelOperationType(OperationType.INPUT)}
/>
<TabLabel
title={describeOperationType(OperationType.SYNTHESIS)}
label={labelOperationType(OperationType.SYNTHESIS)}
/>
</TabList>
{inputPanel}
{synthesisPanel}
</Tabs>
</Modal>
);
}
export default DlgCreateOperation;