import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { getJSON, getJSONAuth, itemsOrEmpty, type AdminResource, type Category, } from "../../api"; import { getToken } from "../../admin/token"; import { resourceTypeDisplay } from "../../resourceTypeLabels"; import { useAdminT } from "../../admin/useAdminT"; import { adminUiPrefix } from "../../adminPaths"; import { useAdminRouterMode } from "../../adminRouterMode"; function statusLabel(t: (k: string) => string, s: string) { if (s === "published") return t("published"); if (s === "draft") return t("draft"); if (s === "archived") return t("archived"); return s; } const pageSize = 25; export function AdminResources() { const t = useAdminT(); const mode = useAdminRouterMode(); const token = getToken(); const [items, setItems] = useState([]); const [catNames, setCatNames] = useState>({}); const [page, setPage] = useState(1); const [total, setTotal] = useState(0); useEffect(() => { getJSON("/api/categories?lang=zh-CN") .then((cats) => { const m: Record = {}; for (const c of cats) m[c.id] = c.name; setCatNames(m); }) .catch(() => setCatNames({})); }, []); useEffect(() => { getJSONAuth<{ items: AdminResource[]; total?: number }>( `/api/admin/resources?limit=${pageSize}&page=${page}`, token, ) .then((r) => { setItems(itemsOrEmpty(r.items)); setTotal(typeof r.total === "number" ? r.total : 0); }) .catch(() => setItems([])); }, [token, page]); const pages = Math.max(1, Math.ceil(total / pageSize)); return (

{t("resources")}

{t("newResource")}

{t("listRange") .replace( "{{from}}", String(total === 0 ? 0 : (page - 1) * pageSize + 1), ) .replace("{{to}}", String(Math.min(page * pageSize, total))) .replace("{{total}}", String(total))}

{t("pageIndicator") .replace("{{c}}", String(page)) .replace("{{p}}", String(pages))}
{items.map((r) => ( ))}
{t("title")} {t("category")} {t("type")} {t("status")} {t("downloads")}
{r.title} {catNames[r.categoryId] ?? r.categoryId} {resourceTypeDisplay(t, r.type)} {statusLabel(t, r.status)} {r.downloadCount ?? 0} {t("edit")}
); }