ConceptPortal-public/rsconcept/frontend/src/pages/LibraryPage/ViewLibrary.tsx

153 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-07-15 17:46:19 +03:00
import { useMemo } from 'react';
import { useIntl } from 'react-intl';
2023-07-25 23:08:10 +03:00
import ConceptTooltip from '../../components/common/ConceptTooltip';
import TextURL from '../../components/common/TextURL';
2023-09-10 20:17:18 +03:00
import DataTable, { createColumnHelper } from '../../components/DataTable';
2023-08-26 19:39:49 +03:00
import HelpLibrary from '../../components/Help/HelpLibrary';
2023-10-23 18:22:55 +03:00
import { EducationIcon, GroupIcon, HelpIcon,SubscribedIcon } from '../../components/Icons';
2023-08-26 17:26:49 +03:00
import { useAuth } from '../../context/AuthContext';
2023-09-05 00:23:53 +03:00
import { useConceptNavigation } from '../../context/NagivationContext';
2023-07-15 17:46:19 +03:00
import { useUsers } from '../../context/UsersContext';
2023-09-10 20:17:18 +03:00
import useLocalStorage from '../../hooks/useLocalStorage';
2023-09-11 20:31:54 +03:00
import { ILibraryItem } from '../../models/library';
2023-08-26 17:26:49 +03:00
import { prefixes } from '../../utils/constants';
2023-07-15 17:46:19 +03:00
2023-07-28 00:03:37 +03:00
interface ViewLibraryProps {
2023-08-25 22:51:20 +03:00
items: ILibraryItem[]
2023-09-11 21:06:51 +03:00
resetQuery: () => void
2023-07-15 17:46:19 +03:00
}
const columnHelper = createColumnHelper<ILibraryItem>();
2023-09-11 21:06:51 +03:00
function ViewLibrary({ items, resetQuery: cleanQuery }: ViewLibraryProps) {
2023-09-05 00:23:53 +03:00
const { navigateTo } = useConceptNavigation();
2023-07-15 17:46:19 +03:00
const intl = useIntl();
2023-09-05 00:23:53 +03:00
const { user } = useAuth();
2023-07-15 17:46:19 +03:00
const { getUserLabel } = useUsers();
2023-09-10 20:17:18 +03:00
const [ itemsPerPage, setItemsPerPage ] = useLocalStorage<number>('library_per_page', 50);
2023-09-05 00:23:53 +03:00
const openRSForm = (item: ILibraryItem) => navigateTo(`/rsforms/${item.id}`);
2023-07-15 17:46:19 +03:00
2023-08-26 17:26:49 +03:00
const columns = useMemo(
() => [
columnHelper.display({
2023-08-26 17:26:49 +03:00
id: 'status',
header: '',
size: 60,
2023-09-10 20:17:18 +03:00
minSize: 60,
maxSize: 60,
cell: props => {
const item = props.row.original;
2023-08-26 17:26:49 +03:00
return (<>
<div
2023-09-11 21:06:51 +03:00
className='flex items-center justify-start gap-1 min-w-[2.75rem]'
2023-08-26 17:26:49 +03:00
id={`${prefixes.library_list}${item.id}`}
>
{(user && user.subscriptions.includes(item.id)) ?
<p title='Отслеживаемая'>
<SubscribedIcon size={3}/>
</p> : null}
{item.is_common ?
<p title='Общедоступная'>
<GroupIcon size={3}/>
</p> : null}
{item.is_canonical ?
<p title='Неизменная'>
<EducationIcon size={3}/>
</p> : null}
2023-08-26 17:26:49 +03:00
</div>
</>);
},
}),
columnHelper.accessor('alias', {
2023-08-26 17:26:49 +03:00
id: 'alias',
header: 'Шифр',
size: 200,
minSize: 200,
maxSize: 200,
2023-09-10 20:17:18 +03:00
enableSorting: true,
sortingFn: 'text'
}),
columnHelper.accessor('title', {
id: 'title',
header: 'Название',
2023-10-08 18:45:15 +03:00
size: 2000,
2023-09-10 20:17:18 +03:00
minSize: 400,
2023-10-08 18:45:15 +03:00
maxSize: 2000,
2023-09-10 20:17:18 +03:00
enableSorting: true,
sortingFn: 'text'
}),
columnHelper.accessor(item => item.owner ?? 0, {
id: 'owner',
header: 'Владелец',
2023-10-08 18:45:15 +03:00
size: 600,
2023-09-10 20:17:18 +03:00
minSize: 200,
2023-10-08 18:45:15 +03:00
maxSize: 600,
cell: props => getUserLabel(props.cell.getValue()),
enableSorting: true,
2023-09-10 20:17:18 +03:00
sortingFn: 'text'
}),
columnHelper.accessor('time_update', {
id: 'time_update',
header: 'Обновлена',
2023-09-11 21:06:51 +03:00
size: 150,
minSize: 150,
maxSize: 150,
cell: props => <div className='text-sm min-w-[8.25rem]'>{new Date(props.cell.getValue()).toLocaleString(intl.locale)}</div>,
2023-09-10 20:17:18 +03:00
enableSorting: true,
sortingFn: 'datetime',
sortDescFirst: true
})
2023-08-26 17:26:49 +03:00
], [intl, getUserLabel, user]);
2023-07-15 17:46:19 +03:00
return (
<>
{items.length !== 0 ?
<div className='sticky top-[2.3rem] w-full'>
<div className='absolute top-[-0.125rem] left-0 flex gap-1 ml-3 z-pop'>
<div id='library-help' className='py-2 '>
<HelpIcon color='text-primary' size={5} />
2023-08-26 19:39:49 +03:00
</div>
<ConceptTooltip anchorSelect='#library-help'>
<div className='max-w-[35rem]'>
<HelpLibrary />
</div>
</ConceptTooltip>
</div>
</div> : null}
<DataTable
columns={columns}
data={items}
2023-09-11 21:06:51 +03:00
headPosition='2.3rem'
noDataComponent={
<div className='flex flex-col gap-4 justify-center p-2 text-center min-h-[6rem]'>
<p>Список схем пуст</p>
<p className='flex justify-center gap-4'>
<TextURL text='Создать схему' href='/rsform-create'/>
<span className='cursor-pointer hover:underline text-url' onClick={cleanQuery}>
Очистить фильтр
</span>
</p>
</div>}
onRowClicked={openRSForm}
2023-09-10 20:17:18 +03:00
enableSorting
initialSorting={{
id: 'time_update',
desc: true
}}
2023-09-10 20:17:18 +03:00
enablePagination
paginationPerPage={itemsPerPage}
onChangePaginationOption={setItemsPerPage}
paginationOptions={[10, 20, 30, 50, 100]}
/>
</>);
2023-07-15 17:46:19 +03:00
}
2023-07-28 00:03:37 +03:00
export default ViewLibrary;