's'
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { use } from "react";
|
||||
import { useTranslation, Link } from "@/i18n/navigation";
|
||||
import { use, useEffect, useMemo, useState } from "react";
|
||||
import { Link } from "@/i18n/navigation";
|
||||
import Footer from "@/app/components/Footer";
|
||||
import { apiFetch } from "@/app/lib/api-client";
|
||||
|
||||
interface CityGuide {
|
||||
slug: string;
|
||||
@@ -149,11 +150,134 @@ const CITY_GUIDES: CityGuide[] = [
|
||||
},
|
||||
];
|
||||
|
||||
type CityApiRecord = {
|
||||
name?: string;
|
||||
nameEn?: string;
|
||||
icon?: string;
|
||||
description?: string;
|
||||
costPerMonth?: number;
|
||||
internetSpeed?: number;
|
||||
nomadsNow?: number;
|
||||
tags?: string[];
|
||||
};
|
||||
|
||||
type CityDetailRecord = {
|
||||
guide?: {
|
||||
summary?: string;
|
||||
arrivalChecklist?: string[];
|
||||
workSetup?: string;
|
||||
bestFor?: string[];
|
||||
};
|
||||
cost?: {
|
||||
breakdown?: Array<{ label?: string; amount?: number }>;
|
||||
monthlyTotal?: number;
|
||||
tip?: string;
|
||||
};
|
||||
pros?: {
|
||||
pros?: string[];
|
||||
cons?: string[];
|
||||
};
|
||||
};
|
||||
|
||||
function mergeCityGuide(slug: string, apiCity?: CityApiRecord | null, detail?: CityDetailRecord | null): CityGuide | null {
|
||||
const fallback = CITY_GUIDES.find((c) => c.slug === slug);
|
||||
if (!apiCity && !fallback) return null;
|
||||
|
||||
const breakdown = detail?.cost?.breakdown || [];
|
||||
const costOfLiving =
|
||||
breakdown.length > 0
|
||||
? breakdown.map((item) => ({
|
||||
item: String(item.label || "项目"),
|
||||
priceZh: item.amount ? `¥${Number(item.amount).toLocaleString("zh-CN")}/月` : "—",
|
||||
priceEn: item.amount ? `¥${Number(item.amount).toLocaleString("en-US")}/month` : "—",
|
||||
}))
|
||||
: fallback?.costOfLiving || [];
|
||||
|
||||
const tips =
|
||||
(detail?.guide?.arrivalChecklist?.length ? detail.guide.arrivalChecklist : null) ||
|
||||
fallback?.tips ||
|
||||
[];
|
||||
|
||||
return {
|
||||
slug,
|
||||
nameZh: apiCity?.name || fallback?.nameZh || slug,
|
||||
nameEn: apiCity?.nameEn || fallback?.nameEn || slug,
|
||||
emoji: apiCity?.icon || fallback?.emoji || "🏙️",
|
||||
descriptionZh:
|
||||
detail?.guide?.summary ||
|
||||
apiCity?.description ||
|
||||
fallback?.descriptionZh ||
|
||||
"",
|
||||
descriptionEn: fallback?.descriptionEn || "",
|
||||
costOfLiving,
|
||||
coworking: fallback?.coworking || [],
|
||||
neighborhoods:
|
||||
detail?.pros?.pros?.length || detail?.pros?.cons?.length
|
||||
? [
|
||||
...(detail?.pros?.pros || []).slice(0, 2).map((pros, index) => ({
|
||||
name: `优势 ${index + 1}`,
|
||||
pros,
|
||||
cons: detail?.pros?.cons?.[index] || "—",
|
||||
})),
|
||||
]
|
||||
: fallback?.neighborhoods || [],
|
||||
tips,
|
||||
};
|
||||
}
|
||||
|
||||
export default function CityGuidePage({ params }: { params: Promise<{ slug: string }> }) {
|
||||
const { slug } = use(params);
|
||||
const { t } = useTranslation("cityGuide");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [apiError, setApiError] = useState(false);
|
||||
const [apiCity, setApiCity] = useState<CityApiRecord | null>(null);
|
||||
const [apiDetail, setApiDetail] = useState<CityDetailRecord | null>(null);
|
||||
|
||||
const city = CITY_GUIDES.find((c) => c.slug === slug);
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
apiFetch<{
|
||||
city?: CityApiRecord;
|
||||
detail?: CityDetailRecord | null;
|
||||
}>(`/api/cities/${slug}`)
|
||||
.then((data) => {
|
||||
if (cancelled) return;
|
||||
setApiCity(data.city || null);
|
||||
setApiDetail(data.detail || null);
|
||||
setApiError(false);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setApiError(true);
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [slug]);
|
||||
|
||||
const city = useMemo(
|
||||
() => mergeCityGuide(slug, apiCity, apiDetail),
|
||||
[slug, apiCity, apiDetail]
|
||||
);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-[#fafafa] dark:bg-gray-950">
|
||||
<main className="max-w-[1200px] mx-auto px-4 sm:px-6 py-6 sm:py-8">
|
||||
<div className="h-4 w-24 animate-pulse rounded bg-gray-200 dark:bg-gray-800" />
|
||||
<div className="mt-4 overflow-hidden rounded-2xl bg-white shadow-lg dark:bg-gray-900">
|
||||
<div className="h-40 animate-pulse bg-gray-200 dark:bg-gray-800" />
|
||||
<div className="space-y-4 p-6 sm:p-8">
|
||||
<div className="h-6 w-2/3 animate-pulse rounded bg-gray-200 dark:bg-gray-800" />
|
||||
<div className="h-4 w-full animate-pulse rounded bg-gray-100 dark:bg-gray-800/80" />
|
||||
<div className="h-4 w-5/6 animate-pulse rounded bg-gray-100 dark:bg-gray-800/80" />
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
<Footer />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!city) {
|
||||
return (
|
||||
@@ -181,6 +305,11 @@ export default function CityGuidePage({ params }: { params: Promise<{ slug: stri
|
||||
</div>
|
||||
|
||||
<div className="p-6 sm:p-8">
|
||||
{apiError && (
|
||||
<p className="mb-4 rounded-xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-800 dark:border-amber-900/50 dark:bg-amber-950/30 dark:text-amber-200">
|
||||
城市数据暂时无法同步,当前展示本地指南内容。
|
||||
</p>
|
||||
)}
|
||||
<p className="text-gray-600 dark:text-gray-400 text-base sm:text-lg mb-8">
|
||||
{city.descriptionZh}
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user