import React, { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import { ArrowRight, ChevronRight, Mail, Menu, Search } from "lucide-react"; import "./styles.css"; import { initializeAnalytics, routePageCode, trackInquirySubmitted, trackPageLeave, trackPageView } from "./analytics"; const API = import.meta.env.VITE_SITE_API_BASE || "/api/site/v1"; type ContentType = "NEWS" | "CASE" | "HELP"; type Item = { productCode?: string; contentCode?: string; productName?: string; title?: string; slug: string; summary?: string; model?: string; publishTime?: number; coverMediaUrl?: string; }; type InquiryField = { fieldCode: string; fieldKey: string; label: string; fieldType: string; placeholder?: string; helpText?: string; maxLength?: number; required: boolean; optionsJson?: string; widthType: string; }; type InquiryFormData = { formCode: string; description?: string; submitButtonText: string; successMessage: string; fields: InquiryField[]; }; type SeoData = { title: string; description?: string; keywords?: string; canonicalUrl?: string; robots?: string; ogTitle?: string; ogDescription?: string; ogImageUrl?: string; structuredData?: string; }; 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((await r.json()).message || "Unable to load content"); return r.json(); } function setMeta(name: string, content?: string) { const selector = `meta[name="${name}"]`; let element = document.head.querySelector(selector); if (!content) { element?.remove(); return; } if (!element) { element = document.createElement("meta"); element.name = name; document.head.appendChild(element); } element.content = content; } function setProperty(property: string, content?: string) { const selector = `meta[property="${property}"]`; let element = document.head.querySelector(selector); if (!content) { element?.remove(); return; } if (!element) { element = document.createElement("meta"); element.setAttribute("property", property); document.head.appendChild(element); } element.content = content; } function applySeo(data?: SeoData) { const canonicalValue = data?.canonicalUrl || `${location.origin}${location.pathname}`; const canonical = new URL(canonicalValue, location.origin).href; if (data?.title) document.title = data.title; setMeta("description", data?.description); setMeta("keywords", data?.keywords); setMeta("robots", data?.robots || "index,follow"); let link = document.head.querySelector('link[rel="canonical"]'); if (!link) { link = document.createElement("link"); link.rel = "canonical"; document.head.appendChild(link); } link.href = canonical; setProperty("og:title", data?.ogTitle || data?.title); setProperty("og:description", data?.ogDescription || data?.description); setProperty("og:image", data?.ogImageUrl ? new URL(data.ogImageUrl, location.origin).href : undefined); setProperty("og:url", canonical); let structuredData = document.head.querySelector('script[data-shoplix-seo="json-ld"]'); structuredData?.remove(); if (data?.structuredData) { structuredData = document.createElement("script"); structuredData.type = "application/ld+json"; structuredData.dataset.shoplixSeo = "json-ld"; structuredData.text = data.structuredData; document.head.appendChild(structuredData); } } function usePageSeo(pageType: string, targetCode = "") { useEffect(() => { let active = true; applySeo(); if (!pageType) return () => { active = false; }; post("/seo/detail", { pageType, targetCode }) .then((data: SeoData) => { if (active) applySeo(data); }) .catch(() => undefined); return () => { active = false; }; }, [pageType, targetCode]); } const nav = [ ["Products", "/products"], ["Cases", "/cases"], ["News", "/news"], ["Help", "/help"], ["About", "/about-us"], ]; type NavigationItem = { title: string; url: string; openType: "CURRENT" | "NEW"; children: NavigationItem[] }; /** 读取已发布导航,没有配置时返回既有默认菜单。 */ function useNavigation(position: "HEADER" | "FOOTER"): NavigationItem[] { const fallback = nav.map(([title, url]) => ({ title, url, openType: "CURRENT" as const, children: [] })); const [items, setItems] = useState(fallback); useEffect(() => { let active = true; post("/navigations/detail", { position }) .then((data) => { if (active && data.items?.length) setItems(data.items); }) .catch(() => undefined); return () => { active = false; }; }, [position]); return items; } /** 渲染支持二级菜单的前台导航链接。 */ function NavigationLinks({ items }: { items: NavigationItem[] }) { return <>{items.map((item) =>
{item.title}{item.children?.length > 0 &&
{item.children.map((child) => {child.title})}
}
)}; } function route() { const p = location.pathname.split("/").filter(Boolean); const previewPage = new URLSearchParams(location.search).get("previewPage") || undefined; if (!p.length) return { kind: "home", previewPage }; if (["products", "news", "cases", "help"].includes(p[0])) return { kind: p[0], slug: p[1], previewPage }; return { kind: "page", slug: p[0], previewPage }; } function App() { const r = route(); useEffect(() => { initializeAnalytics(); const context = { pageCode: routePageCode(), pageName: document.title, scene: r.kind }; trackPageView(context); const leave = () => trackPageLeave(context); window.addEventListener("pagehide", leave, { once: true }); return () => window.removeEventListener("pagehide", leave); }, [r.kind, r.slug]); const runtime = ; const seoRuntime = ; if (r.kind === "home") return <>{runtime}{seoRuntime}