Files
shoplix/web-admin/src/CustomCodeManager.tsx
T
2026-11-30 14:33:13 +08:00

18 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from "react";
import { Code2, Pencil, Plus, Trash2 } from "lucide-react";
import { API } from "./api";
import { getCurrentSiteCode } from "./siteContext";
const SITE = getCurrentSiteCode();
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<CodeItem[]>([]); const [editing,setEditing]=useState<CodeItem|null>(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 <div className="product-editor"><div className="editor-top"><div className="breadcrumb-title"><button className="back" onClick={()=>setEditing(null)}>自定义代码</button><span>/</span><h1>{editing.code?"编辑代码":"新建代码"}</h1>{notice&&<em>{notice}</em>}</div><div className="editor-actions"><button onClick={()=>setEditing(null)}>取消</button><button className="primary" disabled={!editing.title||!editing.content} onClick={()=>void save()}><Code2 size={16}/>保存</button></div></div><div className="editor-layout"><div className="editor-main"><section className="edit-section"><h2>代码信息</h2><div className="edit-grid"><label><span>标题 *</span><input value={editing.title} onChange={e=>setEditing({...editing,title:e.target.value})}/></label><label><span>语言站点</span><input value={editing.locale} onChange={e=>setEditing({...editing,locale:e.target.value})}/></label><label className="wide"><span>代码内容 *</span><textarea rows={18} value={editing.content} onChange={e=>setEditing({...editing,content:e.target.value})}/></label></div></section></div><aside className="editor-side"><section className="edit-section"><h2>发布设置</h2><label className="decor-field"><span>终端</span><select value={editing.device} onChange={e=>setEditing({...editing,device:e.target.value})}><option value="ALL">全部终端</option><option value="DESKTOP">桌面端</option><option value="MOBILE">移动端</option></select></label><label className="decor-field"><span>注入位置</span><select value={editing.position} onChange={e=>setEditing({...editing,position:e.target.value})}><option value="HEAD">顶部 &lt;head&gt;</option><option value="BODY_START">正文顶部</option><option value="BODY_END">正文底部</option></select></label><label className="decor-field"><span>排序</span><input type="number" value={editing.sortNo} onChange={e=>setEditing({...editing,sortNo:Number(e.target.value)})}/></label><label className="check"><input type="checkbox" checked={editing.enabled} onChange={e=>setEditing({...editing,enabled:e.target.checked})}/>启用</label><p>Google Search Console 验证代码通常放在顶部 head;GTM、GA 脚本按官方要求选择位置。</p></section></aside></div></div>;
return <div className="content-page"><header><div><h1>自定义代码</h1><p>用于 Google Search Console、GTM、GA 和客服插件等代码注入</p></div><button className="primary" onClick={()=>setEditing({code:"",siteCode:SITE,title:"",content:"",locale:"ALL",device:"ALL",position:"HEAD",enabled:true,sortNo:30})}><Plus size={17}/>新建代码</button></header>{notice&&<div className="notice">{notice}</div>}<section className="table"><div className="thead"><span>标题</span><span>语言站</span><span>终端</span><span>位置</span><span>状态</span><span>操作</span></div>{items.map(item=><div className="row" key={item.code}><span><strong>{item.title}</strong><small>{item.code}</small></span><span>{item.locale}</span><span>{item.device}</span><span>{item.position}</span><span className={`status ${item.enabled?"published":"draft"}`}>{item.enabled?"已启用":"已停用"}</span><div className="row-actions"><button className="link" onClick={()=>setEditing(item)}><Pencil size={14}/>编辑</button><button className="link danger" onClick={()=>void remove(item.code)}><Trash2 size={14}/>删除</button></div></div>)}</section></div>;
}