2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2024-08-21 16:49:17 +03:00
|
|
|
import { toast } from 'react-toastify';
|
2025-02-12 21:36:25 +03:00
|
|
|
import fileDownload from 'js-file-download';
|
2023-11-26 02:24:16 +03:00
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
import { MiniButton } from '@/components/Control';
|
2024-08-22 22:41:41 +03:00
|
|
|
import { IconCSV } from '@/components/Icons';
|
2025-01-16 16:31:32 +03:00
|
|
|
import { useDialogsStore } from '@/stores/dialogs';
|
2025-02-12 01:35:41 +03:00
|
|
|
import { infoMsg } from '@/utils/labels';
|
2025-01-15 16:06:42 +03:00
|
|
|
import { convertToCSV } from '@/utils/utils';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
2025-02-10 01:32:55 +03:00
|
|
|
import { useApplyLibraryFilter } from '../../backend/useApplyLibraryFilter';
|
|
|
|
import { useLibrarySuspense } from '../../backend/useLibrary';
|
|
|
|
import { useRenameLocation } from '../../backend/useRenameLocation';
|
|
|
|
import { useCreateLibraryFilter, useLibrarySearchStore } from '../../stores/librarySearch';
|
2025-02-12 21:36:25 +03:00
|
|
|
|
2025-02-19 23:30:35 +03:00
|
|
|
import { TableLibraryItems } from './TableLibraryItems';
|
|
|
|
import { ToolbarSearch } from './ToolbarSearch';
|
|
|
|
import { ViewSideLocation } from './ViewSideLocation';
|
2023-11-26 02:24:16 +03:00
|
|
|
|
2025-01-29 23:18:20 +03:00
|
|
|
export function LibraryPage() {
|
2025-01-27 15:03:48 +03:00
|
|
|
const { items: libraryItems } = useLibrarySuspense();
|
|
|
|
const { renameLocation } = useRenameLocation();
|
|
|
|
|
2025-01-15 16:06:42 +03:00
|
|
|
const folderMode = useLibrarySearchStore(state => state.folderMode);
|
|
|
|
const location = useLibrarySearchStore(state => state.location);
|
|
|
|
const setLocation = useLibrarySearchStore(state => state.setLocation);
|
2024-06-02 23:41:46 +03:00
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
const filter = useCreateLibraryFilter();
|
|
|
|
const { filtered } = useApplyLibraryFilter(filter);
|
2024-06-18 15:07:41 +03:00
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
const showChangeLocation = useDialogsStore(state => state.showChangeLocation);
|
2024-08-21 16:49:17 +03:00
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
function handleRenameLocation(newLocation: string) {
|
2025-02-11 20:16:11 +03:00
|
|
|
void renameLocation({
|
|
|
|
target: location,
|
|
|
|
new_location: newLocation
|
|
|
|
}).then(() => setLocation(newLocation));
|
2025-01-16 16:31:32 +03:00
|
|
|
}
|
2023-11-26 02:24:16 +03:00
|
|
|
|
2025-01-16 16:31:32 +03:00
|
|
|
function handleDownloadCSV() {
|
2025-01-27 15:03:48 +03:00
|
|
|
if (filtered.length === 0) {
|
2025-02-12 01:35:41 +03:00
|
|
|
toast.error(infoMsg.noDataToExport);
|
2024-08-22 22:41:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2025-01-27 15:03:48 +03:00
|
|
|
const blob = convertToCSV(filtered);
|
2024-08-22 22:41:41 +03:00
|
|
|
try {
|
|
|
|
fileDownload(blob, 'library.csv', 'text/csv;charset=utf-8;');
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2025-01-16 16:31:32 +03:00
|
|
|
}
|
2024-08-22 22:41:41 +03:00
|
|
|
|
2023-11-26 02:24:16 +03:00
|
|
|
return (
|
2025-01-27 15:03:48 +03:00
|
|
|
<>
|
2025-03-07 20:38:40 +03:00
|
|
|
<ToolbarSearch total={libraryItems.length} filtered={filtered.length} />
|
|
|
|
<div className='relative cc-fade-in flex'>
|
2024-08-22 22:41:41 +03:00
|
|
|
<MiniButton
|
2025-03-09 21:59:21 +03:00
|
|
|
className='absolute z-tooltip top-1 right-0 cc-animate-position'
|
2024-08-22 22:41:41 +03:00
|
|
|
title='Выгрузить в формате CSV'
|
|
|
|
icon={<IconCSV size='1.25rem' className='icon-green' />}
|
|
|
|
onClick={handleDownloadCSV}
|
|
|
|
/>
|
2024-06-19 22:10:15 +03:00
|
|
|
|
2024-12-13 21:31:09 +03:00
|
|
|
<ViewSideLocation
|
2025-01-15 16:06:42 +03:00
|
|
|
isVisible={folderMode}
|
2025-01-16 16:31:32 +03:00
|
|
|
onRenameLocation={() => showChangeLocation({ initial: location, onChangeLocation: handleRenameLocation })}
|
2024-12-13 21:31:09 +03:00
|
|
|
/>
|
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
<TableLibraryItems items={filtered} />
|
2024-06-19 22:10:15 +03:00
|
|
|
</div>
|
2025-01-27 15:03:48 +03:00
|
|
|
</>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-11-26 02:24:16 +03:00
|
|
|
}
|