"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; 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, Activity, TrendingUp, TrendingDown, Minus, ArrowRight, ArrowLeft, BarChart3, Trophy, AlertTriangle, } from "lucide-react"; import { api } from "@/lib/api"; import { PLATFORM_MAP } from "@/lib/platforms"; import { getHealthLevel, HEALTH_LEVELS, type BrandHealthReport, } from "@/types/onboarding"; interface Step4HealthReportProps { brandId: string; brandName: string; competitors: string[]; platforms: string[]; onNext: (report: BrandHealthReport) => void; onBack: () => void; } export function Step4HealthReport({ brandId, brandName, competitors: _competitors, platforms, onNext, onBack, }: Step4HealthReportProps) { const { data: session } = useSession(); const [report, setReport] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const fetchReport = async () => { if (!session?.accessToken) return; try { setLoading(true); setError(null); const data = (await api.onboarding.getHealthReport( session.accessToken, brandId, )) as BrandHealthReport; setReport(data); } catch (err) { console.error("获取健康报告失败:", err); setError("获取健康报告失败,请重试"); } finally { setLoading(false); } }; useEffect(() => { fetchReport(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [session?.accessToken, brandId]); if (loading) { return (

正在生成健康报告...

系统正在分析您的品牌在AI搜索中的表现

); } if (error || (!loading && !report)) { return (

生成报告失败

{error || "无法生成健康报告,请稍后重试"}

); } // TypeScript 类型守卫:经过 loading 和 error 检查后,report 必定存在 if (!report) return null; const healthLevel = getHealthLevel(report.overall_score); const healthConfig = HEALTH_LEVELS[healthLevel]; // 计算领先/落后竞品数量 const leadingCount = report.competitor_scores.filter( (c) => c.is_leading, ).length; const laggingCount = report.competitor_scores.length - leadingCount; return (

{brandName} 健康报告

以下是您的品牌在AI搜索中的综合表现

{/* 综合评分卡片 */}
{/* 评分大数字 */}
{report.overall_score} /100
{/* 健康等级标签 */} {healthConfig.label} {/* 趋势指示 */}
领先 {leadingCount} 个竞品 | 落后 {laggingCount} 个竞品
{/* 平台评分 */} 各平台评分
{Object.entries(report.platform_scores) .filter(([key]) => platforms.includes(key)) .map(([platform, score]) => { const level = getHealthLevel(score); const config = HEALTH_LEVELS[level]; const platformName = PLATFORM_MAP[platform] || platform; return (
{platformName}
{score}
); })}
{/* 竞品对比 */} {report.competitor_scores.length > 0 && ( 竞品对比
{report.competitor_scores.map((competitor, index) => (
{competitor.is_leading ? ( ) : ( )}
{competitor.name}
= 70 ? "text-emerald-600" : competitor.score >= 40 ? "text-amber-600" : "text-red-600" }`} > {competitor.score}分 {competitor.is_leading && ( 领先 )}
))}
)} {/* 优劣势分析 */} 优劣势分析

优势

    {report.strengths.map((strength, index) => (
  • + {strength}
  • ))}

劣势

    {report.weaknesses.map((weakness, index) => (
  • - {weakness}
  • ))}
); }