2023-12-13 14:32:57 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2023-11-06 18:03:23 +03:00
|
|
|
|
import { createColumnHelper } from '@tanstack/react-table';
|
2023-12-15 17:34:50 +03:00
|
|
|
|
import clsx from 'clsx';
|
2023-11-06 23:13:27 +03:00
|
|
|
|
import { Dispatch, useCallback, useEffect, useMemo, useState } from 'react';
|
2023-12-16 19:20:26 +03:00
|
|
|
|
import { BiCheck, BiRefresh, BiX } from 'react-icons/bi';
|
2023-11-06 02:20:16 +03:00
|
|
|
|
|
2024-01-04 19:38:12 +03:00
|
|
|
|
import ConstituentaPicker from '@/components/ConstituentaPicker';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import DataTable, { IConditionalStyle } from '@/components/DataTable';
|
|
|
|
|
import RSInput from '@/components/RSInput';
|
2024-01-04 19:38:12 +03:00
|
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import { useConceptTheme } from '@/context/ThemeContext';
|
|
|
|
|
import { IConstituenta, IRSForm } from '@/models/rsform';
|
|
|
|
|
import { IArgumentValue } from '@/models/rslang';
|
|
|
|
|
import { prefixes } from '@/utils/constants';
|
2023-11-06 02:20:16 +03:00
|
|
|
|
|
|
|
|
|
interface ArgumentsTabProps {
|
2023-12-28 14:04:44 +03:00
|
|
|
|
state: IArgumentsState;
|
|
|
|
|
schema: IRSForm;
|
|
|
|
|
partialUpdate: Dispatch<Partial<IArgumentsState>>;
|
2023-11-06 02:20:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IArgumentsState {
|
2023-12-28 14:04:44 +03:00
|
|
|
|
arguments: IArgumentValue[];
|
|
|
|
|
definition: string;
|
2023-11-06 02:20:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 18:03:23 +03:00
|
|
|
|
const argumentsHelper = createColumnHelper<IArgumentValue>();
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
function ArgumentsTab({ state, schema, partialUpdate }: ArgumentsTabProps) {
|
2023-11-06 18:03:23 +03:00
|
|
|
|
const { colors } = useConceptTheme();
|
2023-12-28 14:04:44 +03:00
|
|
|
|
|
2023-11-06 18:03:23 +03:00
|
|
|
|
const [selectedCst, setSelectedCst] = useState<IConstituenta | undefined>(undefined);
|
|
|
|
|
const [selectedArgument, setSelectedArgument] = useState<IArgumentValue | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
const [argumentValue, setArgumentValue] = useState('');
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
const selectedClearable = useMemo(() => {
|
2023-12-02 00:15:56 +03:00
|
|
|
|
return argumentValue && !!selectedArgument && !!selectedArgument.value;
|
|
|
|
|
}, [argumentValue, selectedArgument]);
|
|
|
|
|
|
|
|
|
|
const isModified = useMemo(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
() => selectedArgument && argumentValue !== selectedArgument.value,
|
|
|
|
|
[selectedArgument, argumentValue]
|
|
|
|
|
);
|
2023-12-02 00:15:56 +03:00
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
useEffect(() => {
|
2023-11-06 23:13:27 +03:00
|
|
|
|
if (!selectedArgument && state.arguments.length > 0) {
|
|
|
|
|
setSelectedArgument(state.arguments[0]);
|
|
|
|
|
}
|
|
|
|
|
}, [state.arguments, selectedArgument]);
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
const handleSelectArgument = useCallback((arg: IArgumentValue) => {
|
2023-11-06 18:03:23 +03:00
|
|
|
|
setSelectedArgument(arg);
|
2023-11-06 23:13:27 +03:00
|
|
|
|
if (arg.value) {
|
|
|
|
|
setArgumentValue(arg.value);
|
|
|
|
|
}
|
2023-11-06 18:03:23 +03:00
|
|
|
|
}, []);
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
const handleSelectConstituenta = useCallback((cst: IConstituenta) => {
|
2023-11-06 18:03:23 +03:00
|
|
|
|
setSelectedCst(cst);
|
|
|
|
|
setArgumentValue(cst.alias);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleClearArgument = useCallback(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
(target: IArgumentValue) => {
|
|
|
|
|
const newArg = { ...target, value: '' };
|
|
|
|
|
partialUpdate({
|
|
|
|
|
arguments: state.arguments.map(arg => (arg.alias !== target.alias ? arg : newArg))
|
|
|
|
|
});
|
|
|
|
|
setSelectedArgument(newArg);
|
|
|
|
|
},
|
|
|
|
|
[partialUpdate, state.arguments]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleReset = useCallback(() => {
|
2023-12-02 00:15:56 +03:00
|
|
|
|
setArgumentValue(selectedArgument?.value ?? '');
|
|
|
|
|
}, [selectedArgument]);
|
|
|
|
|
|
2023-11-06 18:03:23 +03:00
|
|
|
|
const handleAssignArgument = useCallback(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
(target: IArgumentValue, value: string) => {
|
|
|
|
|
const newArg = { ...target, value: value };
|
|
|
|
|
partialUpdate({
|
|
|
|
|
arguments: state.arguments.map(arg => (arg.alias !== target.alias ? arg : newArg))
|
|
|
|
|
});
|
|
|
|
|
setSelectedArgument(newArg);
|
|
|
|
|
},
|
|
|
|
|
[partialUpdate, state.arguments]
|
|
|
|
|
);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
|
|
|
|
const columns = useMemo(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
() => [
|
|
|
|
|
argumentsHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
header: 'Имя',
|
|
|
|
|
size: 40,
|
|
|
|
|
minSize: 40,
|
|
|
|
|
maxSize: 40,
|
|
|
|
|
cell: props => <div className='text-center'>{props.getValue()}</div>
|
|
|
|
|
}),
|
|
|
|
|
argumentsHelper.accessor(arg => arg.value || 'свободный аргумент', {
|
|
|
|
|
id: 'value',
|
|
|
|
|
header: 'Значение',
|
|
|
|
|
size: 200,
|
|
|
|
|
minSize: 200,
|
|
|
|
|
maxSize: 200
|
|
|
|
|
}),
|
|
|
|
|
argumentsHelper.accessor(arg => arg.typification, {
|
|
|
|
|
id: 'type',
|
|
|
|
|
header: 'Типизация',
|
|
|
|
|
enableHiding: true,
|
|
|
|
|
cell: props => (
|
|
|
|
|
<div className={clsx('min-w-[9.3rem] max-w-[9.3rem]', 'text-sm break-words')}>{props.getValue()}</div>
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
argumentsHelper.display({
|
|
|
|
|
id: 'actions',
|
|
|
|
|
size: 50,
|
|
|
|
|
minSize: 50,
|
|
|
|
|
maxSize: 50,
|
|
|
|
|
cell: props => (
|
|
|
|
|
<div className='max-h-[1.2rem]'>
|
|
|
|
|
{props.row.original.value ? (
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Очистить значение'
|
2024-03-08 18:39:08 +03:00
|
|
|
|
icon={<BiX size='0.75rem' className='clr-text-red' />}
|
2023-12-28 14:04:44 +03:00
|
|
|
|
noHover
|
|
|
|
|
onClick={() => handleClearArgument(props.row.original)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
],
|
|
|
|
|
[handleClearArgument]
|
|
|
|
|
);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
|
|
|
|
const conditionalRowStyles = useMemo(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
(): IConditionalStyle<IArgumentValue>[] => [
|
|
|
|
|
{
|
|
|
|
|
when: (arg: IArgumentValue) => arg.alias === selectedArgument?.alias,
|
|
|
|
|
style: { backgroundColor: colors.bgSelected }
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[selectedArgument, colors]
|
|
|
|
|
);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
2023-11-06 02:20:16 +03:00
|
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<>
|
|
|
|
|
<DataTable
|
|
|
|
|
dense
|
|
|
|
|
noFooter
|
|
|
|
|
className={clsx('max-h-[5.8rem] min-h-[5.8rem]', 'overflow-y-auto', 'text-sm', 'border', 'select-none')}
|
|
|
|
|
data={state.arguments}
|
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
|
|
|
|
noDataComponent={<p className={clsx('min-h-[3.6rem]', 'p-2', 'text-center')}>Аргументы отсутствуют</p>}
|
|
|
|
|
onRowClicked={handleSelectArgument}
|
2023-11-06 18:03:23 +03:00
|
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
|
|
|
|
|
<div className={clsx('my-4', 'flex gap-2 justify-center items-center', 'select-none')}>
|
|
|
|
|
<span
|
|
|
|
|
title='Выберите аргумент из списка сверху и значение из списка снизу'
|
|
|
|
|
className='font-semibold text-center'
|
|
|
|
|
>
|
|
|
|
|
{selectedArgument?.alias || 'ARG'}
|
|
|
|
|
</span>
|
|
|
|
|
<span>=</span>
|
|
|
|
|
<RSInput
|
|
|
|
|
noTooltip
|
|
|
|
|
className='w-[12rem]'
|
|
|
|
|
value={argumentValue}
|
|
|
|
|
onChange={newValue => setArgumentValue(newValue)}
|
2023-12-02 00:15:56 +03:00
|
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<div className='flex'>
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Подставить значение аргумента'
|
2024-03-08 18:39:08 +03:00
|
|
|
|
icon={<BiCheck size='1.25rem' className={!!argumentValue && !!selectedArgument ? 'clr-text-green' : ''} />}
|
2023-12-28 14:04:44 +03:00
|
|
|
|
disabled={!argumentValue || !selectedArgument}
|
|
|
|
|
onClick={() => handleAssignArgument(selectedArgument!, argumentValue)}
|
|
|
|
|
/>
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Откатить значение'
|
|
|
|
|
disabled={!isModified}
|
|
|
|
|
onClick={handleReset}
|
|
|
|
|
icon={<BiRefresh size='1.25rem' className={isModified ? 'clr-text-primary' : ''} />}
|
|
|
|
|
/>
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Очистить значение аргумента'
|
|
|
|
|
disabled={!selectedClearable}
|
2024-03-08 18:39:08 +03:00
|
|
|
|
icon={<BiX size='1.25rem' className={selectedClearable ? 'clr-text-red' : ''} />}
|
2023-12-28 14:04:44 +03:00
|
|
|
|
onClick={() => (selectedArgument ? handleClearArgument(selectedArgument) : undefined)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2023-12-02 00:15:56 +03:00
|
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
|
|
|
|
|
<ConstituentaPicker
|
|
|
|
|
value={selectedCst}
|
|
|
|
|
data={schema?.items}
|
|
|
|
|
onSelectValue={handleSelectConstituenta}
|
|
|
|
|
prefixID={prefixes.cst_modal_list}
|
|
|
|
|
rows={7}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<RSInput
|
|
|
|
|
disabled
|
|
|
|
|
id='result'
|
|
|
|
|
placeholder='Итоговое определение'
|
|
|
|
|
className='mt-[1.2rem]'
|
|
|
|
|
height='5.1rem'
|
|
|
|
|
value={state.definition}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2023-11-06 02:20:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
export default ArgumentsTab;
|