2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-08 19:24:08 +03:00
|
|
|
import { useCallback, useLayoutEffect } from 'react';
|
2023-12-16 19:20:26 +03:00
|
|
|
import { BiCog, BiFilterAlt } from 'react-icons/bi';
|
2023-12-08 19:24:08 +03:00
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
import ConceptSearch from '@/components/Common/ConceptSearch';
|
|
|
|
import Dropdown from '@/components/Common/Dropdown';
|
|
|
|
import DropdownButton from '@/components/Common/DropdownButton';
|
|
|
|
import SelectorButton from '@/components/Common/SelectorButton';
|
|
|
|
import useDropdown from '@/hooks/useDropdown';
|
|
|
|
import useLocalStorage from '@/hooks/useLocalStorage';
|
2023-12-26 14:23:51 +03:00
|
|
|
import { CstMatchMode, DependencyMode } from '@/models/miscellaneous';
|
|
|
|
import { applyGraphFilter } from '@/models/miscellaneousAPI';
|
2023-12-15 17:34:50 +03:00
|
|
|
import { IConstituenta, IRSForm } from '@/models/rsform';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { createMockConstituenta, matchConstituenta } from '@/models/rsformAPI';
|
|
|
|
import { extractGlobals } from '@/models/rslangAPI';
|
|
|
|
import { prefixes } from '@/utils/constants';
|
2023-12-26 14:23:51 +03:00
|
|
|
import { describeCstMatchMode, describeCstSource, labelCstMatchMode, labelCstSource } from '@/utils/labels';
|
2023-12-08 19:24:08 +03:00
|
|
|
|
|
|
|
interface ConstituentsSearchProps {
|
|
|
|
schema?: IRSForm
|
|
|
|
activeID?: number
|
|
|
|
activeExpression: string
|
|
|
|
setFiltered: React.Dispatch<React.SetStateAction<IConstituenta[]>>
|
|
|
|
}
|
|
|
|
|
|
|
|
function ConstituentsSearch({ schema, activeID, activeExpression, setFiltered }: ConstituentsSearchProps) {
|
|
|
|
const [filterMatch, setFilterMatch] = useLocalStorage('side-filter-match', CstMatchMode.ALL);
|
|
|
|
const [filterText, setFilterText] = useLocalStorage('side-filter-text', '');
|
|
|
|
const [filterSource, setFilterSource] = useLocalStorage('side-filter-dependency', DependencyMode.ALL);
|
|
|
|
|
|
|
|
const matchModeMenu = useDropdown();
|
|
|
|
const sourceMenu = useDropdown();
|
|
|
|
|
|
|
|
useLayoutEffect(
|
|
|
|
() => {
|
|
|
|
if (!schema || schema.items.length === 0) {
|
|
|
|
setFiltered([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let result: IConstituenta[] = [];
|
|
|
|
if (filterSource === DependencyMode.EXPRESSION) {
|
|
|
|
const aliases = extractGlobals(activeExpression);
|
|
|
|
result = schema.items.filter((cst) => aliases.has(cst.alias));
|
2023-12-15 17:34:50 +03:00
|
|
|
const names = result.map(cst => cst.alias);
|
2023-12-08 19:24:08 +03:00
|
|
|
const diff = Array.from(aliases).filter(name => !names.includes(name));
|
|
|
|
if (diff.length > 0) {
|
|
|
|
diff.forEach(
|
|
|
|
(alias, index) => result.push(
|
2023-12-15 17:34:50 +03:00
|
|
|
createMockConstituenta(-index, alias, 'Конституента отсутствует')
|
2023-12-08 19:24:08 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (!activeID) {
|
|
|
|
result = schema.items;
|
|
|
|
} else {
|
|
|
|
result = applyGraphFilter(schema, activeID, filterSource);
|
|
|
|
}
|
|
|
|
if (filterText) {
|
|
|
|
result = result.filter(cst => matchConstituenta(cst, filterText, filterMatch));
|
|
|
|
}
|
|
|
|
setFiltered(result);
|
|
|
|
}, [filterText, setFiltered, filterSource, activeExpression, schema?.items, schema, filterMatch, activeID]);
|
|
|
|
|
|
|
|
const handleMatchModeChange = useCallback(
|
|
|
|
(newValue: CstMatchMode) => {
|
|
|
|
matchModeMenu.hide();
|
|
|
|
setFilterMatch(newValue);
|
|
|
|
}, [matchModeMenu, setFilterMatch]);
|
|
|
|
|
|
|
|
const handleSourceChange = useCallback(
|
|
|
|
(newValue: DependencyMode) => {
|
|
|
|
sourceMenu.hide();
|
|
|
|
setFilterSource(newValue);
|
|
|
|
}, [sourceMenu, setFilterSource]);
|
|
|
|
|
|
|
|
return (
|
2023-12-19 11:18:28 +03:00
|
|
|
<div className='flex border-b clr-input'>
|
2023-12-08 19:24:08 +03:00
|
|
|
<ConceptSearch noBorder
|
2023-12-18 19:42:27 +03:00
|
|
|
className='min-w-[6rem] pr-2 flex-grow'
|
2023-12-08 19:24:08 +03:00
|
|
|
value={filterText}
|
|
|
|
onChange={setFilterText}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div ref={matchModeMenu.ref}>
|
|
|
|
<SelectorButton transparent tabIndex={-1}
|
2023-12-18 19:42:27 +03:00
|
|
|
title='Настройка атрибутов для фильтрации'
|
|
|
|
className='h-full'
|
2023-12-16 19:20:26 +03:00
|
|
|
icon={<BiFilterAlt size='1.25rem' />}
|
2023-12-26 14:23:51 +03:00
|
|
|
text={labelCstMatchMode(filterMatch)}
|
2023-12-08 19:24:08 +03:00
|
|
|
onClick={matchModeMenu.toggle}
|
|
|
|
/>
|
2023-12-25 16:53:27 +03:00
|
|
|
<Dropdown stretchLeft isOpen={matchModeMenu.isOpen}>
|
2023-12-08 19:24:08 +03:00
|
|
|
{Object.values(CstMatchMode).filter(value => !isNaN(Number(value))).map(
|
|
|
|
(value, index) => {
|
|
|
|
const matchMode = value as CstMatchMode;
|
|
|
|
return (
|
|
|
|
<DropdownButton
|
|
|
|
key={`${prefixes.cst_match_mode_list}${index}`}
|
|
|
|
onClick={() => handleMatchModeChange(matchMode)}
|
|
|
|
>
|
2023-12-26 14:23:51 +03:00
|
|
|
<p><b>{labelCstMatchMode(matchMode)}:</b> {describeCstMatchMode(matchMode)}</p>
|
2023-12-08 19:24:08 +03:00
|
|
|
</DropdownButton>);
|
|
|
|
})}
|
2023-12-25 16:53:27 +03:00
|
|
|
</Dropdown>
|
2023-12-08 19:24:08 +03:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div ref={sourceMenu.ref}>
|
|
|
|
<SelectorButton transparent tabIndex={-1}
|
2023-12-18 19:42:27 +03:00
|
|
|
title='Настройка фильтрации по графу термов'
|
|
|
|
className='h-full pr-2'
|
2023-12-16 19:20:26 +03:00
|
|
|
icon={<BiCog size='1.25rem' />}
|
2023-12-08 19:24:08 +03:00
|
|
|
text={labelCstSource(filterSource)}
|
|
|
|
onClick={sourceMenu.toggle}
|
|
|
|
/>
|
2023-12-25 16:53:27 +03:00
|
|
|
<Dropdown stretchLeft isOpen={sourceMenu.isOpen}>
|
2023-12-08 19:24:08 +03:00
|
|
|
{Object.values(DependencyMode).filter(value => !isNaN(Number(value))).map(
|
|
|
|
(value, index) => {
|
|
|
|
const source = value as DependencyMode;
|
|
|
|
return (
|
|
|
|
<DropdownButton
|
|
|
|
key={`${prefixes.cst_source_list}${index}`}
|
|
|
|
onClick={() => handleSourceChange(source)}
|
|
|
|
>
|
2023-12-16 19:20:26 +03:00
|
|
|
<p><b>{labelCstSource(source)}:</b> {describeCstSource(source)}</p>
|
2023-12-08 19:24:08 +03:00
|
|
|
</DropdownButton>);
|
|
|
|
})}
|
2023-12-25 16:53:27 +03:00
|
|
|
</Dropdown>
|
2023-12-08 19:24:08 +03:00
|
|
|
</div>
|
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ConstituentsSearch;
|