2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2024-08-22 22:41:41 +03:00
|
|
|
import fileDownload from 'js-file-download';
|
2025-01-15 16:06:42 +03:00
|
|
|
import { useCallback, useState } from 'react';
|
2024-08-21 16:49:17 +03:00
|
|
|
import { toast } from 'react-toastify';
|
2023-11-26 02:24:16 +03:00
|
|
|
|
2024-08-22 22:41:41 +03:00
|
|
|
import { IconCSV } from '@/components/Icons';
|
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
|
|
|
import Overlay from '@/components/ui/Overlay';
|
2024-03-20 15:27:32 +03:00
|
|
|
import DataLoader from '@/components/wrap/DataLoader';
|
2023-12-13 14:32:57 +03:00
|
|
|
import { useLibrary } from '@/context/LibraryContext';
|
2024-08-21 16:49:17 +03:00
|
|
|
import DlgChangeLocation from '@/dialogs/DlgChangeLocation';
|
2025-01-15 16:06:42 +03:00
|
|
|
import { IRenameLocationData } from '@/models/library';
|
2025-01-14 21:58:16 +03:00
|
|
|
import { useAppLayoutStore } from '@/stores/appLayout';
|
2025-01-15 16:06:42 +03:00
|
|
|
import { useLibraryFilter, useLibrarySearchStore } from '@/stores/librarySearch';
|
2024-08-21 16:49:17 +03:00
|
|
|
import { information } from '@/utils/labels';
|
2025-01-15 16:06:42 +03:00
|
|
|
import { convertToCSV } from '@/utils/utils';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
2024-06-26 19:47:31 +03:00
|
|
|
import TableLibraryItems from './TableLibraryItems';
|
|
|
|
import ToolbarSearch from './ToolbarSearch';
|
2024-06-27 11:35:26 +03:00
|
|
|
import ViewSideLocation from './ViewSideLocation';
|
2023-11-26 02:24:16 +03:00
|
|
|
|
|
|
|
function LibraryPage() {
|
|
|
|
const library = useLibrary();
|
2025-01-14 21:58:16 +03:00
|
|
|
const noNavigation = useAppLayoutStore(state => state.noNavigation);
|
2023-11-26 02:24:16 +03:00
|
|
|
|
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-15 16:06:42 +03:00
|
|
|
const filter = useLibraryFilter();
|
|
|
|
const items = library.applyFilter(filter);
|
2024-06-18 15:07:41 +03:00
|
|
|
|
2025-01-15 16:06:42 +03:00
|
|
|
const [showRenameLocation, setShowRenameLocation] = useState(false);
|
2024-08-21 16:49:17 +03:00
|
|
|
|
|
|
|
const handleRenameLocation = useCallback(
|
|
|
|
(newLocation: string) => {
|
|
|
|
const data: IRenameLocationData = {
|
2025-01-15 16:06:42 +03:00
|
|
|
target: location,
|
2024-08-21 16:49:17 +03:00
|
|
|
new_location: newLocation
|
|
|
|
};
|
|
|
|
library.renameLocation(data, () => {
|
2025-01-15 16:06:42 +03:00
|
|
|
setLocation(newLocation);
|
2024-08-21 16:49:17 +03:00
|
|
|
toast.success(information.locationRenamed);
|
|
|
|
});
|
|
|
|
},
|
2025-01-15 16:06:42 +03:00
|
|
|
[location, setLocation, library]
|
2024-08-21 16:49:17 +03:00
|
|
|
);
|
2023-11-26 02:24:16 +03:00
|
|
|
|
2024-08-22 22:41:41 +03:00
|
|
|
const handleDownloadCSV = useCallback(() => {
|
|
|
|
if (items.length === 0) {
|
|
|
|
toast.error(information.noDataToExport);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const blob = convertToCSV(items);
|
|
|
|
try {
|
|
|
|
fileDownload(blob, 'library.csv', 'text/csv;charset=utf-8;');
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}, [items]);
|
|
|
|
|
2023-11-26 02:24:16 +03:00
|
|
|
return (
|
2024-12-12 13:19:12 +03:00
|
|
|
<DataLoader isLoading={library.loading} error={library.loadingError} hasNoData={library.items.length === 0}>
|
2024-08-21 16:49:17 +03:00
|
|
|
{showRenameLocation ? (
|
|
|
|
<DlgChangeLocation
|
2025-01-15 16:06:42 +03:00
|
|
|
initial={location}
|
2024-08-21 16:49:17 +03:00
|
|
|
onChangeLocation={handleRenameLocation}
|
|
|
|
hideWindow={() => setShowRenameLocation(false)}
|
|
|
|
/>
|
|
|
|
) : null}
|
2024-09-15 21:19:17 +03:00
|
|
|
<Overlay
|
2025-01-14 21:58:16 +03:00
|
|
|
position={noNavigation ? 'top-[0.25rem] right-[3rem]' : 'top-[0.25rem] right-0'}
|
2024-09-15 21:19:17 +03:00
|
|
|
layer='z-tooltip'
|
2024-12-12 13:19:12 +03:00
|
|
|
className='cc-animate-position'
|
2024-09-15 21:19:17 +03:00
|
|
|
>
|
2024-08-22 22:41:41 +03:00
|
|
|
<MiniButton
|
|
|
|
title='Выгрузить в формате CSV'
|
|
|
|
icon={<IconCSV size='1.25rem' className='icon-green' />}
|
|
|
|
onClick={handleDownloadCSV}
|
|
|
|
/>
|
|
|
|
</Overlay>
|
2025-01-15 16:06:42 +03:00
|
|
|
<ToolbarSearch total={library.items.length ?? 0} filtered={items.length} />
|
2024-06-19 22:10:15 +03:00
|
|
|
|
2024-12-12 13:19:12 +03:00
|
|
|
<div className='cc-fade-in flex'>
|
2024-12-13 21:31:09 +03:00
|
|
|
<ViewSideLocation
|
2025-01-15 16:06:42 +03:00
|
|
|
isVisible={folderMode}
|
2024-12-13 21:31:09 +03:00
|
|
|
folderTree={library.folders}
|
|
|
|
onRenameLocation={() => setShowRenameLocation(true)}
|
|
|
|
/>
|
|
|
|
|
2025-01-15 16:06:42 +03:00
|
|
|
<TableLibraryItems items={items} />
|
2024-06-19 22:10:15 +03:00
|
|
|
</div>
|
2024-01-07 03:29:16 +03:00
|
|
|
</DataLoader>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-11-26 02:24:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default LibraryPage;
|