import { useEffect, useState } from "react"; import { Code2, Pencil, Plus, Trash2 } from "lucide-react"; import { API } from "./api"; const SITE = "SIT0000000000001"; type CodeItem = { code:string; siteCode:string; title:string; content:string; locale:string; device:string; position:string; enabled:boolean; sortNo:number }; async function post(path:string, body:unknown) { const r=await fetch(API+path,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(body)}); if(!r.ok) throw new Error("请求失败"); return r.json(); } /** 自定义代码管理页面。 */ export default function CustomCodeManager() { const [items,setItems]=useState([]); const [editing,setEditing]=useState(null); const [notice,setNotice]=useState(""); const load=()=>post("/custom-codes/list",{siteCode:SITE}).then(d=>setItems(d.items||[])).catch(e=>setNotice(e.message)); useEffect(()=>{void load()},[]); const save=async()=>{if(!editing)return;try{await post("/custom-codes/save",{...editing,enabled:editing.enabled});setEditing(null);setNotice("已保存");void load()}catch(e){setNotice(e instanceof Error?e.message:"保存失败")}}; const remove=async(code:string)=>{if(!confirm("确定删除这段代码吗?"))return;await post(`/custom-codes/delete?siteCode=${SITE}&code=${code}`,{});void load()}; if (editing) return
/

{editing.code?"编辑代码":"新建代码"}

{notice&&{notice}}

代码信息