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-03-12 12:04:50 +03:00
|
|
|
import { MiniButton } from '@/components/control';
|
|
|
|
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-03-12 11:55:43 +03:00
|
|
|
import { useApplyLibraryFilter } from '../../backend/use-apply-library-filter';
|
|
|
|
import { useLibrarySuspense } from '../../backend/use-library';
|
|
|
|
import { useRenameLocation } from '../../backend/use-rename-location';
|
|
|
|
import { useCreateLibraryFilter, useLibrarySearchStore } from '../../stores/library-search';
|
2025-02-12 21:36:25 +03:00
|
|
|
|
2025-03-12 11:55:43 +03:00
|
|
|
import { TableLibraryItems } from './table-library-items';
|
|
|
|
import { ToolbarSearch } from './toolbar-search';
|
|
|
|
import { ViewSideLocation } from './view-side-location';
|
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-18 22:28:27 +03:00
|
|
|
<ToolbarSearch className='top-0 h-9' total={libraryItems.length} filtered={filtered.length} />
|
2025-03-07 20:38:40 +03:00
|
|
|
<div className='relative cc-fade-in flex'>
|
2024-08-22 22:41:41 +03:00
|
|
|
<MiniButton
|
|
|
|
title='Выгрузить в формате CSV'
|
2025-03-20 11:33:42 +03:00
|
|
|
className='absolute z-tooltip -top-8 right-6'
|
2024-08-22 22:41:41 +03:00
|
|
|
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
|
|
|
}
|