"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { api } from "@/lib/api"; import { useI18n } from "@/lib/i18n"; import { useToast } from "@/lib/toast"; import { mergeDestinationsIntoTrip } from "@/lib/tripActions"; import type { Destination } from "@/lib/types"; import RingNext from "@/components/RingNext"; import { useRingSteps } from "@/lib/rings"; const SUGGESTIONS = [ "预算 6000,想找网速好的东南亚城市", "适合第一次远程办公的城市?", "清迈和巴厘岛怎么选?", ]; interface Props { destinations: Destination[]; } export default function AiAssistantClient({ destinations }: Props) { const { t } = useI18n(); const { toast } = useToast(); const router = useRouter(); const rings = useRingSteps(); const [message, setMessage] = useState(""); const [reply, setReply] = useState(""); const [cities, setCities] = useState<{ slug: string; name: string; emoji: string }[]>([]); const [loading, setLoading] = useState(false); const ask = async (text?: string) => { const q = (text ?? message).trim(); if (!q) { toast("先写一个问题", "info"); return; } setMessage(q); setLoading(true); try { const res = await api.askAssistant(q); setReply(res.reply); setCities(res.cities || []); } catch { setReply(t.ai.fail); setCities([]); } finally { setLoading(false); } }; const addToPlan = () => { const dests = cities .map((c) => destinations.find((d) => d.slug === c.slug)) .filter((d): d is Destination => Boolean(d)); if (dests.length === 0) { toast("推荐城市暂无完整数据,请从详情页加入计划", "info"); router.push("/plan"); return; } const { added, skipped } = mergeDestinationsIntoTrip(dests, 1); if (added === 0) { toast(skipped ? t.compare.alreadyInPlan : "未能写入计划", "info"); } else { toast(`${t.compare.addedN} ${added} ${t.compare.citiesUnit}`); } router.push("/plan"); }; return (
{t.ai.subtitle}
{reply}