2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
2024-08-22 22:41:29 +03:00
|
|
|
import fileDownload from 'js-file-download';
|
2024-08-21 16:49:04 +03:00
|
|
|
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';
|
2025-01-14 21:57:32 +03:00
|
|
|
import { useAppLayoutStore } from '@/stores/appLayout';
|
2025-01-16 16:31:03 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2025-01-23 19:41:31 +03:00
|
|
|
import { useCreateLibraryFilter, useLibrarySearchStore } from '@/stores/librarySearch';
|
2024-08-21 16:49:04 +03:00
|
|
|
import { information } from '@/utils/labels';
|
2025-01-15 16:06:18 +03:00
|
|
|
import { convertToCSV } from '@/utils/utils';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2024-06-26 19:47:05 +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();
|
|
|
|
|
2025-01-14 21:57:32 +03:00
|
|
|
const noNavigation = useAppLayoutStore(state => state.noNavigation);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-01-15 16:06:18 +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
|
|
|
|
2025-01-16 16:31:03 +03:00
|
|
|
const showChangeLocation = useDialogsStore(state => state.showChangeLocation);
|
2024-08-21 16:49:04 +03:00
|
|
|
|
2025-01-16 16:31:03 +03:00
|
|
|
function handleRenameLocation(newLocation: string) {
|
2025-01-23 19:41:31 +03:00
|
|
|
renameLocation(
|
|
|
|
{
|
|
|
|
target: location,
|
|
|
|
new_location: newLocation
|
|
|
|
},
|
2025-01-28 19:45:31 +03:00
|
|
|
() => setLocation(newLocation)
|
2025-01-23 19:41:31 +03:00
|
|
|
);
|
2025-01-16 16:31:03 +03:00
|
|
|
}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-01-16 16:31: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);
|
|
|
|
}
|
2025-01-16 16:31:03 +03:00
|
|
|
}
|
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
|
2025-01-14 21:57:32 +03:00
|
|
|
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'>
|
2024-12-13 21:30:49 +03:00
|
|
|
<ViewSideLocation
|
2025-01-15 16:06:18 +03:00
|
|
|
isVisible={folderMode}
|
2025-01-16 16:31:03 +03:00
|
|
|
onRenameLocation={() => showChangeLocation({ initial: location, onChangeLocation: handleRenameLocation })}
|
2024-12-13 21:30:49 +03:00
|
|
|
/>
|
|
|
|
|
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;
|