2026-11-23 14:45:29 +08:00
import React , { useEffect , useMemo , useState } from "react" ;
import { Bold , Heading2 , Italic , Link as LinkIcon , List , Redo2 , Save , Undo2 } from "lucide-react" ;
import { EditorContent , useEditor } from "@tiptap/react" ;
import StarterKit from "@tiptap/starter-kit" ;
import Link from "@tiptap/extension-link" ;
import { apiPost } from "./api" ;
const SITE = "SIT0000000000001" ;
type AgreementKey = "COOKIE_POLICY" | "PRIVACY_POLICY" | "TERMS_OF_SERVICE" ;
2026-11-24 16:25:25 +08:00
type AgreementTab = { key : AgreementKey ; label : string ; pageName : string ; slug : string ; seoDescription :string ; robots :string };
type AgreementSeo = { title :string ; description :string ; canonicalUrl :string ; robots :string ; h1 :string };
2026-11-23 14:45:29 +08:00
const tabs : AgreementTab [] = [
2026-11-24 16:25:25 +08:00
{ key : "COOKIE_POLICY" , label : "Cookies协议" , pageName : "Cookies Policy" , slug : "cookie-policy" , seoDescription : "Learn how this website uses cookies to improve browsing and website functionality." , robots : "noindex,follow" },
{ key : "PRIVACY_POLICY" , label : "隐私协议" , pageName : "Privacy Policy" , slug : "privacy-policy" , seoDescription : "Learn how Shoplix collects, uses and protects personal information." , robots : "index,follow" },
{ key : "TERMS_OF_SERVICE" , label : "服务协议" , pageName : "Terms of Use" , slug : "terms-of-service" , seoDescription : "Review the terms and conditions that govern the use of the Shoplix website and services." , robots : "index,follow" },
2026-11-23 14:45:29 +08:00
];
2026-11-24 16:25:25 +08:00
/** 创建协议页的基础 SEO 默认值。 */
function defaultSeo ( tab :AgreementTab ) : AgreementSeo { return { title : ` ${ tab . pageName } | Shoplix` , description :tab.seoDescription , canonicalUrl : `/ ${ tab . slug } ` , robots :tab.robots , h1 :tab.pageName }}
2026-11-23 14:45:29 +08:00
/** 固定协议页面配置,支持 Cookies、隐私和服务协议独立维护。 */
export default function AgreementSettingsPage() {
const [ activeKey , setActiveKey ] = useState < AgreementKey >( "COOKIE_POLICY" );
const [ pageCodes , setPageCodes ] = useState < Partial < Record < AgreementKey , string >>>({});
const [ title , setTitle ] = useState ( "" );
const [ status , setStatus ] = useState ( "DRAFT" );
const [ notice , setNotice ] = useState ( "" );
const [ saving , setSaving ] = useState ( false );
2026-11-24 16:25:25 +08:00
const [ seo , setSeo ] = useState < AgreementSeo >(() => defaultSeo ( tabs [ 0 ]));
2026-11-23 14:45:29 +08:00
const activeTab = useMemo (() => tabs . find (( tab ) => tab . key === activeKey ) || tabs [ 0 ], [ activeKey ]);
const editor = useEditor ({ extensions : [ StarterKit , Link . configure ({ openOnClick : false })], content : "<p></p>" });
useEffect (() => {
void loadPages ();
}, []);
useEffect (() => {
const code = pageCodes [ activeKey ];
2026-11-24 16:25:25 +08:00
setSeo ( defaultSeo ( activeTab ));
2026-11-23 14:45:29 +08:00
if ( ! code ) {
setTitle ( activeTab . pageName );
setStatus ( "DRAFT" );
editor ? . commands . setContent ( "<p></p>" );
return ;
}
void apiPost ( "/pages/detail" , { siteCode : SITE , pageCode : code }). then (( data ) => {
setTitle ( data . pageName || activeTab . pageName );
setStatus ( data . status || "DRAFT" );
editor ? . commands . setContent ( data . pageContent || "<p></p>" );
2026-11-24 16:25:25 +08:00
return apiPost ( "/seo/detail" ,{ siteCode :SITE , pageType : "SYSTEM_PAGE" , targetCode :code }). catch (() => undefined );
}). then (( seoData ) => {
if ( ! seoData ) return ;
setSeo ({ title :seoData.title || defaultSeo ( activeTab ). title , description :seoData.description || defaultSeo ( activeTab ). description , canonicalUrl :seoData.canonicalUrl || `/ ${ activeTab . slug } ` , robots :seoData.robots || activeTab . robots , h1 :seoData.h1 || activeTab . pageName });
2026-11-23 14:45:29 +08:00
}). catch (( error ) => setNotice ( error instanceof Error ? error . message : "加载失败" ));
}, [ activeKey , pageCodes , editor , activeTab ]);
async function loadPages() {
try {
const data = await apiPost ( "/pages/list" , { siteCode : SITE });
const codes : Partial < Record < AgreementKey , string >> = {};
( data . items || []). forEach (( item : { pageCode : string ; systemPageType : AgreementKey }) => {
if ( tabs . some (( tab ) => tab . key === item . systemPageType )) codes [ item . systemPageType ] = item . pageCode ;
});
setPageCodes ( codes );
} catch ( error ) {
setNotice ( error instanceof Error ? error . message : "加载失败" );
}
}
async function save() {
if ( ! editor || ! title . trim ()) return ;
try {
setSaving ( true );
setNotice ( "" );
const result = await apiPost ( "/pages/save" , {
pageCode : pageCodes [ activeKey ], siteCode : SITE , pageType : "SYSTEM_PAGE" , systemPageType : activeKey ,
pageName : title.trim (), slug : activeTab.slug , pageContent : editor.getHTML (), summary : "" , coverMediaCode : null ,
sortNo : 30 , status ,
});
setPageCodes (( current ) => ({ ... current , [ activeKey ] : result . pageCode }));
2026-11-24 16:25:25 +08:00
await apiPost ( "/seo/save" ,{ siteCode :SITE , pageType : "SYSTEM_PAGE" , targetCode :result.pageCode , title :seo.title.trim () || title . trim (), description :seo.description.trim (), keywords : "" , h1 :seo.h1.trim () || title . trim (), canonicalUrl :seo.canonicalUrl.trim () || `/ ${ activeTab . slug } ` , robots :seo.robots , ogTitle : "" , ogDescription : "" , ogImageUrl : "" , structuredData :null , status , headings : []});
setNotice ( "协议与 SEO 已保存" );
2026-11-23 14:45:29 +08:00
} catch ( error ) {
setNotice ( error instanceof Error ? error . message : "保存失败" );
} finally {
setSaving ( false );
}
}
const addLink = () => { const href = window . prompt ( "请输入链接地址" ); if ( href ) editor ? . chain (). focus (). setLink ({ href }). run (); };
return < div className = "agreement-settings" >
< div className = "agreement-top" >< div >< h1 > 协议设置 </ h1 >< p > 维护网站 Cookies 、隐私和服务协议内容。 </ p ></ div >< button className = "primary" disabled = { saving || ! title . trim ()} onClick = {() => void save ()}>< Save size = { 16 }/>{ saving ? "保存中" : "保存" }</ button ></ div >
< div className = "agreement-tabs" >{ tabs . map (( tab ) => < button key = { tab . key } className = { tab . key === activeKey ? "active" : "" } onClick = {() => setActiveKey ( tab . key )}>{ tab . label }</ button >)}</ div >
< section className = "agreement-card" >< div className = "agreement-card-head" >< h2 >{ activeTab . label }</ h2 >< select value = { status } onChange = {( event ) => setStatus ( event . target . value )}>< option value = "DRAFT" > 草稿 </ option >< option value = "PUBLISHED" > 已上架 </ option >< option value = "OFFLINE" > 已下架 </ option ></ select ></ div >
< label className = "agreement-title" >< span > 页面标题 </ span >< input value = { title } onChange = {( event ) => setTitle ( event . target . value )} maxLength = { 100 }/></ label >
< label className = "agreement-content" >< span > 页面详情 </ span >< div className = "rich-editor" >< div className = "rich-toolbar" >< Tool title = "撤销" onClick = {() => editor ? . chain (). focus (). undo (). run ()}>< Undo2 /></ Tool >< Tool title = "重做" onClick = {() => editor ? . chain (). focus (). redo (). run ()}>< Redo2 /></ Tool >< Tool title = "粗体" onClick = {() => editor ? . chain (). focus (). toggleBold (). run ()}>< Bold /></ Tool >< Tool title = "斜体" onClick = {() => editor ? . chain (). focus (). toggleItalic (). run ()}>< Italic /></ Tool >< Tool title = "二级标题" onClick = {() => editor ? . chain (). focus (). toggleHeading ({ level : 2 }). run ()}>< Heading2 /></ Tool >< Tool title = "项目列表" onClick = {() => editor ? . chain (). focus (). toggleBulletList (). run ()}>< List /></ Tool >< Tool title = "链接" onClick = { addLink }>< LinkIcon /></ Tool ></ div >< EditorContent editor = { editor }/></ div ></ label >
2026-11-24 16:25:25 +08:00
</ section >
< section className = "agreement-card agreement-seo" >< div className = "agreement-card-head" >< h2 > SEO 设置 </ h2 ></ div >< div className = "agreement-seo-grid" >< label >< span > SEO 标题 * </ span >< input maxLength = { 70 } value = { seo . title } onChange = {( event ) => setSeo ({... seo , title :event.target.value })}/></ label >< label >< span > 页面 H1 * </ span >< input maxLength = { 120 } value = { seo . h1 } onChange = {( event ) => setSeo ({... seo , h1 :event.target.value })}/></ label >< label className = "wide" >< span > SEO 描述 * </ span >< textarea maxLength = { 160 } rows = { 3 } value = { seo . description } onChange = {( event ) => setSeo ({... seo , description :event.target.value })}/></ label >< label >< span > Canonical * </ span >< input value = { seo . canonicalUrl } onChange = {( event ) => setSeo ({... seo , canonicalUrl :event.target.value })}/></ label >< label >< span > Robots </ span >< select value = { seo . robots } onChange = {( event ) => setSeo ({... seo , robots :event.target.value })}>< option value = "index,follow" > 允许收录并跟踪链接 </ option >< option value = "noindex,follow" > 不收录但跟踪链接 </ option ></ select ></ label ></ div ></ section >{ notice && < p className = "agreement-notice" >{ notice }</ p >}
2026-11-23 14:45:29 +08:00
</ div >;
}
function Tool ({ title , onClick , children } : { title : string ; onClick : () => void ; children : React.ReactElement }) { return < button type = "button" title = { title } onClick = { onClick }>{ React . cloneElement ( children , { size : 18 } as never )}</ button >; }