Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add use-download-files hook for downloading files on chat #3399

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/frontend/src/controllers/API/queries/files/index.ts
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";
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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}`);
console.log(response);
anovazzi1 marked this conversation as resolved.
Show resolved Hide resolved
const blob = await response.blob();
console.log(blob);
const url = URL.createObjectURL(blob);
console.log(url);

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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ export default function FileCardWrapper({
{formatFileName(name, 50)}
<ForwardedIconComponent name={show ? "ChevronDown" : "ChevronRight"} />
</span>
<FileCard
showFile={show}
fileName={name}
fileType={type}
content={path}
/>
<FileCard showFile={show} fileName={name} fileType={type} path={path} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useGetDownloadFileMutation } from "@/controllers/API/queries/files";
import { useState } from "react";
import { ForwardedIconComponent } from "../../../../../components/genericIconComponent";
import { BACKEND_URL, BASE_URL_API } from "../../../../../constants/constants";
Expand All @@ -6,18 +7,20 @@ import { fileCardPropsType } from "../../../../../types/components";
import formatFileName from "../filePreviewChat/utils/format-file-name";
import DownloadButton from "./components/downloadButton/downloadButton";
import getClasses from "./utils/get-classes";
import handleDownload from "./utils/handle-download";

const imgTypes = new Set(["png", "jpg", "jpeg", "gif", "webp", "image"]);

export default function FileCard({
fileName,
content,
path,
fileType,
showFile = true,
}: fileCardPropsType): JSX.Element | undefined {
const [isHovered, setIsHovered] = useState(false);
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
const { mutate } = useGetDownloadFileMutation({
filename: fileName,
path: path,
});
function handleMouseEnter(): void {
setIsHovered(true);
}
Expand All @@ -27,7 +30,7 @@ export default function FileCard({

const fileWrapperClasses = getClasses(isHovered);

const imgSrc = `${BASE_URL_API}files/images/${content}`;
const imgSrc = `${BASE_URL_API}files/images/${path}`;

if (showFile) {
if (imgTypes.has(fileType)) {
Expand All @@ -46,7 +49,7 @@ export default function FileCard({
/>
<DownloadButton
isHovered={isHovered}
handleDownload={() => handleDownload({ fileName, content })}
handleDownload={() => mutate(undefined)}
/>
</div>
</div>
Expand All @@ -56,7 +59,7 @@ export default function FileCard({
return (
<div
className={fileWrapperClasses}
onClick={() => handleDownload({ fileName, content })}
onClick={() => mutate(undefined)}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
Expand All @@ -69,7 +72,7 @@ export default function FileCard({
</div>
<DownloadButton
isHovered={isHovered}
handleDownload={() => handleDownload({ fileName, content })}
handleDownload={() => mutate(undefined)}
/>
</div>
);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default function ChatView({

const is_ai =
sender === "Machine" || sender === null || sender === undefined;

return {
isSend: !is_ai,
message,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/types/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ export interface Props {

export type fileCardPropsType = {
fileName: string;
content: string;
path: string;
fileType: string;
showFile?: boolean;
};
Expand Down
Loading