'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'; import { ICstSubstitute, IOperation, IOperationSchema, IOperationUpdateData, OperationID, OperationType } from '@/models/oss'; 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; onSubmit: (data: IOperationUpdateData) => void; } 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(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] ); const [substitutions, setSubstitutions] = useState(target.substitutions); const cache = useRSFormCache(); const schemas = useMemo( () => schemasIDs.map(id => cache.getSchema(id)).filter(item => item !== undefined), [schemasIDs, cache] ); const isValid = useMemo(() => alias !== '', [alias]); useEffect(() => { cache.preload(schemasIDs); }, [schemasIDs]); 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]); const handleSubmit = () => { const data: IOperationUpdateData = { target: target.id, item_data: { alias: alias, title: title, comment: comment }, positions: [], arguments: target.operation_type !== OperationType.SYNTHESIS ? undefined : inputs, substitutions: target.operation_type !== OperationType.SYNTHESIS ? undefined : substitutions }; onSubmit(data); }; const cardPanel = useMemo( () => ( ), [alias, comment, title, setAlias] ); const argumentsPanel = useMemo( () => ( ), [oss, target, inputs, setInputs] ); const synthesisPanel = useMemo( () => ( ), [cache.loading, cache.error, substitutions, schemas] ); return ( {target.operation_type === OperationType.SYNTHESIS ? ( ) : null} {target.operation_type === OperationType.SYNTHESIS ? ( ) : null} {cardPanel} {target.operation_type === OperationType.SYNTHESIS ? argumentsPanel : null} {target.operation_type === OperationType.SYNTHESIS ? synthesisPanel : null} ); } export default DlgEditOperation;