"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Skeleton } from "@/components/ui/skeleton"; import { Loader2, Rocket, Target, TrendingUp, Plus, Users, BarChart3, ArrowLeft, CheckCircle2, LayoutDashboard, AlertTriangle, } from "lucide-react"; import { api } from "@/lib/api"; import type { ActionSuggestion } from "@/types/onboarding"; interface Step5ActionSuggestionsProps { brandId: string; brandName: string; onComplete: () => void; onBack: () => void; onSkip: () => void; } const ACTION_ICONS: Record = { improve_platform: BarChart3, add_competitor: Users, optimize_content: TrendingUp, increase_frequency: Rocket, }; const PRIORITY_COLORS: Record< string, { bg: string; text: string; border: string } > = { high: { bg: "bg-red-50", text: "text-red-600", border: "border-red-200" }, medium: { bg: "bg-amber-50", text: "text-amber-600", border: "border-amber-200", }, low: { bg: "bg-blue-50", text: "text-blue-600", border: "border-blue-200" }, }; export function Step5ActionSuggestions({ brandId, brandName, onComplete, onBack, onSkip, }: Step5ActionSuggestionsProps) { const { data: session } = useSession(); const router = useRouter(); const [suggestions, setSuggestions] = useState([]); const [loading, setLoading] = useState(true); const [completing, setCompleting] = useState(false); const [error, setError] = useState(null); const fetchSuggestions = async () => { if (!session?.accessToken) return; try { setLoading(true); setError(null); const data = (await api.onboarding.getActionSuggestions( session.accessToken, brandId, )) as ActionSuggestion[]; setSuggestions(data || []); } catch (err) { console.error("获取行动建议失败:", err); setError("获取行动建议失败,请重试"); } finally { setLoading(false); } }; useEffect(() => { fetchSuggestions(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [session?.accessToken, brandId]); const handleComplete = async () => { if (!session?.accessToken) return; try { setCompleting(true); await api.onboarding.completeOnboarding(session.accessToken, brandId); onComplete(); } catch (err) { console.error("完成引导失败:", err); // 即使API失败也继续,因为品牌已创建 onComplete(); } finally { setCompleting(false); } }; const _handleGoToDashboard = () => { router.push("/dashboard"); }; if (loading) { return (

正在生成行动建议...

基于您的品牌表现,为您定制优化方案

); } // 错误状态 if (!loading && error) { return (

获取建议失败

{error}

); } // 按优先级分组 const highPriority = suggestions.filter((s) => s.priority === "high"); const mediumPriority = suggestions.filter((s) => s.priority === "medium"); const lowPriority = suggestions.filter((s) => s.priority === "low"); const renderSuggestionCard = ( suggestion: ActionSuggestion, index: number, ) => { const Icon = ACTION_ICONS[suggestion.action_type] || Target; const colors = PRIORITY_COLORS[suggestion.priority]; return (

{suggestion.title}

{suggestion.priority === "high" ? "高优" : suggestion.priority === "medium" ? "中优" : "低优"}

{suggestion.description}

{index + 1}
); }; return (

下一步行动建议

基于您的品牌 “{brandName}” 的表现, 我们为您准备了以下优化建议

{/* 高优先级建议 */} {highPriority.length > 0 && ( 主要行动(最需要改进) {highPriority.map((suggestion, index) => renderSuggestionCard(suggestion, index), )} )} {/* 中优先级建议 */} {mediumPriority.length > 0 && ( 次要行动(可选项) {mediumPriority.map((suggestion, index) => renderSuggestionCard(suggestion, index + highPriority.length), )} )} {/* 低优先级建议 */} {lowPriority.length > 0 && ( 额外建议 {lowPriority.map((suggestion, index) => renderSuggestionCard( suggestion, index + highPriority.length + mediumPriority.length, ), )} )} {error &&

{error}

}

您可以随时在品牌详情页查看完整建议

); }