"use client"; import { Images, Loader2 } from "lucide-react"; import { useEffect, useState } from "react"; import Modal from "@/components/Modal"; import { apiMyMedia, type MediaAttachment } from "@/lib/api"; import { mediaURLFitsBackground, type BgSurface } from "@/lib/bg"; const SURFACE_LABEL: Record = { site: "前台背景", admin: "后台背景", }; const BG_MAX_BYTES = 8 * 1024 * 1024; const BG_MAX_DIM = 8192; function mediaFitsBackground(item: MediaAttachment): boolean { if (!mediaURLFitsBackground(item.url)) return false; if (item.size > 0 && item.size > BG_MAX_BYTES) return false; if (item.width > 0 && item.width > BG_MAX_DIM) return false; if (item.height > 0 && item.height > BG_MAX_DIM) return false; return true; } export default function BgMediaPicker({ open, surface, onClose, onPick, }: { open: boolean; surface: BgSurface; onClose: () => void; onPick: (item: MediaAttachment) => void; }) { const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); useEffect(() => { if (!open) return; let cancelled = false; setLoading(true); setError(""); void apiMyMedia() .then((res) => { if (cancelled) return; if (res.error) { setError(res.error); setItems([]); return; } const all = res.attachments || []; setItems(all.filter(mediaFitsBackground)); }) .catch(() => { if (!cancelled) setError("加载媒体库失败"); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [open, surface]); return ( {loading ? (

加载中

) : error ? (

{error}

) : items.length === 0 ? (

没有可用图片。请先在发帖或媒体库上传 JPEG / PNG / WebP。

) : (
{items.map((item) => ( ))}
)}
); }