'use client'; import { useCallback, useMemo, useState } from 'react'; import BadgeConstituenta from '@/components/info/BadgeConstituenta'; import SelectConstituenta from '@/components/select/SelectConstituenta'; import DataTable, { createColumnHelper } from '@/components/ui/DataTable'; import MiniButton from '@/components/ui/MiniButton'; import { useConceptOptions } from '@/context/ConceptOptionsContext'; import { ILibraryItem } from '@/models/library'; import { ICstSubstitute, IMultiSubstitution } from '@/models/oss'; import { ConstituentaID, IConstituenta, IRSForm } from '@/models/rsform'; import { IconPageLeft, IconPageRight, IconRemove, IconReplace } from '../Icons'; import NoData from '../ui/NoData'; import SelectLibraryItem from './SelectLibraryItem'; interface PickSubstitutionsProps { substitutions: ICstSubstitute[]; setSubstitutions: React.Dispatch>; prefixID: string; rows?: number; allowSelfSubstitution?: boolean; schemas: IRSForm[]; filter?: (cst: IConstituenta) => boolean; } const columnHelper = createColumnHelper(); function PickSubstitutions({ substitutions, setSubstitutions, prefixID, rows, schemas, filter, allowSelfSubstitution }: PickSubstitutionsProps) { const { colors } = useConceptOptions(); const [leftArgument, setLeftArgument] = useState( schemas.length === 1 ? schemas[0] : undefined ); const [rightArgument, setRightArgument] = useState( schemas.length === 1 && allowSelfSubstitution ? schemas[0] : undefined ); const [leftCst, setLeftCst] = useState(undefined); const [rightCst, setRightCst] = useState(undefined); const [deleteRight, setDeleteRight] = useState(true); const toggleDelete = () => setDeleteRight(prev => !prev); const getSchemaByCst = useCallback( (id: ConstituentaID): IRSForm | undefined => { for (const schema of schemas) { const cst = schema.cstByID.get(id); if (cst) { return schema; } } return undefined; }, [schemas] ); const getConstituenta = useCallback( (id: ConstituentaID): IConstituenta | undefined => { for (const schema of schemas) { const cst = schema.cstByID.get(id); if (cst) { return cst; } } return undefined; }, [schemas] ); const substitutionData: IMultiSubstitution[] = useMemo( () => substitutions.map(item => ({ original_source: getSchemaByCst(item.original), original: getConstituenta(item.original), substitution: getConstituenta(item.substitution), substitution_source: getSchemaByCst(item.substitution) })), [getConstituenta, getSchemaByCst, substitutions] ); function addSubstitution() { if (!leftCst || !rightCst) { return; } const newSubstitution: ICstSubstitute = { original: deleteRight ? rightCst.id : leftCst.id, substitution: deleteRight ? leftCst.id : rightCst.id }; setSubstitutions(prev => [...prev, newSubstitution]); setLeftCst(undefined); setRightCst(undefined); } const handleDeleteRow = useCallback( (row: number) => { setSubstitutions(prev => { const newItems: ICstSubstitute[] = []; prev.forEach((item, index) => { if (index !== row) { newItems.push(item); } }); return newItems; }); }, [setSubstitutions] ); const columns = useMemo( () => [ columnHelper.accessor(item => item.substitution_source?.alias ?? 'N/A', { id: 'left_schema', header: 'Операция', size: 100, cell: props =>
{props.getValue()}
}), columnHelper.accessor(item => item.substitution?.alias ?? 'N/A', { id: 'left_alias', header: () => Имя, size: 65, cell: props => props.row.original.substitution ? ( ) : ( 'N/A' ) }), columnHelper.display({ id: 'status', header: '', size: 40, cell: () => }), columnHelper.accessor(item => item.original?.alias ?? 'N/A', { id: 'right_alias', header: () => Имя, size: 65, cell: props => props.row.original.original ? ( ) : ( 'N/A' ) }), columnHelper.accessor(item => item.original_source?.alias ?? 'N/A', { id: 'right_schema', header: 'Операция', size: 100, cell: props =>
{props.getValue()}
}), columnHelper.display({ id: 'actions', cell: props => (
} onClick={() => handleDeleteRow(props.row.index)} />
) }) ], [handleDeleteRow, colors, prefixID] ); return (
item.id !== rightArgument?.id)} value={leftArgument} onSelectValue={setLeftArgument} /> !substitutions.find(item => item.original === cst.id) && (!filter || filter(cst)) )} value={leftCst} onSelectValue={setLeftCst} />
) : ( ) } /> } disabled={!leftCst || !rightCst || leftCst === rightCst} onClick={addSubstitution} />
item.id !== leftArgument?.id)} value={rightArgument} onSelectValue={setRightArgument} /> !substitutions.find(item => item.original === cst.id) && (!filter || filter(cst)) )} value={rightCst} onSelectValue={setRightCst} />

Список пуст

Добавьте отождествление

} />
); } export default PickSubstitutions;