import { useEffect, useState } from "react"; import type { CSSProperties } from "react"; import { api } from "../api"; import type { NavigateFn } from "./route-config"; import type { Product } from "../types"; import "../trash-page.css"; const coverOf = (p: Product): string => p.cover_preview_url || p.images?.find((i) => i.preview_url)?.preview_url || ""; const mediaStyle = (url: string): CSSProperties => ({ ["--mock-media-url"]: `url(${url})` } as CSSProperties); // 垃圾桶:已软删(status=archived)的商品 · 可恢复 / 可彻底删除(不可恢复) export function TrashPage({ onRestore, onPurge }: { navigate: NavigateFn; onRestore: (id: string) => Promise | void; onPurge: (id: string) => Promise | void; }) { const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [confirmId, setConfirmId] = useState(null); const [busyId, setBusyId] = useState(null); useEffect(() => { let alive = true; api.productsTrash().then((r) => { if (alive) setItems(r.results); }).catch(() => { if (alive) setItems([]); }).finally(() => { if (alive) setLoading(false); }); return () => { alive = false; }; }, []); async function restore(id: string) { setBusyId(id); try { await onRestore(id); setItems((list) => list.filter((p) => p.id !== id)); } finally { setBusyId(null); } } async function purge(id: string) { setBusyId(id); try { await onPurge(id); setItems((list) => list.filter((p) => p.id !== id)); setConfirmId(null); } finally { setBusyId(null); } } return (

垃圾桶

// 已删除商品 · 可恢复,或彻底删除(不可恢复)
{loading ? (
// 加载中…
) : items.length === 0 ? (
// 垃圾桶是空的
) : (
{items.map((p) => { const cover = coverOf(p); const busy = busyId === p.id; return (
{!cover && 无图}
{p.title}
// {p.category || "未分类"}
{confirmId === p.id ? ( <> 彻底删除?不可恢复 ) : ( <> )}
); })}
)}
); }