2024-10-29 12:44:51 +03:00
|
|
|
|
import clsx from 'clsx';
|
2024-09-12 13:27:06 +03:00
|
|
|
|
import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
|
|
import DataTable, { createColumnHelper, IConditionalStyle } from '@/components/ui/DataTable';
|
|
|
|
|
import SearchBar from '@/components/ui/SearchBar';
|
2024-06-26 19:47:05 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/ConceptOptionsContext';
|
2024-09-12 13:27:06 +03:00
|
|
|
|
import { useLibrary } from '@/context/LibraryContext';
|
|
|
|
|
import useDropdown from '@/hooks/useDropdown';
|
2024-06-19 22:09:31 +03:00
|
|
|
|
import { ILibraryItem, LibraryItemID, LibraryItemType } from '@/models/library';
|
2024-08-17 22:30:49 +03:00
|
|
|
|
import { matchLibraryItem } from '@/models/libraryAPI';
|
2024-09-12 13:27:06 +03:00
|
|
|
|
import { prefixes } from '@/utils/constants';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-09-12 13:27:06 +03:00
|
|
|
|
import { IconClose, IconFolderTree } from '../Icons';
|
|
|
|
|
import { CProps } from '../props';
|
|
|
|
|
import Dropdown from '../ui/Dropdown';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import FlexColumn from '../ui/FlexColumn';
|
2024-09-12 13:27:06 +03:00
|
|
|
|
import MiniButton from '../ui/MiniButton';
|
|
|
|
|
import SelectLocation from './SelectLocation';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-10-29 12:44:51 +03:00
|
|
|
|
interface PickSchemaProps extends CProps.Styling {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
id?: string;
|
|
|
|
|
initialFilter?: string;
|
|
|
|
|
rows?: number;
|
|
|
|
|
|
2024-08-17 22:30:49 +03:00
|
|
|
|
items: ILibraryItem[];
|
|
|
|
|
itemType: LibraryItemType;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
value?: LibraryItemID;
|
2024-07-26 00:33:22 +03:00
|
|
|
|
baseFilter?: (target: ILibraryItem) => boolean;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
onSelectValue: (newValue: LibraryItemID) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<ILibraryItem>();
|
|
|
|
|
|
2024-08-17 22:30:49 +03:00
|
|
|
|
function PickSchema({
|
|
|
|
|
id,
|
|
|
|
|
initialFilter = '',
|
|
|
|
|
rows = 4,
|
|
|
|
|
items,
|
|
|
|
|
itemType,
|
|
|
|
|
value,
|
|
|
|
|
onSelectValue,
|
2024-10-29 12:44:51 +03:00
|
|
|
|
baseFilter,
|
|
|
|
|
className,
|
|
|
|
|
...restProps
|
2024-08-17 22:30:49 +03:00
|
|
|
|
}: PickSchemaProps) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
const intl = useIntl();
|
|
|
|
|
const { colors } = useConceptOptions();
|
2024-09-12 13:27:06 +03:00
|
|
|
|
const { folders } = useLibrary();
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
const [filterText, setFilterText] = useState(initialFilter);
|
2024-09-12 13:27:06 +03:00
|
|
|
|
const [filterLocation, setFilterLocation] = useState('');
|
2024-08-17 22:30:49 +03:00
|
|
|
|
const [filtered, setFiltered] = useState<ILibraryItem[]>([]);
|
|
|
|
|
const baseFiltered = useMemo(
|
|
|
|
|
() => items.filter(item => item.item_type === itemType && (!baseFilter || baseFilter(item))),
|
|
|
|
|
[items, itemType, baseFilter]
|
|
|
|
|
);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-09-12 13:27:06 +03:00
|
|
|
|
const locationMenu = useDropdown();
|
|
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
|
useLayoutEffect(() => {
|
2024-09-12 13:27:06 +03:00
|
|
|
|
let newFiltered = baseFiltered.filter(item => matchLibraryItem(item, filterText));
|
|
|
|
|
if (filterLocation.length > 0) {
|
|
|
|
|
newFiltered = newFiltered.filter(
|
|
|
|
|
item => item.location === filterLocation || item.location.startsWith(`${filterLocation}/`)
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-08-17 22:30:49 +03:00
|
|
|
|
setFiltered(newFiltered);
|
2024-10-23 16:20:29 +03:00
|
|
|
|
}, [filterText, filterLocation, baseFiltered]);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
const columns = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
header: 'Шифр',
|
|
|
|
|
size: 150,
|
|
|
|
|
minSize: 80,
|
|
|
|
|
maxSize: 150
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor('title', {
|
|
|
|
|
id: 'title',
|
|
|
|
|
header: 'Название',
|
|
|
|
|
size: 1200,
|
|
|
|
|
minSize: 200,
|
|
|
|
|
maxSize: 1200,
|
|
|
|
|
cell: props => <div className='text-ellipsis'>{props.getValue()}</div>
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor('time_update', {
|
|
|
|
|
id: 'time_update',
|
|
|
|
|
header: 'Дата',
|
|
|
|
|
cell: props => (
|
|
|
|
|
<div className='whitespace-nowrap'>
|
|
|
|
|
{new Date(props.getValue()).toLocaleString(intl.locale, {
|
|
|
|
|
year: '2-digit',
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
day: '2-digit'
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
],
|
|
|
|
|
[intl]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const conditionalRowStyles = useMemo(
|
|
|
|
|
(): IConditionalStyle<ILibraryItem>[] => [
|
|
|
|
|
{
|
|
|
|
|
when: (item: ILibraryItem) => item.id === value,
|
|
|
|
|
style: { backgroundColor: colors.bgSelected }
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[value, colors]
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-12 13:27:06 +03:00
|
|
|
|
const handleLocationClick = useCallback(
|
|
|
|
|
(event: CProps.EventMouse, newValue: string) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
locationMenu.hide();
|
|
|
|
|
setFilterLocation(newValue);
|
|
|
|
|
},
|
|
|
|
|
[locationMenu]
|
|
|
|
|
);
|
|
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
|
return (
|
2024-10-29 12:44:51 +03:00
|
|
|
|
<div className={clsx('border divide-y', className)} {...restProps}>
|
|
|
|
|
<div className='flex justify-between clr-input items-center pr-1 rounded-t-md'>
|
2024-09-12 13:27:06 +03:00
|
|
|
|
<SearchBar
|
|
|
|
|
id={id ? `${id}__search` : undefined}
|
2024-10-29 12:44:51 +03:00
|
|
|
|
className='clr-input flex-grow rounded-t-md'
|
2024-09-12 13:27:06 +03:00
|
|
|
|
noBorder
|
2024-11-21 15:09:31 +03:00
|
|
|
|
query={filterText}
|
|
|
|
|
onChangeQuery={newValue => setFilterText(newValue)}
|
2024-09-12 13:27:06 +03:00
|
|
|
|
/>
|
|
|
|
|
<div ref={locationMenu.ref}>
|
|
|
|
|
<MiniButton
|
|
|
|
|
icon={<IconFolderTree size='1.25rem' className={!!filterLocation ? 'icon-green' : 'icon-primary'} />}
|
|
|
|
|
title='Фильтр по расположению'
|
|
|
|
|
className='mt-1'
|
|
|
|
|
onClick={() => locationMenu.toggle()}
|
|
|
|
|
/>
|
|
|
|
|
<Dropdown isOpen={locationMenu.isOpen} stretchLeft className='w-[20rem] h-[12.5rem] z-modalTooltip mt-0'>
|
|
|
|
|
<SelectLocation
|
|
|
|
|
folderTree={folders}
|
|
|
|
|
value={filterLocation}
|
|
|
|
|
prefix={prefixes.folders_list}
|
|
|
|
|
dense
|
|
|
|
|
onClick={(event, target) => handleLocationClick(event, target.getPath())}
|
|
|
|
|
/>
|
|
|
|
|
</Dropdown>
|
|
|
|
|
</div>
|
|
|
|
|
{filterLocation.length > 0 ? (
|
|
|
|
|
<MiniButton
|
|
|
|
|
icon={<IconClose size='1.25rem' className='icon-red' />}
|
|
|
|
|
title='Сбросить фильтр'
|
|
|
|
|
onClick={() => setFilterLocation('')}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<DataTable
|
|
|
|
|
id={id}
|
|
|
|
|
rows={rows}
|
|
|
|
|
dense
|
|
|
|
|
noHeader
|
|
|
|
|
noFooter
|
|
|
|
|
className='text-sm select-none cc-scroll-y'
|
2024-08-17 22:30:49 +03:00
|
|
|
|
data={filtered}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
|
|
|
|
noDataComponent={
|
2024-06-21 19:16:41 +03:00
|
|
|
|
<FlexColumn className='dense p-3 items-center min-h-[6rem]'>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<p>Список схем пуст</p>
|
|
|
|
|
<p>Измените параметры фильтра</p>
|
|
|
|
|
</FlexColumn>
|
|
|
|
|
}
|
|
|
|
|
onRowClicked={rowData => onSelectValue(rowData.id)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PickSchema;
|