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

133 lines
4.1 KiB
TypeScript
Raw Normal View History

import { createColumnHelper } from '@tanstack/react-table';
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
2023-08-26 19:39:49 +03:00
import ConceptTooltip from '../../components/Common/ConceptTooltip';
import DataTable from '../../components/Common/DataTable';
import TextURL from '../../components/Common/TextURL';
2023-08-26 19:39:49 +03:00
import HelpLibrary from '../../components/Help/HelpLibrary';
import { EducationIcon, EyeIcon, GroupIcon, HelpIcon } 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-08-26 17:26:49 +03:00
import { prefixes } from '../../utils/constants';
2023-08-26 19:39:49 +03:00
import { ILibraryItem } from '../../utils/models';
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-08-26 19:39:49 +03:00
cleanQuery: () => void
2023-07-15 17:46:19 +03:00
}
const columnHelper = createColumnHelper<ILibraryItem>();
2023-08-26 19:39:49 +03:00
function ViewLibrary({ items, 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-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,
maxSize: 60,
cell: props => {
const item = props.row.original;
2023-08-26 17:26:49 +03:00
return (<>
<div
className='flex items-center justify-start gap-1'
id={`${prefixes.library_list}${item.id}`}
>
{user && user.subscriptions.includes(item.id) && <p title='Отслеживаемая'><EyeIcon size={3}/></p>}
{item.is_common && <p title='Общедоступная'><GroupIcon size={3}/></p>}
{item.is_canonical && <p title='Неизменная'><EducationIcon size={3}/></p>}
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,
enableSorting: true
}),
columnHelper.accessor('title', {
id: 'title',
header: 'Название',
minSize: 200,
size: 1000,
maxSize: 1000,
enableSorting: true
}),
columnHelper.accessor(item => item.owner ?? 0, {
id: 'owner',
header: 'Владелец',
cell: props => getUserLabel(props.cell.getValue()),
enableSorting: true,
enableResizing: false,
minSize: 200,
size: 300,
maxSize: 300
}),
columnHelper.accessor('time_update', {
id: 'time_update',
header: 'Обновлена',
minSize: 200,
size: 200,
maxSize: 200,
cell: props => new Date(props.cell.getValue()).toLocaleString(intl.locale),
enableSorting: true
})
2023-08-26 17:26:49 +03:00
], [intl, getUserLabel, user]);
2023-07-15 17:46:19 +03:00
return (
2023-08-26 19:39:49 +03:00
<div>
<div className='relative w-full'>
<div className='absolute top-[-0.125rem] left-0 flex gap-1 ml-3 z-pop'>
2023-08-26 19:39:49 +03:00
<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>
<DataTable
2023-07-15 17:46:19 +03:00
columns={columns}
2023-08-25 22:51:20 +03:00
data={items}
// defaultSortFieldId='time_update'
// defaultSortAsc={false}
2023-07-22 12:24:14 +03:00
noDataComponent={
<div className='flex flex-col gap-4 justify-center p-2 text-center min-h-[10rem]'>
<p><b>Список схем пуст</b></p>
<p>
<TextURL text='Создать схему' href='/rsform-create'/>
<span> | </span>
2023-08-27 15:39:49 +03:00
<TextURL text='Все схемы' href='/library'/>
<span> | </span>
2023-08-16 18:56:48 +03:00
<span className='cursor-pointer hover:underline text-url' onClick={cleanQuery}>
<b>Очистить фильтр</b>
</span>
</p>
</div>}
2023-07-22 12:24:14 +03:00
// pagination
// paginationPerPage={50}
// paginationRowsPerPageOptions={[10, 20, 30, 50, 100]}
2023-07-15 17:46:19 +03:00
onRowClicked={openRSForm}
/>
2023-08-26 19:39:49 +03:00
</div>
2023-07-15 17:46:19 +03:00
);
}
2023-07-28 00:03:37 +03:00
export default ViewLibrary;