修改页面标题路由

This commit is contained in:
2026-08-22 22:40:25 +08:00
parent 4e778d895b
commit 65322c20c1
11 changed files with 137 additions and 15 deletions
+2 -1
View File
@@ -1,8 +1,9 @@
import Link from "next/link";
import { getLocaleMessages } from "@/lib/i18n";
export function PageBreadcrumb({ items }: { items: Array<{ label: string; href?: string }> }) {
return (
<nav className="page-breadcrumb" aria-label="页面位置">
<nav className="page-breadcrumb" aria-label={getLocaleMessages("zh-CN").common.pagePosition}>
{items.map((item, index) => (
<span className="page-breadcrumb-item" key={`${item.label}-${index}`}>
{index > 0 && <span className="page-breadcrumb-separator" aria-hidden="true">›</span>}
+17 -1
View File
@@ -10,7 +10,23 @@ export function SiteHeader() {
const [menuOpen, setMenuOpen] = useState(false);
useEffect(() => {
const syncLocale = (nextLocale: Locale) => setLocale(nextLocale);
const updateDocumentTitle = (nextLocale: Locale) => {
const messages = getLocaleMessages(nextLocale);
const pathname = window.location.pathname;
const currentTitle = document.title.split(" | ")[0];
const pageLabel = pathname === "/"
? ""
: pathname === "/news"
? messages.common.news
: currentTitle;
document.title = pageLabel ? `${pageLabel} | ${messages.common.siteTitle}` : messages.common.siteTitle;
};
const syncLocale = (nextLocale: Locale) => {
setLocale(nextLocale);
updateDocumentTitle(nextLocale);
};
const handleLocaleChange = (event: Event) => {
const nextLocale = (event as CustomEvent<{ locale?: Locale }>).detail?.locale ?? getInitialLocale();
+17 -3
View File
@@ -1,11 +1,25 @@
"use client";
import Link from "next/link";
import { useState } from "react";
import { useEffect, useState } from "react";
import type { NewsItem } from "@/data/news";
import { getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n";
export function NewsList({ news }: { news: NewsItem[] }) {
const [activeCategory, setActiveCategory] = useState<string | null>(null);
const [locale, setLocale] = useState<Locale>("zh-CN");
const messages = getLocaleMessages(locale);
useEffect(() => {
const syncLocale = () => setLocale(getInitialLocale());
syncLocale();
window.addEventListener("kr-locale-change", syncLocale);
window.addEventListener("storage", syncLocale);
return () => {
window.removeEventListener("kr-locale-change", syncLocale);
window.removeEventListener("storage", syncLocale);
};
}, []);
const categories = Array.from(new Set(news.map((item) => item.category).filter(Boolean)));
const visibleNews = activeCategory
? news.filter((item) => item.category === activeCategory)
@@ -20,7 +34,7 @@ export function NewsList({ news }: { news: NewsItem[] }) {
className={`news-filter-button${activeCategory === null ? " active" : ""}`}
onClick={() => setActiveCategory(null)}
>
全部
{messages.common.all}
</button>
{categories.map((category) => (
<button
@@ -34,7 +48,7 @@ export function NewsList({ news }: { news: NewsItem[] }) {
))}
</div>
<div className="news-toolbar-right">
<button type="button" className="news-view-toggle" aria-label="列表视图">☰</button>
<button type="button" className="news-view-toggle" aria-label={messages.common.listView}>☰</button>
</div>
</div>
+20 -6
View File
@@ -1,9 +1,23 @@
"use client";
import { useState } from "react";
import { useEffect, useState } from "react";
import { getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n";
export function NewsShare({ title }: { title: string }) {
const [copied, setCopied] = useState(false);
const [locale, setLocale] = useState<Locale>("zh-CN");
const messages = getLocaleMessages(locale);
useEffect(() => {
const syncLocale = () => setLocale(getInitialLocale());
syncLocale();
window.addEventListener("kr-locale-change", syncLocale);
window.addEventListener("storage", syncLocale);
return () => {
window.removeEventListener("kr-locale-change", syncLocale);
window.removeEventListener("storage", syncLocale);
};
}, []);
const getPageUrl = () => window.location.href;
@@ -27,13 +41,13 @@ export function NewsShare({ title }: { title: string }) {
return (
<section className="news-share" aria-labelledby="news-share-title">
<h2 id="news-share-title">分享</h2>
<h2 id="news-share-title">{messages.common.share}</h2>
<div className="news-share-actions">
<button type="button" className="news-share-button" onClick={copyLink} aria-label="复制链接">
{copied ? "已复制" : "复制链接"}
<button type="button" className="news-share-button" onClick={copyLink} aria-label={messages.common.copyLink}>
{copied ? messages.common.copied : messages.common.copyLink}
</button>
<button type="button" className="news-share-button news-share-x" onClick={shareOnX} aria-label="分享到 X">X</button>
<button type="button" className="news-share-button news-share-qq" onClick={shareOnQq} aria-label="分享到 QQ">QQ</button>
<button type="button" className="news-share-button news-share-x" onClick={shareOnX} aria-label={messages.common.shareX}>X</button>
<button type="button" className="news-share-button news-share-qq" onClick={shareOnQq} aria-label={messages.common.shareQq}>QQ</button>
</div>
</section>
);