fix: add media center empty state
This commit is contained in:
1 parent
01771926bd
commit
4d1aba4888
2 files changed
+111
-5
No files matched your search
@@ -1,6 +1,112 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Image, Search } from "lucide-react";
|
||||
const API="http://localhost:8081/api/admin/v1";const SITE="SIT0000000000001";
|
||||
type Media={mediaCode:string;mediaType:string;originalFileName:string;url:string;altText?:string};
|
||||
import { ImageOff, Search } from "lucide-react";
|
||||
|
||||
const API = "http://localhost:8081/api/admin/v1";
|
||||
const SITE = "SIT0000000000001";
|
||||
|
||||
type Media = {
|
||||
mediaCode: string;
|
||||
mediaType: string;
|
||||
originalFileName: string;
|
||||
url: string;
|
||||
altText?: string;
|
||||
};
|
||||
|
||||
/** 素材中心页面。 */
|
||||
export default function MediaCenter(){const [items,setItems]=useState<Media[]>([]);const [type,setType]=useState("ALL");const [query,setQuery]=useState("");useEffect(()=>{fetch(API+"/media/list",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({siteCode:SITE,mediaType:type})}).then(r=>r.json()).then(d=>setItems(d.items||[])).catch(()=>setItems([]))},[type]);const filtered=items.filter(x=>x.originalFileName.toLowerCase().includes(query.toLowerCase()));return <div className="content-page"><header><div><h1>素材中心</h1><p>集中管理上传过的图片和视频,编辑内容时可直接复用</p></div></header><section className="toolbar"><div className="search"><Search size={17}/><input placeholder="搜索文件名" value={query} onChange={e=>setQuery(e.target.value)}/></div><select value={type} onChange={e=>setType(e.target.value)}><option value="ALL">全部素材</option><option value="IMAGE">图片</option><option value="VIDEO">视频</option></select></section><section className="media-grid">{filtered.map(item=><article className="media-card" key={item.mediaCode}>{item.mediaType==="VIDEO"?<video src={item.url} controls/>:<img src={`http://localhost:8081${item.url}`} alt={item.altText||item.originalFileName}/>}<strong title={item.originalFileName}>{item.originalFileName}</strong><small>{item.mediaCode}</small><button className="link" onClick={()=>navigator.clipboard.writeText(item.url)}>复制地址</button></article>)}</section></div>}
|
||||
export default function MediaCenter() {
|
||||
const [items, setItems] = useState<Media[]>([]);
|
||||
const [type, setType] = useState("ALL");
|
||||
const [query, setQuery] = useState("");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
fetch(`${API}/media/list`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ siteCode: SITE, mediaType: type }),
|
||||
})
|
||||
.then(async (response) => {
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => ({}));
|
||||
throw new Error(data.message || "素材加载失败");
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then((data) => setItems(data.items || []))
|
||||
.catch((reason) => {
|
||||
setItems([]);
|
||||
setError(reason instanceof Error ? reason.message : "素材加载失败");
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
}, [type]);
|
||||
|
||||
const filtered = items.filter((item) =>
|
||||
item.originalFileName.toLowerCase().includes(query.trim().toLowerCase()),
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="content-page media-center">
|
||||
<header>
|
||||
<div>
|
||||
<h1>素材中心</h1>
|
||||
<p>集中管理上传过的图片和视频,编辑内容时可直接复用</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section className="toolbar media-toolbar">
|
||||
<div className="search">
|
||||
<Search size={17} />
|
||||
<input
|
||||
placeholder="搜索文件名"
|
||||
value={query}
|
||||
onChange={(event) => setQuery(event.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<select value={type} onChange={(event) => setType(event.target.value)}>
|
||||
<option value="ALL">全部素材</option>
|
||||
<option value="IMAGE">图片</option>
|
||||
<option value="VIDEO">视频</option>
|
||||
</select>
|
||||
</section>
|
||||
|
||||
{error && <div className="notice">{error}。请检查 Admin 服务和数据库连接。</div>}
|
||||
|
||||
<section className="media-panel">
|
||||
{loading ? (
|
||||
<div className="media-empty">
|
||||
<strong>正在加载素材</strong>
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<div className="media-empty">
|
||||
<ImageOff size={46} strokeWidth={1.5} />
|
||||
<strong>{query ? "未找到匹配素材" : "暂无素材"}</strong>
|
||||
<p>{query ? "请尝试更换搜索关键词" : "上传图片或视频后,素材会统一展示在这里"}</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="media-grid">
|
||||
{filtered.map((item) => (
|
||||
<article className="media-card" key={item.mediaCode}>
|
||||
{item.mediaType === "VIDEO" ? (
|
||||
<video src={item.url} controls />
|
||||
) : (
|
||||
<img
|
||||
src={`http://localhost:8081${item.url}`}
|
||||
alt={item.altText || item.originalFileName}
|
||||
/>
|
||||
)}
|
||||
<strong title={item.originalFileName}>{item.originalFileName}</strong>
|
||||
<small>{item.mediaCode}</small>
|
||||
<button className="link" onClick={() => navigator.clipboard.writeText(item.url)}>
|
||||
复制地址
|
||||
</button>
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user