mirror of
https://github.com/KivotosRailway/website.git
synced 2026-11-03 11:06:32 +09:00
修复首页新闻板块未跟随语言的错误
This commit is contained in:
+3
-3
@@ -1,8 +1,8 @@
|
|||||||
import { HomePageContent } from "@/components/home/home-page-content";
|
import { HomePageContent } from "@/components/home/home-page-content";
|
||||||
import { getLatestNews } from "@/data/news";
|
import { getNewsCatalog } from "@/data/news";
|
||||||
|
|
||||||
export default async function Home() {
|
export default async function Home() {
|
||||||
const news = await getLatestNews();
|
const newsByLocale = await getNewsCatalog();
|
||||||
|
|
||||||
return <HomePageContent news={news} />;
|
return <HomePageContent newsByLocale={newsByLocale} />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { useEffect, useState } from "react";
|
|||||||
import type { NewsItem } from "@/data/news";
|
import type { NewsItem } from "@/data/news";
|
||||||
import { applyLocale, getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n";
|
import { applyLocale, getInitialLocale, getLocaleMessages, type Locale } from "@/lib/i18n";
|
||||||
|
|
||||||
export function HomePageContent({ news }: { news: NewsItem[] }) {
|
export function HomePageContent({ newsByLocale }: { newsByLocale: Record<Locale, NewsItem[]> }) {
|
||||||
const [locale, setLocale] = useState<Locale>("zh-CN");
|
const [locale, setLocale] = useState<Locale>("zh-CN");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -38,6 +38,7 @@ export function HomePageContent({ news }: { news: NewsItem[] }) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const t = getLocaleMessages(locale);
|
const t = getLocaleMessages(locale);
|
||||||
|
const visibleNews = newsByLocale[locale] ?? newsByLocale["zh-CN"] ?? [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="home-page">
|
<main className="home-page">
|
||||||
@@ -69,7 +70,7 @@ export function HomePageContent({ news }: { news: NewsItem[] }) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="news-grid" aria-label={t.home.latest}>
|
<div className="news-grid" aria-label={t.home.latest}>
|
||||||
{news.map((post) => (
|
{visibleNews.slice(0, 3).map((post) => (
|
||||||
<article key={post.id} className="news-card">
|
<article key={post.id} className="news-card">
|
||||||
<Link href={post.href} className="news-card-link" aria-label={post.title}>
|
<Link href={post.href} className="news-card-link" aria-label={post.title}>
|
||||||
<div className="news-visual" style={{ backgroundImage: `url(${post.cover})` }} />
|
<div className="news-visual" style={{ backgroundImage: `url(${post.cover})` }} />
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ export async function getNewsBySlug(slug: string, locale: Locale = "zh-CN"): Pro
|
|||||||
return items.find((item) => item.slug === slug) ?? null;
|
return items.find((item) => item.slug === slug) ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLatestNews() {
|
export async function getLatestNews(locale: Locale = "zh-CN") {
|
||||||
const items = await getNewsList();
|
const items = await getNewsList(locale);
|
||||||
return items.slice(0, 3);
|
return items.slice(0, 3);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user