feat: modularize folder download functionality with reusable utilities (#8084)
✨ (index.tsx): Add support for custom download folder functionality to improve file download process 📝 (use-get-download-folders.ts): Update useGetDownloadFolders function to use customGetDownloadTypeFolders for downloading folders 📝 (custom-get-download-folders.ts): Add customGetDownloadTypeFolders and customGetDownloadFolderBlob functions for handling custom download folder logic
This commit is contained in:
parent
2781a3ee7e
commit
ed37473b94
3 changed files with 49 additions and 31 deletions
|
|
@ -30,6 +30,7 @@ import {
|
|||
} from "@/customization/feature-flags";
|
||||
import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate";
|
||||
import { track } from "@/customization/utils/analytics";
|
||||
import { customGetDownloadFolderBlob } from "@/customization/utils/custom-get-download-folders";
|
||||
import { createFileUpload } from "@/helpers/create-file-upload";
|
||||
import { getObjectsFromFilelist } from "@/helpers/get-objects-from-filelist";
|
||||
import useUploadFlow from "@/hooks/flows/use-upload-flow";
|
||||
|
|
@ -169,35 +170,14 @@ const SideBarFoldersButtonsComponent = ({
|
|||
});
|
||||
};
|
||||
|
||||
const handleDownloadFolder = (id: string) => {
|
||||
const handleDownloadFolder = (id: string, folderName: string) => {
|
||||
mutateDownloadFolder(
|
||||
{
|
||||
folderId: id,
|
||||
},
|
||||
{
|
||||
onSuccess: (response) => {
|
||||
// Create a blob from the response data
|
||||
const blob = new Blob([response.data], {
|
||||
type: "application/x-zip-compressed",
|
||||
});
|
||||
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
|
||||
// Get filename from header or use default
|
||||
const filename =
|
||||
response.headers?.["content-disposition"]
|
||||
?.split("filename=")[1]
|
||||
?.replace(/['"]/g, "") ?? "flows.zip";
|
||||
|
||||
link.setAttribute("download", filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
|
||||
track("Project Exported", { folderId: id });
|
||||
customGetDownloadFolderBlob(response, id, folderName, setSuccessData);
|
||||
},
|
||||
onError: (e) => {
|
||||
setErrorData({
|
||||
|
|
@ -460,7 +440,9 @@ const SideBarFoldersButtonsComponent = ({
|
|||
item={item}
|
||||
index={index}
|
||||
handleDeleteFolder={handleDeleteFolder}
|
||||
handleDownloadFolder={handleDownloadFolder}
|
||||
handleDownloadFolder={() =>
|
||||
handleDownloadFolder(item.id!, item.name)
|
||||
}
|
||||
handleSelectFolderToRename={
|
||||
handleSelectFolderToRename
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { customGetDownloadTypeFolders } from "@/customization/utils/custom-get-download-folders";
|
||||
import { useMutationFunctionType } from "@/types/api";
|
||||
import { UseMutationResult } from "@tanstack/react-query";
|
||||
import { api } from "../../api";
|
||||
import { getURL } from "../../helpers/constants";
|
||||
import { UseRequestProcessor } from "../../services/request-processor";
|
||||
|
||||
interface IGetDownloadFolders {
|
||||
folderId: string;
|
||||
}
|
||||
|
|
@ -19,12 +19,7 @@ export const useGetDownloadFolders: useMutationFunctionType<
|
|||
): Promise<any> => {
|
||||
const response = await api.get<any>(
|
||||
`${getURL("PROJECTS")}/download/${payload.folderId}`,
|
||||
{
|
||||
responseType: "blob",
|
||||
headers: {
|
||||
Accept: "application/x-zip-compressed",
|
||||
},
|
||||
},
|
||||
customGetDownloadTypeFolders(),
|
||||
);
|
||||
return response;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
import { AxiosRequestConfig, ResponseType } from "axios";
|
||||
import { track } from "./analytics";
|
||||
|
||||
export const customGetDownloadTypeFolders = (): AxiosRequestConfig => {
|
||||
return {
|
||||
responseType: "blob" as ResponseType,
|
||||
headers: {
|
||||
Accept: "application/x-zip-compressed",
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const customGetDownloadFolderBlob = (
|
||||
response: any,
|
||||
id: string,
|
||||
folderName?: string,
|
||||
setSuccessData?: (data: any) => void,
|
||||
) => {
|
||||
// Create a blob from the response data
|
||||
const blob = new Blob([response.data], {
|
||||
type: "application/x-zip-compressed",
|
||||
});
|
||||
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
|
||||
// Get filename from header or use default
|
||||
const filename =
|
||||
response.headers?.["content-disposition"]
|
||||
?.split("filename=")[1]
|
||||
?.replace(/['"]/g, "") ?? "flows.zip";
|
||||
|
||||
link.setAttribute("download", filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
|
||||
track("Project Exported", { folderId: id });
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue