2023-08-25 11:33:14 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-05-23 09:15:13 +03:00
|
|
|
"use client";
|
2023-06-15 12:52:46 +03:00
|
|
|
import {
|
|
|
|
Dispatch,
|
|
|
|
forwardRef,
|
|
|
|
RefObject,
|
|
|
|
SetStateAction,
|
|
|
|
useState,
|
|
|
|
} from "react";
|
2023-08-25 11:33:14 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-06-13 17:33:41 +03:00
|
|
|
import Button from "@/lib/components/ui/Button";
|
|
|
|
import { AnimatedCard } from "@/lib/components/ui/Card";
|
|
|
|
import Ellipsis from "@/lib/components/ui/Ellipsis";
|
2023-07-12 16:45:45 +03:00
|
|
|
import { Modal } from "@/lib/components/ui/Modal";
|
2023-08-25 11:33:14 +03:00
|
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
2023-06-20 17:17:13 +03:00
|
|
|
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
2023-06-28 20:39:27 +03:00
|
|
|
import { useAxios, useToast } from "@/lib/hooks";
|
2023-06-15 12:52:46 +03:00
|
|
|
import { Document } from "@/lib/types/Document";
|
2023-06-26 20:23:48 +03:00
|
|
|
import { useEventTracking } from "@/services/analytics/useEventTracking";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-05-23 09:15:13 +03:00
|
|
|
import DocumentData from "./DocumentData";
|
|
|
|
|
|
|
|
interface DocumentProps {
|
|
|
|
document: Document;
|
|
|
|
setDocuments: Dispatch<SetStateAction<Document[]>>;
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:26:49 +03:00
|
|
|
const DocumentItem = forwardRef(
|
|
|
|
({ document, setDocuments }: DocumentProps, forwardedRef) => {
|
|
|
|
const [isDeleting, setIsDeleting] = useState(false);
|
|
|
|
const { publish } = useToast();
|
|
|
|
const { session } = useSupabase();
|
2023-06-02 18:01:49 +03:00
|
|
|
const { axiosInstance } = useAxios();
|
2023-06-26 20:23:48 +03:00
|
|
|
const { track } = useEventTracking();
|
2023-06-28 20:39:27 +03:00
|
|
|
const { currentBrain } = useBrainContext();
|
2023-06-02 18:01:49 +03:00
|
|
|
|
2023-07-20 19:17:55 +03:00
|
|
|
const canDeleteFile = currentBrain?.role === "Owner";
|
2023-08-25 11:33:14 +03:00
|
|
|
const { t } = useTranslation(["translation", "explore"]);
|
2023-07-19 18:13:02 +03:00
|
|
|
|
2023-05-29 13:26:49 +03:00
|
|
|
if (!session) {
|
2023-08-25 11:33:14 +03:00
|
|
|
throw new Error(t("sessionNotFound", { ns: "explore" }));
|
2023-05-23 09:15:13 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 13:26:49 +03:00
|
|
|
const deleteDocument = async (name: string) => {
|
|
|
|
setIsDeleting(true);
|
2023-06-26 20:23:48 +03:00
|
|
|
void track("DELETE_DOCUMENT");
|
2023-05-29 13:26:49 +03:00
|
|
|
try {
|
2023-08-25 11:33:14 +03:00
|
|
|
if (currentBrain?.id === undefined) {
|
|
|
|
throw new Error(t("noBrain", { ns: "explore" }));
|
|
|
|
}
|
2023-06-28 20:39:27 +03:00
|
|
|
await axiosInstance.delete(
|
|
|
|
`/explore/${name}/?brain_id=${currentBrain.id}`
|
|
|
|
);
|
2023-05-29 13:26:49 +03:00
|
|
|
setDocuments((docs) => docs.filter((doc) => doc.name !== name)); // Optimistic update
|
2023-06-28 20:39:27 +03:00
|
|
|
publish({
|
|
|
|
variant: "success",
|
2023-08-25 11:33:14 +03:00
|
|
|
text: t("deleted", {
|
|
|
|
fileName: name,
|
|
|
|
brain: currentBrain.name,
|
|
|
|
ns: "explore",
|
|
|
|
}),
|
2023-06-28 20:39:27 +03:00
|
|
|
});
|
2023-05-29 13:26:49 +03:00
|
|
|
} catch (error) {
|
2023-08-07 15:13:41 +03:00
|
|
|
publish({
|
|
|
|
variant: "warning",
|
2023-08-25 11:33:14 +03:00
|
|
|
text: t("errorDeleting", { fileName: name, ns: "explore" }),
|
2023-08-07 15:13:41 +03:00
|
|
|
});
|
2023-05-29 13:26:49 +03:00
|
|
|
console.error(`Error deleting ${name}`, error);
|
|
|
|
}
|
|
|
|
setIsDeleting(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AnimatedCard
|
|
|
|
initial={{ x: -64, opacity: 0 }}
|
|
|
|
animate={{ x: 0, opacity: 1 }}
|
|
|
|
exit={{ x: 64, opacity: 0 }}
|
|
|
|
layout
|
|
|
|
ref={forwardedRef as RefObject<HTMLDivElement>}
|
|
|
|
className="flex flex-col sm:flex-row sm:items-center justify-between w-full p-5 gap-5"
|
|
|
|
>
|
2023-06-05 08:53:47 +03:00
|
|
|
<Ellipsis tooltip maxCharacters={30}>
|
|
|
|
{document.name}
|
|
|
|
</Ellipsis>
|
2023-05-29 13:26:49 +03:00
|
|
|
<div className="flex gap-2 self-end">
|
2023-08-25 11:33:14 +03:00
|
|
|
<Modal
|
|
|
|
Trigger={
|
|
|
|
<Button className="">{t("view", { ns: "explore" })}</Button>
|
|
|
|
}
|
|
|
|
>
|
2023-06-02 18:01:49 +03:00
|
|
|
<DocumentData documentName={document.name} />
|
2023-05-29 13:26:49 +03:00
|
|
|
</Modal>
|
|
|
|
|
2023-07-19 18:13:02 +03:00
|
|
|
{canDeleteFile && (
|
|
|
|
<Modal
|
2023-08-25 11:33:14 +03:00
|
|
|
title={t("deleteConfirmTitle", { ns: "explore" })}
|
|
|
|
desc={t("deleteConfirmText", { ns: "explore" })}
|
|
|
|
Trigger={
|
|
|
|
<Button isLoading={isDeleting} variant={"danger"} className="">
|
|
|
|
{t("deleteButton")}
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
CloseTrigger={
|
|
|
|
<Button
|
|
|
|
variant={"danger"}
|
|
|
|
isLoading={isDeleting}
|
|
|
|
onClick={() => {
|
|
|
|
void deleteDocument(document.name);
|
|
|
|
}}
|
|
|
|
className="self-end"
|
|
|
|
>
|
|
|
|
{t("deleteForeverButton")}
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<p>{document.name}</p>
|
|
|
|
</Modal>
|
2023-07-19 18:13:02 +03:00
|
|
|
)}
|
2023-05-29 13:26:49 +03:00
|
|
|
</div>
|
|
|
|
</AnimatedCard>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2023-05-23 09:15:13 +03:00
|
|
|
|
|
|
|
DocumentItem.displayName = "DocumentItem";
|
|
|
|
export default DocumentItem;
|