"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useContext, useEffect, useMemo, useState } from "react"; import toast from "react-hot-toast"; import { z } from "zod"; import { Schema_File } from "~/schema"; import { cn } from "~/utils"; import Icon from "~/components/Icon"; import { Button } from "~/components/ui/button"; import { Separator } from "~/components/ui/separator"; import { LayoutContext } from "~/context/layoutContext"; import config from "~/config/gIndex.config"; import FileGrid from "./@file.grid"; import FileList from "./@file.list"; import { GetFiles } from "./actions"; type Props = { files: z.infer[]; nextPageToken?: string; root?: boolean; }; export default function FileBrowser({ files, nextPageToken, root }: Props) { const { layout } = useContext(LayoutContext); const pathname = usePathname(); const prevPath = useMemo(() => { const path = pathname .split("/") .slice(0, -1) .join("/") .replace(/\/+/g, "/"); return new URL(path, config.basePath).pathname; }, [pathname]); const [fileList, setFileList] = useState[]>(files); const [nextToken, setNextToken] = useState(nextPageToken); const [loadMoreLoading, setLoadMoreLoading] = useState(false); const [loading, setLoading] = useState(true); useEffect(() => { setLoading(false); }, []); const onLoadMore = async () => { setLoadMoreLoading(true); try { if (!nextToken) throw new Error("No more files to load"); const data = await GetFiles({ pageToken: nextToken }); const uniqueData = [...fileList, ...data.files].filter( (item, index, array) => index === array.findIndex((i) => i.encryptedId === item.encryptedId), ); setFileList(uniqueData); // const uniqueData = new Set([...fileList, ...data.files]); // setFileList([...uniqueData]); setNextToken(data.nextPageToken); } catch (error) { const e = error as Error; console.error(e.message); toast.error(e.message); } finally { setLoadMoreLoading(false); } }; if (loading) { return (

Wait a moment while we load your files...

); } return (
{!root && ( )} {!fileList.length && (
There are no files in this folder
)} {layout === "list" && (
{fileList.map((file) => (
))}
)} {layout === "grid" && (
{fileList.map((file) => (
))}
)} {nextToken && ( )}
); }