-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add use-download-files hook for downloading files on chat (#…
…3399) * feat: add useGetDownloadFileMutation hook for downloading files This commit adds a new hook called useGetDownloadFileMutation to handle the downloading of files in the frontend. The hook takes in the path and filename as parameters and uses the fetch API to download the file. It then creates a URL object for the downloaded file and sets it as the href of a dynamically created anchor element. Finally, it triggers a click event on the anchor element to initiate the file download. The URL object is revoked after the download is complete. * feat: add use-download-files hook for downloading files on chat This commit adds a new hook called use-download-files to handle the downloading of files in the frontend. The hook takes in the path and filename as parameters and uses the fetch API to download the file. It then creates a URL object for the downloaded file and sets it as the href of a dynamically created anchor element. Finally, it triggers a click event on the anchor element to initiate the file download. The URL object is revoked after the download is complete. * [autofix.ci] apply automated fixes * feat: refactor file download handling in chat view Refactor the file download handling in the chat view by introducing a new hook called `use-download-files`. This hook takes in the path and filename as parameters and uses the fetch API to download the file. It creates a URL object for the downloaded file and sets it as the href of a dynamically created anchor element. Finally, it triggers a click event on the anchor element to initiate the file download. The URL object is revoked after the download is complete. * remove console.log --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Cristhian Zanforlin Lousa <[email protected]>
- Loading branch information
1 parent
2a27b08
commit 0f2729d
Showing
7 changed files
with
58 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./use-download-files"; | ||
export * from "./use-get-download-images"; | ||
export * from "./use-get-profile-pictures"; | ||
export * from "./use-post-upload-file"; |
45 changes: 45 additions & 0 deletions
45
src/frontend/src/controllers/API/queries/files/use-download-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { keepPreviousData } from "@tanstack/react-query"; | ||
import { | ||
useMutationFunctionType, | ||
useQueryFunctionType, | ||
} from "../../../../types/api"; | ||
import { api } from "../../api"; | ||
import { getURL } from "../../helpers/constants"; | ||
import { UseRequestProcessor } from "../../services/request-processor"; | ||
|
||
interface DownloadImagesQueryParams { | ||
path: string; | ||
filename: string; | ||
} | ||
|
||
export const useGetDownloadFileMutation: useMutationFunctionType< | ||
DownloadImagesQueryParams | ||
> = (params, options) => { | ||
const { mutate } = UseRequestProcessor(); | ||
|
||
const getDownloadImagesFn = async () => { | ||
if (!params) return; | ||
// need to use fetch because axios convert blob data to string, and this convertion can corrupt the file | ||
const response = await fetch(`${getURL("FILES")}/download/${params.path}`); | ||
const blob = await response.blob(); | ||
const url = URL.createObjectURL(blob); | ||
|
||
const link = document.createElement("a"); | ||
link.href = url; | ||
link.setAttribute("download", params.filename); // Set the filename | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
|
||
URL.revokeObjectURL(url); | ||
return {}; | ||
}; | ||
|
||
const queryResult = mutate( | ||
["useGetDownloadFileMutation", params.path], | ||
getDownloadImagesFn, | ||
options, | ||
); | ||
|
||
return queryResult; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
src/frontend/src/modals/IOModal/components/chatView/fileComponent/utils/handle-download.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters