"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useMemo, useState } from "react"; import { toast } from "sonner"; import { type z } from "zod"; import { FileItem } from "~/components/explorer"; import { Status } from "~/components/global"; import { PageLoader } from "~/components/layout"; import { Button, LoadingButton } from "~/components/ui/button"; import Icon from "~/components/ui/icon"; import { useLayout } from "~/context/layoutContext"; import useLoading from "~/hooks/useLoading"; import { cn } from "~/lib/utils"; import { type Schema_File } from "~/types/schema"; import { ListFiles } from "~/actions/files"; import config from "config"; type Props = { encryptedId: string; files: z.infer[]; nextPageToken?: string; showBackButton?: boolean; }; export default function FileExplorerLayout({ encryptedId, files, nextPageToken, showBackButton }: Props) { const { layout, isPending } = useLayout(); const loading = useLoading(); 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 [filesList, setFilesList] = useState[]>(files); const [nextToken, setNextToken] = useState(nextPageToken); const [isLoadingMore, setLoadingMore] = useState(false); const onLoadMore = async () => { setLoadingMore(true); try { if (!nextToken) throw new Error("No more files to load"); const data = await ListFiles({ id: encryptedId, pageToken: nextToken }); if (!data.success) throw new Error(data.error); const uniqueData = [...filesList, ...data.data.files].filter( (item, index, self) => index === self.findIndex((i) => i.encryptedId === item.encryptedId), ); setFilesList(uniqueData); setNextToken(data.data.nextPageToken ?? undefined); } catch (error) { const e = error as Error; console.error(`[OnLoadMore] ${e.message}`); toast.error(e.message); } finally { setLoadingMore(false); } }; if (loading || isPending) return ; return (
{showBackButton && ( )} {!filesList.length ? ( ) : (
{filesList.map((file) => (
))}
)} {nextToken && ( Load more files )}
); }