mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-15 05:31:33 +03:00
2226eef06b
* feat(brains): remove unrelevent buttons on management page * feat: improve feed input ux * feat: update brain knowledge context add knowledge to feed logic * feat: allow to drop files everywhere in the chat section * style: add slide in effect when opening feed card * test(chatPage): fix failing tests
73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { FileRejection, useDropzone } from "react-dropzone";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { FeedItemUploadType } from "@/app/chat/[chatId]/components/ActionsBar/types";
|
|
import { useEventTracking } from "@/services/analytics/june/useEventTracking";
|
|
|
|
import { useToast } from "./useToast";
|
|
import { useKnowledgeContext } from "../context/KnowledgeProvider/hooks/useKnowledgeContext";
|
|
import { acceptedFormats } from "../helpers/acceptedFormats";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useCustomDropzone = () => {
|
|
const { knowledgeToFeed, addKnowledgeToFeed } = useKnowledgeContext();
|
|
|
|
const files: File[] = (
|
|
knowledgeToFeed.filter((c) => c.source === "upload") as FeedItemUploadType[]
|
|
).map((c) => c.file);
|
|
|
|
const { publish } = useToast();
|
|
const { track } = useEventTracking();
|
|
|
|
const { t } = useTranslation(["upload"]);
|
|
|
|
const onDrop = (acceptedFiles: File[], fileRejections: FileRejection[]) => {
|
|
if (fileRejections.length > 0) {
|
|
const firstRejection = fileRejections[0];
|
|
|
|
if (firstRejection.errors[0].code === "file-invalid-type") {
|
|
publish({ variant: "danger", text: t("invalidFileType") });
|
|
} else {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("maxSizeError", { ns: "upload" }),
|
|
});
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
for (const file of acceptedFiles) {
|
|
const isAlreadyInFiles =
|
|
files.filter((f) => f.name === file.name && f.size === file.size)
|
|
.length > 0;
|
|
if (isAlreadyInFiles) {
|
|
publish({
|
|
variant: "warning",
|
|
text: t("alreadyAdded", { fileName: file.name, ns: "upload" }),
|
|
});
|
|
} else {
|
|
void track("FILE_UPLOADED");
|
|
addKnowledgeToFeed({
|
|
source: "upload",
|
|
file: file,
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
const { getInputProps, getRootProps, isDragActive, open } = useDropzone({
|
|
onDrop,
|
|
noClick: true,
|
|
maxSize: 100000000, // 1 MB
|
|
accept: acceptedFormats,
|
|
});
|
|
|
|
return {
|
|
getInputProps,
|
|
getRootProps,
|
|
isDragActive,
|
|
open,
|
|
};
|
|
};
|