2023-05-22 20:54:07 +03:00
|
|
|
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
import axios from "axios";
|
|
|
|
import DocumentItem from "./DocumentItem";
|
|
|
|
import { Document } from "./types";
|
2023-05-22 21:24:18 +03:00
|
|
|
import Button from "../components/ui/Button";
|
|
|
|
import Link from "next/link";
|
2023-05-23 08:34:53 +03:00
|
|
|
import Spinner from "../components/ui/Spinner";
|
2023-05-23 08:46:36 +03:00
|
|
|
import { AnimatePresence } from "framer-motion";
|
2023-05-24 23:21:22 +03:00
|
|
|
import { useSupabase } from "../supabase-provider";
|
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
|
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-24 23:21:22 +03:00
|
|
|
const { supabase, session } = useSupabase();
|
|
|
|
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-22 20:54:07 +03:00
|
|
|
const fetchDocuments = async () => {
|
2023-05-23 08:34:53 +03:00
|
|
|
setIsPending(true);
|
2023-05-22 20:54:07 +03:00
|
|
|
try {
|
|
|
|
console.log(
|
|
|
|
`Fetching documents from ${process.env.NEXT_PUBLIC_BACKEND_URL}/explore`
|
|
|
|
);
|
|
|
|
const response = await axios.get<{ documents: Document[] }>(
|
2023-05-24 23:21:22 +03:00
|
|
|
`${process.env.NEXT_PUBLIC_BACKEND_URL}/explore`,
|
|
|
|
{
|
|
|
|
headers: {
|
2023-05-26 11:57:29 +03:00
|
|
|
Authorization: `Bearer ${session.access_token}`,
|
2023-05-24 23:21:22 +03:00
|
|
|
},
|
|
|
|
}
|
2023-05-22 20:54:07 +03:00
|
|
|
);
|
|
|
|
setDocuments(response.data.documents);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching documents", error);
|
|
|
|
setDocuments([]);
|
|
|
|
}
|
2023-05-23 08:34:53 +03:00
|
|
|
setIsPending(false);
|
2023-05-22 20:54:07 +03:00
|
|
|
};
|
2023-05-21 02:20:55 +03:00
|
|
|
|
2023-05-26 11:57:29 +03:00
|
|
|
useEffect(() => {
|
|
|
|
fetchDocuments();
|
|
|
|
}, [fetchDocuments]);
|
|
|
|
|
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-23 11:06:58 +03:00
|
|
|
<div className="flex flex-col items-center justify-center my-10">
|
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 />
|
|
|
|
) : (
|
|
|
|
<div className="w-full max-w-xl flex flex-col gap-5">
|
|
|
|
{documents.length !== 0 ? (
|
|
|
|
<AnimatePresence>
|
|
|
|
{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>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</section>
|
|
|
|
</main>
|
2023-05-22 20:54:07 +03:00
|
|
|
);
|
2023-05-21 02:20:55 +03:00
|
|
|
}
|