From ca0f5bc90760ee679912590be8f00656f5a661c6 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Mon, 7 Jul 2025 15:26:19 -0300 Subject: [PATCH] refactor: Extract bulk download logic to custom hook (#8910) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ (custom-handle-bulk-files-download.tsx): introduce custom hook useCustomHandleBulkFilesDownload to handle bulk file downloads in a more customized way 🔧 (index.tsx): replace direct usage of useGetDownloadFilesV2 with the new custom hook useCustomHandleBulkFilesDownload for handling bulk file downloads in a more efficient and maintainable manner * Update src/frontend/src/customization/hooks/custom-handle-bulk-files-download.tsx Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../custom-handle-bulk-files-download.tsx | 36 +++++++++++++++++++ .../pages/MainPage/pages/filesPage/index.tsx | 30 +++++----------- 2 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 src/frontend/src/customization/hooks/custom-handle-bulk-files-download.tsx diff --git a/src/frontend/src/customization/hooks/custom-handle-bulk-files-download.tsx b/src/frontend/src/customization/hooks/custom-handle-bulk-files-download.tsx new file mode 100644 index 000000000..0103d719d --- /dev/null +++ b/src/frontend/src/customization/hooks/custom-handle-bulk-files-download.tsx @@ -0,0 +1,36 @@ +import { useGetDownloadFilesV2 } from "@/controllers/API/queries/file-management/use-get-download-files"; + +export const useCustomHandleBulkFilesDownload = () => { + const { mutate: downloadFiles } = useGetDownloadFilesV2(); + + const handleBulkDownload = ( + selectedFiles: any, + setSuccessData: (data: { title: string }) => void, + setErrorData: (data: { title: string; list: string[] }) => void, + setIsDownloading: (isDownloading: boolean) => void, + ) => { + setIsDownloading(true); + downloadFiles( + { + ids: selectedFiles.map((file) => file.id), + }, + { + onSuccess: (data) => { + setSuccessData({ title: data.message }); + setIsDownloading(false); + }, + onError: (error) => { + setErrorData({ + title: "Error downloading files", + list: [ + error.message || "An error occurred while downloading the files", + ], + }); + setIsDownloading(false); + }, + }, + ); + }; + + return { handleBulkDownload }; +}; diff --git a/src/frontend/src/pages/MainPage/pages/filesPage/index.tsx b/src/frontend/src/pages/MainPage/pages/filesPage/index.tsx index 1774e45a5..71bd99221 100644 --- a/src/frontend/src/pages/MainPage/pages/filesPage/index.tsx +++ b/src/frontend/src/pages/MainPage/pages/filesPage/index.tsx @@ -8,8 +8,8 @@ import Loading from "@/components/ui/loading"; import { SidebarTrigger } from "@/components/ui/sidebar"; import { useGetFilesV2 } from "@/controllers/API/queries/file-management"; import { useDeleteFilesV2 } from "@/controllers/API/queries/file-management/use-delete-files"; -import { useGetDownloadFilesV2 } from "@/controllers/API/queries/file-management/use-get-download-files"; import { usePostRenameFileV2 } from "@/controllers/API/queries/file-management/use-put-rename-file"; +import { useCustomHandleBulkFilesDownload } from "@/customization/hooks/custom-handle-bulk-files-download"; import { customPostUploadFileV2 } from "@/customization/hooks/use-custom-post-upload-file"; import useUploadFile from "@/hooks/files/use-upload-file"; import DeleteConfirmationModal from "@/modals/deleteConfirmationModal"; @@ -37,6 +37,7 @@ export const FilesPage = () => { const [selectedFiles, setSelectedFiles] = useState([]); const [quantitySelected, setQuantitySelected] = useState(0); const [isShiftPressed, setIsShiftPressed] = useState(false); + const [isDownloading, setIsDownloading] = useState(false); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { @@ -75,8 +76,7 @@ export const FilesPage = () => { const { mutate: rename } = usePostRenameFileV2(); const { mutate: deleteFiles, isPending: isDeleting } = useDeleteFilesV2(); - const { mutate: downloadFiles, isPending: isDownloading } = - useGetDownloadFilesV2(); + const { handleBulkDownload } = useCustomHandleBulkFilesDownload(); const handleRename = (params: NewValueParams) => { rename({ @@ -251,26 +251,14 @@ export const FilesPage = () => { }; const handleDownload = () => { - console.log(selectedFiles); - downloadFiles( - { - ids: selectedFiles.map((file) => file.id), - }, - { - onSuccess: (data) => { - setSuccessData({ title: data.message }); - }, - onError: (error) => { - setErrorData({ - title: "Error downloading files", - list: [ - error.message || "An error occurred while downloading the files", - ], - }); - }, - }, + handleBulkDownload( + selectedFiles, + setSuccessData, + setErrorData, + setIsDownloading, ); }; + const handleDelete = () => { deleteFiles( {