import React, { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import { Boxes, Code2, ChevronDown, FileText, FolderTree, HelpCircle, Image, Mail, Newspaper, PanelsTopLeft, Paintbrush, Plus, Search, Settings, Users, X, } from "lucide-react"; import "./styles.css"; import ProductEditor from "./ProductEditor"; import ContentEditor from "./ContentEditor"; import CategoryManager from "./CategoryManager"; import InquirySettingsPanel from "./InquirySettingsPanel"; import PageEditor from "./PageEditor"; import DecorationEditor from "./DecorationEditor"; import CustomCodeManager from "./CustomCodeManager"; import MediaCenter from "./MediaCenter"; import LoginPage, { type Account } from "./LoginPage"; import SettingsPage from "./SettingsPage"; import { apiPost } from "./api"; const SITE_WEB = window.location.hostname === "admin.steliro.com" ? "https://site.steliro.com" : "http://localhost:5174"; const SITE = "SIT0000000000001"; type Section = "products" | "product-categories" | "NEWS" | "NEWS-categories" | "CASE" | "CASE-categories" | "HELP" | "HELP-categories" | "pages" | "decoration" | "custom-code" | "media" | "inquiries" | "inquiry-settings" | "settings"; type EditorialSection = "NEWS" | "CASE" | "HELP"; type ContentEditorState = { contentType: EditorialSection; contentCode: string }; type Item = { productCode?: string; contentCode?: string; pageCode?: string; productName?: string; title?: string; pageName?: string; slug: string; status: string; model?: string; publishTime?: string; inquiryCode?: string; customerName?: string; customerEmail?: string; companyName?: string; countryCode?: string; countryName?: string; provinceName?: string; cityName?: string; clientIp?: string; readStatus?: number; inquiryTitle?: string; sourceType?: string; statusLabel?: string; createTime?: string; }; async function post(path: string, body: unknown) { return apiPost(path, body); } const menus = [ { key: "pages" as Section, label: "页面", icon: FileText }, { key: "decoration" as Section, label: "店铺装修", icon: Paintbrush }, { key: "custom-code" as Section, label: "自定义代码", icon: Code2 }, { key: "media" as Section, label: "素材中心", icon: Image }, { key: "inquiries" as Section, label: "询盘", icon: Mail }, ]; const sectionLabels: Record = { products: "产品", "product-categories": "产品分类", NEWS: "新闻", "NEWS-categories": "新闻分类", CASE: "案例", "CASE-categories": "案例分类", HELP: "帮助", "HELP-categories": "帮助分类", pages: "页面", decoration: "店铺装修", "custom-code": "自定义代码", media: "素材中心", inquiries: "询盘", "inquiry-settings": "询盘设置", settings: "系统设置", }; const statusLabels: Record = { DRAFT: "草稿", PUBLISHED: "已上架", OFFLINE: "已下架" }; function App({ account, onAccount, onLogout }: { account: Account; onAccount: (value: Account) => void; onLogout: () => void }) { const contentRoutes: Record = { NEWS: "news", CASE: "cases", HELP: "help" }; const resolveContentRoute = (): ContentEditorState | null => { const match = window.location.pathname.match(/^\/(news|cases|help)\/(new|([^/]+)\/edit)$/); if (!match) return null; const contentType = ({ news: "NEWS", cases: "CASE", help: "HELP" } as const)[match[1] as "news" | "cases" | "help"]; return { contentType, contentCode: match[2] === "new" ? "" : decodeURIComponent(match[3]) }; }; const initialContentEditor = resolveContentRoute(); const [section, setSection] = useState
(initialContentEditor?.contentType || (window.location.pathname.startsWith("/pages/") ? "pages" : "products")); const [items, setItems] = useState([]); const [error, setError] = useState(""); const [query, setQuery] = useState(""); const [open, setOpen] = useState(false); const [saving, setSaving] = useState(false); const resolveProductRoute = () => { if (window.location.pathname === "/products/new") return ""; const match = window.location.pathname.match(/^\/products\/([^/]+)\/edit$/); return match ? decodeURIComponent(match[1]) : null; }; const resolvePageRoute = () => { if (window.location.pathname === "/pages/new") return ""; const match = window.location.pathname.match(/^\/pages\/([^/]+)\/edit$/); return match ? decodeURIComponent(match[1]) : null; }; const [editingProduct, setEditingProduct] = useState(resolveProductRoute); const [editingContent, setEditingContent] = useState(initialContentEditor); const [editingPage, setEditingPage] = useState(resolvePageRoute); const [productMenuOpen, setProductMenuOpen] = useState(true); const [contentMenuOpen, setContentMenuOpen] = useState(true); const navigateSection = (next: Section) => { setEditingProduct(null); setEditingContent(null); setEditingPage(null); setSection(next); window.history.pushState({}, "", "/"); }; const [form, setForm] = useState({ name: "", slug: "", summary: "", body: "", model: "", status: "DRAFT", }); const load = async () => { try { setError(""); const data = section.endsWith("-categories") ? { items: [] } : section === "settings" ? { items: [] } : section === "products" ? await post("/products/list", { siteCode: SITE }) : section === "pages" ? await post("/pages/list", { siteCode: SITE }) : section === "decoration" ? { items: [] } : section === "inquiries" ? await post("/inquiries/list", { siteCode: SITE }) : section === "inquiry-settings" ? { items: [] } : await post("/contents/list", { siteCode: SITE, contentType: section, }); setItems(data.items || []); } catch (e) { setError(e instanceof Error ? e.message : "加载失败"); setItems([]); } }; useEffect(() => { void load(); }, [section]); useEffect(() => { const handlePopState = () => { setEditingProduct(resolveProductRoute()); setEditingPage(resolvePageRoute()); const content = resolveContentRoute(); setEditingContent(content); if (content) setSection(content.contentType); }; window.addEventListener("popstate", handlePopState); return () => window.removeEventListener("popstate", handlePopState); }, []); const openProductEditor = (productCode?: string) => { const path = productCode ? `/products/${encodeURIComponent(productCode)}/edit` : "/products/new"; window.history.pushState({}, "", path); setEditingProduct(productCode || ""); }; const closeProductEditor = () => { window.history.pushState({}, "", "/"); setEditingProduct(null); void load(); }; const openContentEditor = (contentType: EditorialSection, contentCode?: string) => { const route = contentRoutes[contentType]; const path = contentCode ? `/${route}/${encodeURIComponent(contentCode)}/edit` : `/${route}/new`; window.history.pushState({}, "", path); setEditingContent({ contentType, contentCode: contentCode || "" }); }; const closeContentEditor = () => { window.history.pushState({}, "", "/"); setEditingContent(null); }; const openPageEditor = (pageCode?: string) => { window.history.pushState({}, "", pageCode ? `/pages/${encodeURIComponent(pageCode)}/edit` : "/pages/new"); setEditingPage(pageCode || ""); }; const closePageEditor = () => { window.history.pushState({}, "", "/"); setEditingPage(null); }; const title = sectionLabels[section]; const filtered = items.filter((x) => JSON.stringify(x).toLowerCase().includes(query.toLowerCase()), ); const changeItemStatus = async (item: Item) => { const status = item.status === "PUBLISHED" ? "OFFLINE" : "PUBLISHED"; try { setError(""); if (section === "products" && item.productCode) await post("/products/status", { siteCode: SITE, productCode: item.productCode, status }); else if (["NEWS", "CASE", "HELP"].includes(section) && item.contentCode) await post("/contents/status", { siteCode: SITE, contentCode: item.contentCode, contentType: section, status }); await load(); } catch (operationError) { setError(operationError instanceof Error ? operationError.message : "状态更新失败"); } }; const deleteItem = async (item: Item) => { if (!window.confirm("删除后该内容将不再展示,确认删除吗?")) return; try { setError(""); if (section === "products" && item.productCode) await post("/products/delete", { siteCode: SITE, productCode: item.productCode }); else if (["NEWS", "CASE", "HELP"].includes(section) && item.contentCode) await post("/contents/delete", { siteCode: SITE, contentCode: item.contentCode, contentType: section }); await load(); } catch (operationError) { setError(operationError instanceof Error ? operationError.message : "删除失败"); } }; const save = async () => { try { setSaving(true); setError(""); if (section === "products") await post("/products/save", { siteCode: SITE, productName: form.name, slug: form.slug, model: form.model, summary: form.summary, productContent: form.body, featured: false, inquiryEnabled: true, sortNo: 0, status: form.status, specifications: [], }); else if (section === "pages") await post("/pages/save", { siteCode: SITE, pageName: form.name, slug: form.slug, summary: form.summary, pageContent: form.body, systemPage: false, sortNo: 0, status: form.status, }); else await post("/contents/save", { siteCode: SITE, contentType: section, title: form.name, slug: form.slug, summary: form.summary, contentBody: form.body, featured: false, sortNo: 0, status: form.status, }); setOpen(false); setForm({ name: "", slug: "", summary: "", body: "", model: "", status: "DRAFT", }); await load(); } catch (e) { setError(e instanceof Error ? e.message : "保存失败"); } finally { setSaving(false); } }; return (
{section === "settings" ? : section === "decoration" ? navigateSection("pages")}/> : section === "custom-code" ? : section === "media" ? : editingProduct !== null ? : editingContent !== null ? : editingPage !== null ? : <> {section.endsWith("-categories") ? : <>

{title}管理

Local Development Site · 英文站内容

{!["inquiries", "inquiry-settings"].includes(section) &&
{["NEWS", "CASE", "HELP"].includes(section) && }
}
{section === "inquiry-settings" ? : <>
setQuery(e.target.value)} placeholder={`搜索${title}名称或路径`} />
{error && (
{error}。请先启动 Admin 后端,并初始化本地站点数据。
)}
{section === "inquiries" ? <>公司名称电子邮箱国家 / IP地址询盘时间询盘来源状态操作 : <>名称英文路径状态发布时间操作}
{filtered.length === 0 && !error ? (
暂无{title}数据

点击右上角新建按钮录入第一条内容。

) : ( filtered.map((item, index) => (
{section === "inquiries" ? <>{item.companyName || item.customerName || "--"}{item.customerName || "--"}{item.customerEmail || "--"}{item.countryName || item.countryCode || "未知"}({item.clientIp || "--"}){item.createTime?.replace("T", " ").slice(0, 16) || "--"}{item.productName || item.inquiryTitle || item.sourceType || "网站询盘"}{item.readStatus === 0 ? "未读" : item.statusLabel || item.status}
: <>{item.productName || item.title || item.pageName}{item.model || item.productCode || item.contentCode || item.pageCode}/{item.slug}{statusLabels[item.status] || item.status}{item.publishTime?.replace("T", " ").slice(0, 16) || "--"}
{["products", "NEWS", "CASE", "HELP"].includes(section) && item.slug && 查看前台}{["products", "NEWS", "CASE", "HELP"].includes(section) && <>}
}
)) )}
} } }
{open && (

新建{title}

前台内容统一使用英文录入

{section === "products" && ( )}