2023-12-13 14:32:57 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
|
import clsx from 'clsx';
|
2024-01-16 13:47:29 +03:00
|
|
|
|
import { useLayoutEffect, useMemo, useState } from 'react';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
import { useIntl } from 'react-intl';
|
2023-07-25 23:08:10 +03:00
|
|
|
|
|
2024-04-01 21:45:10 +03:00
|
|
|
|
import { urls } from '@/app/urls';
|
2024-04-01 19:07:20 +03:00
|
|
|
|
import BadgeHelp from '@/components/man/BadgeHelp';
|
2024-04-07 19:45:07 +03:00
|
|
|
|
import { CProps } from '@/components/props';
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import DataTable, { createColumnHelper, VisibilityState } from '@/components/ui/DataTable';
|
2024-01-06 03:15:02 +03:00
|
|
|
|
import FlexColumn from '@/components/ui/FlexColumn';
|
2024-01-04 19:38:12 +03:00
|
|
|
|
import TextURL from '@/components/ui/TextURL';
|
2023-12-26 14:23:51 +03:00
|
|
|
|
import { useConceptNavigation } from '@/context/NavigationContext';
|
2024-05-02 21:19:23 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/OptionsContext';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import { useUsers } from '@/context/UsersContext';
|
|
|
|
|
import useLocalStorage from '@/hooks/useLocalStorage';
|
2024-01-16 13:47:29 +03:00
|
|
|
|
import useWindowSize from '@/hooks/useWindowSize';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import { ILibraryItem } from '@/models/library';
|
2023-12-26 14:23:51 +03:00
|
|
|
|
import { HelpTopic } from '@/models/miscellaneous';
|
2024-03-27 15:32:59 +03:00
|
|
|
|
import { storage } from '@/utils/constants';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
|
2023-12-05 01:22:44 +03:00
|
|
|
|
import ItemIcons from './ItemIcons';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-07-28 00:03:37 +03:00
|
|
|
|
interface ViewLibraryProps {
|
2023-12-28 14:04:44 +03:00
|
|
|
|
items: ILibraryItem[];
|
|
|
|
|
resetQuery: () => void;
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-09 20:36:55 +03:00
|
|
|
|
const columnHelper = createColumnHelper<ILibraryItem>();
|
|
|
|
|
|
2024-03-18 16:21:39 +03:00
|
|
|
|
function ViewLibrary({ items, resetQuery }: ViewLibraryProps) {
|
2023-12-13 14:32:57 +03:00
|
|
|
|
const router = useConceptNavigation();
|
2023-07-15 17:46:19 +03:00
|
|
|
|
const intl = useIntl();
|
|
|
|
|
const { getUserLabel } = useUsers();
|
2024-05-02 21:19:23 +03:00
|
|
|
|
const { calculateHeight } = useConceptOptions();
|
2024-03-27 15:32:59 +03:00
|
|
|
|
const [itemsPerPage, setItemsPerPage] = useLocalStorage<number>(storage.libraryPagination, 50);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2024-04-07 19:45:07 +03:00
|
|
|
|
function handleOpenItem(item: ILibraryItem, event: CProps.EventMouse) {
|
|
|
|
|
router.push(urls.schema(item.id), event.ctrlKey);
|
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2024-01-16 13:47:29 +03:00
|
|
|
|
const windowSize = useWindowSize();
|
|
|
|
|
|
|
|
|
|
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
setColumnVisibility({
|
|
|
|
|
owner: !windowSize.isSmall
|
|
|
|
|
});
|
|
|
|
|
}, [windowSize]);
|
|
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
|
const columns = useMemo(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
() => [
|
|
|
|
|
columnHelper.display({
|
|
|
|
|
id: 'status',
|
|
|
|
|
header: '',
|
|
|
|
|
size: 60,
|
|
|
|
|
minSize: 60,
|
|
|
|
|
maxSize: 60,
|
2024-05-02 17:04:18 +03:00
|
|
|
|
cell: props => <ItemIcons item={props.row.original} />
|
2023-12-28 14:04:44 +03:00
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
header: 'Шифр',
|
2024-01-16 13:47:29 +03:00
|
|
|
|
size: 150,
|
|
|
|
|
minSize: 80,
|
|
|
|
|
maxSize: 150,
|
2023-12-28 14:04:44 +03:00
|
|
|
|
enableSorting: true,
|
|
|
|
|
sortingFn: 'text'
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor('title', {
|
|
|
|
|
id: 'title',
|
|
|
|
|
header: 'Название',
|
2024-01-16 13:47:29 +03:00
|
|
|
|
size: 1200,
|
|
|
|
|
minSize: 200,
|
|
|
|
|
maxSize: 1200,
|
2023-12-28 14:04:44 +03:00
|
|
|
|
enableSorting: true,
|
|
|
|
|
sortingFn: 'text'
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(item => item.owner ?? 0, {
|
|
|
|
|
id: 'owner',
|
|
|
|
|
header: 'Владелец',
|
2024-01-16 13:47:29 +03:00
|
|
|
|
size: 400,
|
|
|
|
|
minSize: 100,
|
|
|
|
|
maxSize: 400,
|
2024-03-08 18:39:08 +03:00
|
|
|
|
cell: props => getUserLabel(props.getValue()),
|
2023-12-28 14:04:44 +03:00
|
|
|
|
enableSorting: true,
|
|
|
|
|
sortingFn: 'text'
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor('time_update', {
|
2023-11-27 11:33:34 +03:00
|
|
|
|
id: 'time_update',
|
2024-01-16 13:47:29 +03:00
|
|
|
|
header: windowSize.isSmall ? 'Дата' : 'Обновлена',
|
2023-12-28 14:04:44 +03:00
|
|
|
|
cell: props => (
|
2024-01-16 13:47:29 +03:00
|
|
|
|
<div className='whitespace-nowrap'>
|
2024-03-08 18:39:08 +03:00
|
|
|
|
{new Date(props.getValue()).toLocaleString(intl.locale, {
|
2024-01-16 13:47:29 +03:00
|
|
|
|
year: '2-digit',
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
...(!windowSize.isSmall && {
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
minute: '2-digit'
|
|
|
|
|
})
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
),
|
|
|
|
|
enableSorting: true,
|
|
|
|
|
sortingFn: 'datetime',
|
|
|
|
|
sortDescFirst: true
|
|
|
|
|
})
|
|
|
|
|
],
|
2024-05-02 17:04:18 +03:00
|
|
|
|
[intl, getUserLabel, windowSize]
|
2023-12-28 14:04:44 +03:00
|
|
|
|
);
|
2023-09-10 20:17:18 +03:00
|
|
|
|
|
2024-05-02 21:19:23 +03:00
|
|
|
|
const tableHeight = useMemo(() => calculateHeight('2.2rem'), [calculateHeight]);
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
return (
|
|
|
|
|
<>
|
2024-02-22 11:35:27 +03:00
|
|
|
|
<div className='sticky top-[2.3rem]'>
|
2024-01-04 15:04:14 +03:00
|
|
|
|
<div
|
|
|
|
|
className={clsx(
|
|
|
|
|
'z-pop', // prettier: split lines
|
|
|
|
|
'absolute top-[0.125rem] left-[0.25rem]',
|
|
|
|
|
'ml-3',
|
|
|
|
|
'flex gap-1'
|
|
|
|
|
)}
|
|
|
|
|
>
|
2024-04-01 19:07:20 +03:00
|
|
|
|
<BadgeHelp topic={HelpTopic.LIBRARY} className='max-w-[30rem] text-sm' offset={5} place='right-start' />
|
2023-12-28 14:04:44 +03:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<DataTable
|
2024-03-18 16:21:39 +03:00
|
|
|
|
id='library_data'
|
2023-12-28 14:04:44 +03:00
|
|
|
|
columns={columns}
|
|
|
|
|
data={items}
|
2024-05-02 21:19:23 +03:00
|
|
|
|
headPosition='0'
|
|
|
|
|
className='text-xs sm:text-sm cc-scroll-y'
|
|
|
|
|
style={{ maxHeight: tableHeight }}
|
2023-12-28 14:04:44 +03:00
|
|
|
|
noDataComponent={
|
2024-01-06 03:15:02 +03:00
|
|
|
|
<FlexColumn className='p-3 items-center min-h-[6rem]'>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<p>Список схем пуст</p>
|
2024-01-06 03:15:02 +03:00
|
|
|
|
<p className='flex gap-6'>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
<TextURL text='Создать схему' href='/library/create' />
|
2024-03-18 16:21:39 +03:00
|
|
|
|
<TextURL text='Очистить фильтр' onClick={resetQuery} />
|
2023-12-28 14:04:44 +03:00
|
|
|
|
</p>
|
2024-01-06 03:15:02 +03:00
|
|
|
|
</FlexColumn>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
}
|
2024-01-16 13:47:29 +03:00
|
|
|
|
columnVisibility={columnVisibility}
|
2023-12-28 14:04:44 +03:00
|
|
|
|
onRowClicked={handleOpenItem}
|
|
|
|
|
enableSorting
|
|
|
|
|
initialSorting={{
|
|
|
|
|
id: 'time_update',
|
|
|
|
|
desc: true
|
|
|
|
|
}}
|
|
|
|
|
enablePagination
|
|
|
|
|
paginationPerPage={itemsPerPage}
|
|
|
|
|
onChangePaginationOption={setItemsPerPage}
|
|
|
|
|
paginationOptions={[10, 20, 30, 50, 100]}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
export default ViewLibrary;
|