From 3769795f9ec7a7f23b618b5b5f9f216d516b8660 Mon Sep 17 00:00:00 2001 From: !MAD! <61308761+iMADi-ARCH@users.noreply.github.com> Date: Mon, 29 May 2023 15:56:49 +0530 Subject: [PATCH] Fix/poplayout warning (#194) * fix(auth): use redirect instead of router * fix(auth): use router for after render redirects * fix(anims): forward ref in file component * fix(anims): forward ref in document item * fix(CI) --- frontend/app/explore/DocumentItem/index.tsx | 160 ++++++++++-------- frontend/app/explore/page.tsx | 2 +- .../components/Crawler/hooks/useCrawler.ts | 2 +- .../FileUploader/components/FileComponent.tsx | 76 +++++---- .../FileUploader/hooks/useFileUploader.ts | 2 +- .../upload/components/FileUploader/index.tsx | 2 +- 6 files changed, 130 insertions(+), 114 deletions(-) diff --git a/frontend/app/explore/DocumentItem/index.tsx b/frontend/app/explore/DocumentItem/index.tsx index b9e5a70d8..04e50116b 100644 --- a/frontend/app/explore/DocumentItem/index.tsx +++ b/frontend/app/explore/DocumentItem/index.tsx @@ -3,7 +3,14 @@ import Spinner from "@/app/components/ui/Spinner"; import { useSupabase } from "@/app/supabase-provider"; import { useToast } from "@/lib/hooks/useToast"; import axios from "axios"; -import { Dispatch, SetStateAction, Suspense, useState } from "react"; +import { + Dispatch, + RefObject, + SetStateAction, + Suspense, + forwardRef, + useState, +} from "react"; import Button from "../../components/ui/Button"; import { AnimatedCard } from "../../components/ui/Card"; import Modal from "../../components/ui/Modal"; @@ -15,83 +22,86 @@ interface DocumentProps { setDocuments: Dispatch>; } -const DocumentItem = ({ document, setDocuments }: DocumentProps) => { - const [isDeleting, setIsDeleting] = useState(false); - const { publish } = useToast(); - const { session } = useSupabase(); - if (!session) { - throw new Error("User session not found"); - } - - const deleteDocument = async (name: string) => { - setIsDeleting(true); - try { - await axios.delete( - `${process.env.NEXT_PUBLIC_BACKEND_URL}/explore/${name}`, - { - headers: { - Authorization: `Bearer ${session.access_token}`, - }, - } - ); - setDocuments((docs) => docs.filter((doc) => doc.name !== name)); // Optimistic update - publish({ variant: "success", text: `${name} deleted.` }); - } catch (error) { - console.error(`Error deleting ${name}`, error); +const DocumentItem = forwardRef( + ({ document, setDocuments }: DocumentProps, forwardedRef) => { + const [isDeleting, setIsDeleting] = useState(false); + const { publish } = useToast(); + const { session } = useSupabase(); + if (!session) { + throw new Error("User session not found"); } - setIsDeleting(false); - }; - return ( - -

{document.name}

-
- {/* VIEW MODAL */} - View} - > - }> - {/* @ts-expect-error TODO: check if DocumentData component can be sync */} - - - + const deleteDocument = async (name: string) => { + setIsDeleting(true); + try { + await axios.delete( + `${process.env.NEXT_PUBLIC_BACKEND_URL}/explore/${name}`, + { + headers: { + Authorization: `Bearer ${session.access_token}`, + }, + } + ); + setDocuments((docs) => docs.filter((doc) => doc.name !== name)); // Optimistic update + publish({ variant: "success", text: `${name} deleted.` }); + } catch (error) { + console.error(`Error deleting ${name}`, error); + } + setIsDeleting(false); + }; - {/* DELETE MODAL */} - - Delete - - } - CloseTrigger={ - - } - > -

{document.name}

-
-
-
- ); -}; + return ( + } + className="flex flex-col sm:flex-row sm:items-center justify-between w-full p-5 gap-5" + > +

{document.name}

+
+ {/* VIEW MODAL */} + View} + > + }> + {/* @ts-expect-error TODO: check if DocumentData component can be sync */} + + + + + {/* DELETE MODAL */} + + Delete + + } + CloseTrigger={ + + } + > +

{document.name}

+
+
+
+ ); + } +); DocumentItem.displayName = "DocumentItem"; export default DocumentItem; diff --git a/frontend/app/explore/page.tsx b/frontend/app/explore/page.tsx index 84d43c4b5..52e25b09d 100644 --- a/frontend/app/explore/page.tsx +++ b/frontend/app/explore/page.tsx @@ -41,7 +41,7 @@ export default function ExplorePage() { setIsPending(false); }; fetchDocuments(); - }, []); + }, [session.access_token]); return (
diff --git a/frontend/app/upload/components/Crawler/hooks/useCrawler.ts b/frontend/app/upload/components/Crawler/hooks/useCrawler.ts index 1bdde621e..e8fba9e1b 100644 --- a/frontend/app/upload/components/Crawler/hooks/useCrawler.ts +++ b/frontend/app/upload/components/Crawler/hooks/useCrawler.ts @@ -61,7 +61,7 @@ export const useCrawler = () => { } finally { setCrawling(false); } - }, [session.access_token]); + }, [session.access_token, publish]); return { isCrawling, diff --git a/frontend/app/upload/components/FileUploader/components/FileComponent.tsx b/frontend/app/upload/components/FileUploader/components/FileComponent.tsx index f7aeb1294..fbaa1ccdd 100644 --- a/frontend/app/upload/components/FileUploader/components/FileComponent.tsx +++ b/frontend/app/upload/components/FileUploader/components/FileComponent.tsx @@ -1,41 +1,47 @@ import { motion } from "framer-motion"; -import { Dispatch, SetStateAction } from "react"; +import { Dispatch, RefObject, SetStateAction, forwardRef } from "react"; import { MdClose } from "react-icons/md"; -export const FileComponent = ({ - file, - setFiles, -}: { +interface FileComponentProps { file: File; setFiles: Dispatch>; -}) => { - return ( - -
-
- - {file.name} - - - {(file.size / 1000).toFixed(3)} kb - -
-
- -
- ); -}; +
+
+ + {file.name} + + + {(file.size / 1000).toFixed(3)} kb + +
+
+ + + ); + } +); + +FileComponent.displayName = "FileComponent"; + +export default FileComponent; diff --git a/frontend/app/upload/components/FileUploader/hooks/useFileUploader.ts b/frontend/app/upload/components/FileUploader/hooks/useFileUploader.ts index bad14fbc5..9f12764ee 100644 --- a/frontend/app/upload/components/FileUploader/hooks/useFileUploader.ts +++ b/frontend/app/upload/components/FileUploader/hooks/useFileUploader.ts @@ -45,7 +45,7 @@ export const useFileUploader = () => { }); } }, - [session.access_token] + [session.access_token, publish] ); const onDrop = (acceptedFiles: File[], fileRejections: FileRejection[]) => { diff --git a/frontend/app/upload/components/FileUploader/index.tsx b/frontend/app/upload/components/FileUploader/index.tsx index 1b9dc3a35..68aeffde6 100644 --- a/frontend/app/upload/components/FileUploader/index.tsx +++ b/frontend/app/upload/components/FileUploader/index.tsx @@ -2,7 +2,7 @@ import { AnimatePresence } from "framer-motion"; import Button from "../../../components/ui/Button"; import Card from "../../../components/ui/Card"; -import { FileComponent } from "./components/FileComponent"; +import FileComponent from "./components/FileComponent"; import { useFileUploader } from "./hooks/useFileUploader"; export const FileUploader = (): JSX.Element => {