// File: components/ai/ai-suggestion-field.tsx // Component แสดง AI Suggestion พร้อม Accept / Reject / Edit actions (ADR-018, ADR-020) 'use client'; import { useEffect, useState } from 'react'; import { Check, X, Edit2, Sparkles } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import apiClient from '@/lib/api/client'; import { cn } from '@/lib/utils'; // สีตาม confidence score ของ ADR-023A const getConfidenceClass = (confidence: number): string => { if (confidence >= 0.85) return 'text-green-700 bg-green-50 border-green-400'; if (confidence >= 0.6) return 'text-yellow-700 bg-yellow-50 border-yellow-400'; return 'text-muted-foreground bg-muted border-border'; }; const getConfidenceLabel = (confidence: number): string => { if (confidence >= 0.85) return 'AI แนะนำ'; if (confidence >= 0.6) return 'ตรวจสอบก่อนยืนยัน'; return ''; }; export interface AiSuggestionFieldProps { label: string; value: string; suggestion?: string; confidence?: number; isUnknown?: boolean; jobId?: string; onJobCompleted?: (result: unknown) => void; onJobFailed?: () => void; onAccept: () => void; onReject: () => void; onEdit: (newValue: string) => void; className?: string; } export function AiSuggestionField({ label, value, suggestion, confidence, isUnknown = false, jobId, onJobCompleted, onJobFailed, onAccept, onReject, onEdit, className, }: AiSuggestionFieldProps) { const [isEditing, setIsEditing] = useState(false); const [editValue, setEditValue] = useState(value); // มีค่าจาก AI หรือไม่ const hasSuggestion = suggestion !== undefined && suggestion !== ''; // ค่าปัจจุบันตรงกับที่ AI แนะนำ const isAiValue = hasSuggestion && value === suggestion; const handleSave = () => { onEdit(editValue); setIsEditing(false); }; const handleStartEdit = () => { setEditValue(value); setIsEditing(true); }; useEffect(() => { if (!jobId) return undefined; const interval = window.setInterval(() => { void apiClient .get(`/ai/jobs/${jobId}/status`) .then((response) => { const status = response.data?.status as string | undefined; if (status === 'completed') { window.clearInterval(interval); onJobCompleted?.(response.data?.result); } if (status === 'failed') { window.clearInterval(interval); onJobFailed?.(); } }) .catch(() => { window.clearInterval(interval); onJobFailed?.(); }); }, 3000); return () => window.clearInterval(interval); }, [jobId, onJobCompleted, onJobFailed]); return (
{isUnknown ? 'ไม่รู้จัก — กรุณาเลือกจาก dropdown: ' : 'AI แนะนำ: '}
)}