'use client'; import { useMutation } from '@tanstack/react-query'; import { AlertTriangle, Check, RefreshCw, Sparkles } from 'lucide-react'; import Link from 'next/link'; import * as React from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { analyticsApi, type ProjectAiAdvice } from '@/lib/analytics-api'; import { ApiError } from '@/lib/api-client'; import { useAuthStore } from '@/lib/auth-store'; interface ProjectAiAdviceCardProps { projectId: string; /** * Persona labels already rendered by ProjectPersonaFitCard. We de-dupe * before showing AI-suggested ones so users don't see the same chip twice. */ existingPersonas?: string[]; } export function ProjectAiAdviceCard({ projectId, existingPersonas = [], }: ProjectAiAdviceCardProps) { const user = useAuthStore((s) => s.user); const isAdmin = user?.role === 'ADMIN'; const mutation = useMutation({ mutationFn: () => analyticsApi.getProjectAiAdvice(projectId), }); const { data, error, isPending, isSuccess } = mutation; // Initial state — show trigger button. if (!isSuccess && !isPending && !error) { return ( ); } // Loading — skeleton. if (isPending) { return ( AI đang phân tích dự án…
); } // Error state. if (error) { const apiErr = error instanceof ApiError ? error : null; const status = apiErr?.status ?? 0; const notConfigured = status === 503; if (notConfigured) { return (

AI chưa được cấu hình. Liên hệ quản trị viên.

{isAdmin && ( Cấu hình Claude API → )}
); } return (

Không lấy được phân tích AI. {apiErr?.message ?? 'Vui lòng thử lại.'}

); } if (!data) return null; const { advice } = data; const extraPersonas = advice.suitableFor.filter( (p) => !existingPersonas.includes(p), ); return ( AI nhận định dự án {advice.summary &&

{advice.summary}

}
{advice.pros.length > 0 && (

Điểm mạnh

    {advice.pros.map((p, i) => (
  • {p}
  • ))}
)} {advice.cons.length > 0 && (

Cần cân nhắc

    {advice.cons.map((c, i) => (
  • {c}
  • ))}
)}
{extraPersonas.length > 0 && (

Phù hợp với

{extraPersonas.map((p) => (
{p} AI gợi ý
))}
)}
); }