"use client"; import { useState } from "react"; import Link from "next/link"; import { api } from "@/lib/api"; import { useI18n } from "@/lib/i18n"; import { useToast } from "@/lib/toast"; import { mergeDestinationsIntoTrip } from "@/lib/tripActions"; import { meetupCityHref } from "@/lib/meetupLinks"; import type { CostResult, Destination } from "@/lib/types"; interface Props { destinations: Destination[] } export default function Calculator({ destinations }: Props) { const { t } = useI18n(); const { toast } = useToast(); const [slug, setSlug] = useState(destinations[0]?.slug || ""); const [housing, setHousing] = useState("mid"); const [months, setMonths] = useState(3); const [result, setResult] = useState(null); const [loading, setLoading] = useState(false); const selected = destinations.find((d) => d.slug === slug); const calculate = async () => { setLoading(true); try { const data = await api.calculateCost({ destination_slug: slug, housing, months }); setResult(data); } catch { setResult(null); } finally { setLoading(false); } }; const addToPlan = () => { if (!selected) return; const { added } = mergeDestinationsIntoTrip([selected], months); toast( added ? t.common.addedToPlanMonths .replace("{emoji}", selected.emoji) .replace("{city}", selected.name) .replace("{months}", String(months)) : t.common.alreadyInPlan, added ? "success" : "info", { href: "/plan", label: t.strip.openPlan } ); }; return (
🧮 CALCULATOR

旅居费用计算器

根据目的地和生活方式,精准估算你的旅居预算

{[{ v: "budget", l: "💰 经济" }, { v: "mid", l: "🏠 舒适" }, { v: "premium", l: "✨ 豪华" }].map((o) => ( ))}
setMonths(+e.target.value)} />
{result ? (
{result.emoji}

{result.destination}

{result.months} 个月旅居预算

{Object.entries(result.breakdown).map(([k, v]) => (
{k}
¥{v.toLocaleString()}
))}
月均¥{result.per_month.toLocaleString()}
总计¥{result.total.toLocaleString()}
{selected && (
{t.common.detail} {t.common.cityMeetups}
)}
) : (
🧮

选择目的地和参数,点击计算查看预算明细

)}
); }