import { NostrEvent, NostrLink } from "@snort/system"; import { useState } from "react"; import { FormatBytes } from "../const"; import classNames from "classnames"; import Profile from "../components/profile"; interface FileInfo { id: string; url: string; name?: string; type?: string; size?: number; uploader?: Array; } export default function FileList({ files, pages, page, onPage, onDelete, }: { files: Array; pages?: number; page?: number; onPage?: (n: number) => void; onDelete?: (id: string) => void; }) { const [viewType, setViewType] = useState<"grid" | "list">("grid"); if (files.length === 0) { return No Files; } function renderInner(f: FileInfo) { if ( f.type?.startsWith("image/") || f.type?.startsWith("video/") || !f.type ) { return ( ); } } function getInfo(f: File | NostrEvent | FileInfo): FileInfo { if ("url" in f) { return f; } if ("created_at" in f) { return { id: f.tags.find((a) => a[0] === "x")![1], url: f.tags.find((a) => a[0] === "url")![1], name: f.content, type: f.tags.find((a) => a[0] === "m")?.at(1), size: Number(f.tags.find((a) => a[0] === "size")?.at(1)), uploader: "uploader" in f ? (f.uploader as Array) : undefined, }; } else { return { id: f.name, url: URL.createObjectURL(f), name: f.name, type: f.type, size: f.size, }; } } function pageButtons(page: number, n: number) { const ret = []; const start = 0; for (let x = start; x < n; x++) { ret.push(
onPage?.(x)} className={classNames( "bg-neutral-700 hover:bg-neutral-600 min-w-8 text-center cursor-pointer font-bold", { "rounded-l-md": x === start, "rounded-r-md": x + 1 === n, "bg-neutral-400": page === x, }, )} > {x + 1}
, ); } return ret; } function showGrid() { return (
{files.map((a) => { const info = getInfo(a); return (
{(info.name?.length ?? 0) === 0 ? "Untitled" : info.name!.length > 20 ? `${info.name?.substring(0, 10)}...${info.name?.substring(info.name.length - 10)}` : info.name}
{info.size && !isNaN(info.size) ? FormatBytes(info.size, 2) : ""}
{info.type}
{info.uploader && info.uploader.map((a) => ( ))}
{renderInner(info)}
); })}
); } function showList() { return ( {files.some((i) => "uploader" in i) && ( )} {files.map((a) => { const info = getInfo(a); return ( {info.uploader && ( )} ); })}
Preview Name Type Size Uploader Actions
{renderInner(info)} {(info.name?.length ?? 0) === 0 ? "" : info.name} {info.type} {info.size && !isNaN(info.size) ? FormatBytes(info.size, 2) : ""} {info.uploader.map((a) => ( ))}
); } return ( <>
setViewType("grid")} className={`bg-neutral-700 hover:bg-neutral-600 min-w-20 text-center cursor-pointer font-bold rounded-l-md ${viewType === "grid" ? "bg-neutral-500" : ""}`} > Grid
setViewType("list")} className={`bg-neutral-700 hover:bg-neutral-600 min-w-20 text-center cursor-pointer font-bold rounded-r-md ${viewType === "list" ? "bg-neutral-500" : ""}`} > List
{viewType === "grid" ? showGrid() : showList()} {pages !== undefined && ( <>
{pageButtons(page ?? 0, pages)}
)} ); }