账户登录
This commit is contained in:
1 parent
f08dcee83a
commit
8e80e26c91
57 files changed
+1401
-100
No files matched your search
+105
-9
@@ -2,8 +2,9 @@ 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 = "http://localhost:8082/api/site/v1";
|
||||
const API = import.meta.env.VITE_SITE_API_BASE || "/api/site/v1";
|
||||
type ContentType = "NEWS" | "CASE" | "HELP";
|
||||
type Item = {
|
||||
productCode?: string;
|
||||
@@ -13,7 +14,7 @@ type Item = {
|
||||
slug: string;
|
||||
summary?: string;
|
||||
model?: string;
|
||||
publishTime?: string;
|
||||
publishTime?: number;
|
||||
coverMediaUrl?: string;
|
||||
};
|
||||
type InquiryField = {
|
||||
@@ -35,6 +36,17 @@ type InquiryFormData = {
|
||||
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",
|
||||
@@ -45,6 +57,53 @@ async function post(path: string, body: unknown) {
|
||||
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<HTMLMetaElement>(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<HTMLMetaElement>(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<HTMLLinkElement>('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<HTMLScriptElement>('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"],
|
||||
@@ -62,13 +121,49 @@ function route() {
|
||||
}
|
||||
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 = <CustomCodeRuntime />;
|
||||
if (r.kind === "home") return <>{runtime}<DecoratedPage pageType={r.previewPage || "HOME"} fallback={<><Header /><main><Home /></main><Footer /></>} /></>;
|
||||
if (r.kind === "products") return <>{runtime}<DecoratedPage pageType={r.previewPage || (r.slug ? "PRODUCT_DETAIL" : "PRODUCT_CATEGORY")} fallback={<><Header /><main><Collection title="Products" type="products" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
if (r.kind === "news") return <>{runtime}<DecoratedPage pageType={r.previewPage || (r.slug ? "NEWS_DETAIL" : "NEWS_CATEGORY")} fallback={<><Header /><main><Collection title="News" type="NEWS" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
if (r.kind === "cases") return <>{runtime}<DecoratedPage pageType={r.previewPage || (r.slug ? "CASE_DETAIL" : "CASE_CATEGORY")} fallback={<><Header /><main><Collection title="Case Studies" type="CASE" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
if (r.kind === "help") return <>{runtime}<DecoratedPage pageType={r.previewPage || (r.slug ? "HELP_DETAIL" : "HELP_CATEGORY")} fallback={<><Header /><main><Collection title="Help Center" type="HELP" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
return <>{runtime}<DecoratedPage pageType={r.previewPage || (r.slug === "about-us" ? "ABOUT_US" : "CUSTOM_PAGE")} fallback={<><Header /><main><Page slug={r.slug!} /></main><Footer /></>} /></>;
|
||||
const seoRuntime = <SeoRuntime current={r} />;
|
||||
if (r.kind === "home") return <>{runtime}{seoRuntime}<DecoratedPage pageType={r.previewPage || "HOME"} fallback={<><Header /><main><Home /></main><Footer /></>} /></>;
|
||||
if (r.kind === "products") return <>{runtime}{seoRuntime}<DecoratedPage pageType={r.previewPage || (r.slug ? "PRODUCT_DETAIL" : "PRODUCT_CATEGORY")} fallback={<><Header /><main><Collection title="Products" type="products" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
if (r.kind === "news") return <>{runtime}{seoRuntime}<DecoratedPage pageType={r.previewPage || (r.slug ? "NEWS_DETAIL" : "NEWS_CATEGORY")} fallback={<><Header /><main><Collection title="News" type="NEWS" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
if (r.kind === "cases") return <>{runtime}{seoRuntime}<DecoratedPage pageType={r.previewPage || (r.slug ? "CASE_DETAIL" : "CASE_CATEGORY")} fallback={<><Header /><main><Collection title="Case Studies" type="CASE" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
if (r.kind === "help") return <>{runtime}{seoRuntime}<DecoratedPage pageType={r.previewPage || (r.slug ? "HELP_DETAIL" : "HELP_CATEGORY")} fallback={<><Header /><main><Collection title="Help Center" type="HELP" slug={r.slug} /></main><Footer /></>} /></>;
|
||||
return <>{runtime}{seoRuntime}<DecoratedPage pageType={r.previewPage || (r.slug === "about-us" ? "ABOUT_US" : "CUSTOM_PAGE")} fallback={<><Header /><main><Page slug={r.slug!} /></main><Footer /></>} /></>;
|
||||
}
|
||||
function SeoRuntime({ current }: { current: ReturnType<typeof route> }) {
|
||||
const [target, setTarget] = useState({ pageType: "", targetCode: "" });
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
const listType = current.kind === "home" ? "HOME"
|
||||
: current.kind === "products" && !current.slug ? "PRODUCT_LIST"
|
||||
: current.kind === "news" && !current.slug ? "NEWS_LIST"
|
||||
: current.kind === "cases" && !current.slug ? "CASE_LIST"
|
||||
: current.kind === "help" && !current.slug ? "HELP_CATEGORY_LIST" : "";
|
||||
if (listType) { setTarget({ pageType: listType, targetCode: "" }); return () => { active = false; }; }
|
||||
setTarget({ pageType: "", targetCode: "" });
|
||||
if (["products", "news", "cases", "help"].includes(current.kind) && current.slug) {
|
||||
const product = current.kind === "products";
|
||||
const contentType = current.kind === "cases" ? "CASE" : current.kind.toUpperCase();
|
||||
post(product ? "/products/detail" : "/contents/detail", product ? { slug: current.slug } : { contentType, slug: current.slug })
|
||||
.then(data => { if (active) setTarget({ pageType: product ? "PRODUCT_DETAIL" : `${contentType}_DETAIL`, targetCode: data.productCode || data.contentCode || "" }); })
|
||||
.catch(() => undefined);
|
||||
} else if (current.kind === "page" && current.slug) {
|
||||
post("/pages/detail", { slug: current.slug })
|
||||
.then(data => { if (active) setTarget({ pageType: data.pageType === "SYSTEM_PAGE" ? "SYSTEM_PAGE" : "CUSTOM_PAGE", targetCode: data.pageCode || "" }); })
|
||||
.catch(() => undefined);
|
||||
}
|
||||
return () => { active = false; };
|
||||
}, [current.kind, current.slug]);
|
||||
usePageSeo(target.pageType, target.targetCode);
|
||||
return null;
|
||||
}
|
||||
function CustomCodeRuntime() {
|
||||
useEffect(() => { let nodes: HTMLElement[] = []; post("/custom-codes/list", { siteCode: "SIT0000000000001" }).then(data => { (data.items || []).forEach((item: { content: string; position: string }) => { const container = item.position === "HEAD" ? document.head : document.body; const wrapper = document.createElement("div"); wrapper.innerHTML = item.content; Array.from(wrapper.children).forEach(child => { let node: HTMLElement = child as HTMLElement; if (child.tagName === "SCRIPT") { const script = document.createElement("script"); Array.from(child.attributes).forEach(attribute => script.setAttribute(attribute.name, attribute.value)); script.text = child.textContent || ""; node = script; } container.insertBefore(node, item.position === "BODY_START" ? container.firstChild : null); nodes.push(node); }); }); }).catch(() => undefined); return () => nodes.forEach(node => node.remove()); }, []);
|
||||
@@ -281,7 +376,7 @@ function Collection({
|
||||
</div>
|
||||
<div>
|
||||
<small>
|
||||
{item.model || item.publishTime?.slice(0, 10) || "INSIGHT"}
|
||||
{item.model || (item.publishTime ? new Date(item.publishTime).toLocaleDateString("en-CA") : "INSIGHT")}
|
||||
</small>
|
||||
<h2>{item.productName || item.title}</h2>
|
||||
<p>
|
||||
@@ -398,6 +493,7 @@ function InquiryForm({
|
||||
})),
|
||||
landingUrl: sessionStorage.getItem("landingUrl") || location.href,
|
||||
});
|
||||
trackInquirySubmitted(result.inquiryCode, scene);
|
||||
setNotice(result.successMessage);
|
||||
} catch (err) {
|
||||
setNotice(
|
||||
|
||||
Reference in new issue
Block a user