// File: frontend/components/ai/ai-chat-messages.tsx // Change Log: // - 2026-05-19: สร้างคอมโพเนนต์แสดงผลประวัติการสนทนาและการตอบสนองของ AI 'use client'; import { useRef, useEffect } from 'react'; import { Bot, User, AlertCircle, Loader2, Sparkles } from 'lucide-react'; import { ChatMessage } from '@/types/ai-chat'; import { ScrollArea } from '@/components/ui/scroll-area'; interface AiChatMessagesProps { messages: ChatMessage[]; isLoading: boolean; onSuggestedActionClick: (query: string) => void; } export function AiChatMessages({ messages, isLoading, onSuggestedActionClick }: AiChatMessagesProps) { const bottomRef = useRef(null); useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages, isLoading]); const parseMarkdown = (text: string) => { if (!text) return null; const lines = text.split('\n'); let inCodeBlock = false; let codeBlockContent: string[] = []; const elements: React.ReactNode[] = []; const renderInline = (lineText: string, key: string) => { const parts = lineText.split(/(\*\*.*?\*\*|`.*?`)/g); return ( {parts.map((part, index) => { if (part.startsWith('**') && part.endsWith('**')) { return {part.slice(2, -2)}; } if (part.startsWith('`') && part.endsWith('`')) { return {part.slice(1, -1)}; } return part; })} ); }; lines.forEach((line, index) => { const trimmed = line.trim(); if (trimmed.startsWith('```')) { if (inCodeBlock) { elements.push(
              {codeBlockContent.join('\n')}
            
); codeBlockContent = []; inCodeBlock = false; } else { inCodeBlock = true; } return; } if (inCodeBlock) { codeBlockContent.push(line); return; } if (trimmed.startsWith('- ') || trimmed.startsWith('* ')) { elements.push(
  • {renderInline(trimmed.slice(2), `li-inline-${index}`)}
  • ); return; } if (/^\d+\.\s/.test(trimmed)) { const dotIndex = trimmed.indexOf('.'); elements.push(
  • {renderInline(trimmed.slice(dotIndex + 1).trim(), `ol-inline-${index}`)}
  • ); return; } if (trimmed.startsWith('#')) { const hashCount = (trimmed.match(/^#+/) || [''])[0].length; const headerText = trimmed.replace(/^#+\s*/, ''); if (hashCount === 1) elements.push(

    {renderInline(headerText, `h1-inline-${index}`)}

    ); else if (hashCount === 2) elements.push(

    {renderInline(headerText, `h2-inline-${index}`)}

    ); else elements.push(

    {renderInline(headerText, `h3-inline-${index}`)}

    ); return; } if (trimmed === '') { elements.push(
    ); return; } elements.push(

    {renderInline(line, `p-inline-${index}`)}

    ); }); return
    {elements}
    ; }; return (
    {messages.length === 0 && (

    ยินดีต้อนรับสู่ระบบช่วยเหลือ AI

    คุณสามารถพิมพ์ถามคำถามเพื่อขอสรุปข้อมูล ตรวจสอบความถูกต้อง หรือค้นหาเงื่อนไขในเอกสารฉบับนี้ได้ทันที

    )} {messages.map((message) => { const isUser = message.role === 'user'; const isError = message.content === 'ไม่สามารถเชื่อมต่อ AI ได้ กรุณาลองใหม่'; return (
    {!isUser && (
    {isError ? : }
    )}
    {message.isStreaming ? (
    AI กำลังอ่านวิเคราะห์ข้อมูลเอกสาร...
    ) : isUser ? (

    {message.content}

    ) : ( parseMarkdown(message.content) )}
    {!isUser && message.suggestedActions && message.suggestedActions.length > 0 && (
    {message.suggestedActions.map((action, idx) => ( ))}
    )}
    {isUser && (
    )}
    ); })} {isLoading && messages.length > 0 && !messages[messages.length - 1].isStreaming && (
    AI กำลังประมวลผลคำตอบ...
    )}
    ); }