Portal/rsconcept/frontend/src/pages/LibraryPage/LibraryPage.tsx

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
2024-08-22 22:41:29 +03:00
import fileDownload from 'js-file-download';
import { toast } from 'react-toastify';
2024-06-07 20:17:03 +03:00
2025-01-23 19:41:31 +03:00
import { useApplyLibraryFilter } from '@/backend/library/useApplyLibraryFilter';
import { useLibrarySuspense } from '@/backend/library/useLibrary';
import { useRenameLocation } from '@/backend/library/useRenameLocation';
2024-08-22 22:41:29 +03:00
import { IconCSV } from '@/components/Icons';
import MiniButton from '@/components/ui/MiniButton';
import Overlay from '@/components/ui/Overlay';
import { useAppLayoutStore } from '@/stores/appLayout';
import { useDialogsStore } from '@/stores/dialogs';
2025-01-23 19:41:31 +03:00
import { useCreateLibraryFilter, useLibrarySearchStore } from '@/stores/librarySearch';
import { information } from '@/utils/labels';
import { convertToCSV } from '@/utils/utils';
2024-06-07 20:17:03 +03:00
import TableLibraryItems from './TableLibraryItems';
import ToolbarSearch from './ToolbarSearch';
2024-06-27 11:34:52 +03:00
import ViewSideLocation from './ViewSideLocation';
2024-06-07 20:17:03 +03:00
function LibraryPage() {
2025-01-23 19:41:31 +03:00
const { items: libraryItems } = useLibrarySuspense();
const { renameLocation } = useRenameLocation();
const noNavigation = useAppLayoutStore(state => state.noNavigation);
2024-06-07 20:17:03 +03:00
const folderMode = useLibrarySearchStore(state => state.folderMode);
const location = useLibrarySearchStore(state => state.location);
const setLocation = useLibrarySearchStore(state => state.setLocation);
2024-06-07 20:17:03 +03:00
2025-01-23 19:41:31 +03:00
const filter = useCreateLibraryFilter();
const { filtered } = useApplyLibraryFilter(filter);
2024-06-18 15:06:52 +03:00
const showChangeLocation = useDialogsStore(state => state.showChangeLocation);
function handleRenameLocation(newLocation: string) {
2025-01-23 19:41:31 +03:00
renameLocation(
{
target: location,
new_location: newLocation
},
() => setLocation(newLocation)
2025-01-23 19:41:31 +03:00
);
}
2024-06-07 20:17:03 +03:00
function handleDownloadCSV() {
2025-01-23 19:41:31 +03:00
if (filtered.length === 0) {
2024-08-22 22:41:29 +03:00
toast.error(information.noDataToExport);
return;
}
2025-01-23 19:41:31 +03:00
const blob = convertToCSV(filtered);
2024-08-22 22:41:29 +03:00
try {
fileDownload(blob, 'library.csv', 'text/csv;charset=utf-8;');
} catch (error) {
console.error(error);
}
}
2024-08-22 22:41:29 +03:00
2024-06-07 20:17:03 +03:00
return (
2025-01-23 19:41:31 +03:00
<>
2024-09-15 21:18:32 +03:00
<Overlay
position={noNavigation ? 'top-[0.25rem] right-[3rem]' : 'top-[0.25rem] right-0'}
2024-09-15 21:18:32 +03:00
layer='z-tooltip'
2024-12-11 23:37:23 +03:00
className='cc-animate-position'
2024-09-15 21:18:32 +03:00
>
2024-08-22 22:41:29 +03:00
<MiniButton
title='Выгрузить в формате CSV'
icon={<IconCSV size='1.25rem' className='icon-green' />}
onClick={handleDownloadCSV}
/>
</Overlay>
2025-01-23 19:41:31 +03:00
<ToolbarSearch total={libraryItems.length} filtered={filtered.length} />
2024-06-19 22:09:31 +03:00
2024-12-12 13:17:24 +03:00
<div className='cc-fade-in flex'>
<ViewSideLocation
isVisible={folderMode}
onRenameLocation={() => showChangeLocation({ initial: location, onChangeLocation: handleRenameLocation })}
/>
2025-01-23 19:41:31 +03:00
<TableLibraryItems items={filtered} />
2024-06-19 22:09:31 +03:00
</div>
2025-01-23 19:41:31 +03:00
</>
2024-06-07 20:17:03 +03:00
);
}
export default LibraryPage;