2023-05-22 20:54:07 +03:00
|
|
|
"use client";
|
2023-06-02 18:01:49 +03:00
|
|
|
import { useAxios } from "@/lib/useAxios";
|
2023-05-27 17:42:48 +03:00
|
|
|
import { AnimatePresence, motion } from "framer-motion";
|
2023-05-22 21:24:18 +03:00
|
|
|
import Link from "next/link";
|
2023-05-26 14:56:29 +03:00
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import Button from "../components/ui/Button";
|
2023-05-23 08:34:53 +03:00
|
|
|
import Spinner from "../components/ui/Spinner";
|
2023-05-24 23:21:22 +03:00
|
|
|
import { useSupabase } from "../supabase-provider";
|
2023-05-26 14:56:29 +03:00
|
|
|
import DocumentItem from "./DocumentItem";
|
|
|
|
import { Document } from "./types";
|
2023-05-24 23:21:22 +03:00
|
|
|
|
2023-05-21 02:20:55 +03:00
|
|
|
export default function ExplorePage() {
|
2023-05-22 20:54:07 +03:00
|
|
|
const [documents, setDocuments] = useState<Document[]>([]);
|
2023-05-23 08:34:53 +03:00
|
|
|
const [isPending, setIsPending] = useState(true);
|
2023-05-26 14:56:29 +03:00
|
|
|
const { session } = useSupabase();
|
2023-06-02 18:01:49 +03:00
|
|
|
const { axiosInstance } = useAxios();
|
|
|
|
|
2023-05-24 23:21:22 +03:00
|
|
|
if (session === null) {
|
2023-05-26 11:57:29 +03:00
|
|
|
redirect("/login");
|
2023-05-24 23:21:22 +03:00
|
|
|
}
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-05-26 11:57:29 +03:00
|
|
|
useEffect(() => {
|
2023-05-26 12:27:29 +03:00
|
|
|
const fetchDocuments = async () => {
|
|
|
|
setIsPending(true);
|
|
|
|
try {
|
|
|
|
console.log(
|
|
|
|
`Fetching documents from ${process.env.NEXT_PUBLIC_BACKEND_URL}/explore`
|
|
|
|
);
|
2023-06-02 18:01:49 +03:00
|
|
|
const response = await axiosInstance.get<{ documents: Document[] }>(
|
|
|
|
"/explore"
|
2023-05-26 12:27:29 +03:00
|
|
|
);
|
|
|
|
setDocuments(response.data.documents);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching documents", error);
|
|
|
|
setDocuments([]);
|
|
|
|
}
|
|
|
|
setIsPending(false);
|
|
|
|
};
|
2023-05-26 11:57:29 +03:00
|
|
|
fetchDocuments();
|
2023-06-09 19:49:47 +03:00
|
|
|
}, [session.access_token, axiosInstance]);
|
2023-05-26 11:57:29 +03:00
|
|
|
|
2023-05-22 20:54:07 +03:00
|
|
|
return (
|
2023-05-23 11:06:58 +03:00
|
|
|
<main>
|
2023-05-26 11:57:29 +03:00
|
|
|
<section className="w-full outline-none pt-32 flex flex-col gap-5 items-center justify-center p-6">
|
2023-05-27 17:42:48 +03:00
|
|
|
<div className="flex flex-col items-center justify-center">
|
2023-05-26 11:57:29 +03:00
|
|
|
<h1 className="text-3xl font-bold text-center">
|
|
|
|
Explore uploaded data
|
|
|
|
</h1>
|
|
|
|
<h2 className="opacity-50">
|
|
|
|
View or delete stored data used by your brain
|
|
|
|
</h2>
|
2023-05-23 08:34:53 +03:00
|
|
|
</div>
|
2023-05-23 11:06:58 +03:00
|
|
|
{isPending ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
2023-05-27 17:42:48 +03:00
|
|
|
<motion.div layout className="w-full max-w-xl flex flex-col gap-5">
|
2023-05-23 11:06:58 +03:00
|
|
|
{documents.length !== 0 ? (
|
2023-05-27 17:42:48 +03:00
|
|
|
<AnimatePresence mode="popLayout">
|
2023-05-23 11:06:58 +03:00
|
|
|
{documents.map((document) => (
|
|
|
|
<DocumentItem
|
|
|
|
key={document.name}
|
|
|
|
document={document}
|
|
|
|
setDocuments={setDocuments}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</AnimatePresence>
|
|
|
|
) : (
|
|
|
|
<div className="flex flex-col items-center justify-center mt-10 gap-1">
|
|
|
|
<p className="text-center">Oh No, Your Brain is empty.</p>
|
|
|
|
<Link href="/upload">
|
2023-05-25 11:10:35 +03:00
|
|
|
<Button>Upload</Button>
|
2023-05-23 11:06:58 +03:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
)}
|
2023-05-27 17:42:48 +03:00
|
|
|
</motion.div>
|
2023-05-23 11:06:58 +03:00
|
|
|
)}
|
|
|
|
</section>
|
|
|
|
</main>
|
2023-05-22 20:54:07 +03:00
|
|
|
);
|
2023-05-21 02:20:55 +03:00
|
|
|
}
|