// File: frontend/components/admin/ai/PromptEditor.tsx // Change Log: // - 2026-06-14: Created PromptEditor component with live placeholder validation and save actions (conforming to task T018) import React, { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Input } from '@/components/ui/input'; import { AlertCircle, CheckCircle, Save, HelpCircle } from 'lucide-react'; import { PromptType } from '@/lib/types/ai-prompts'; import { PLACEHOLDER_REQUIREMENTS } from '@/contracts/frontend-types'; interface PromptEditorProps { promptType: PromptType; initialTemplate: string; onSave: (template: string, manualNote: string) => Promise; isSaving: boolean; } /** * คอมโพเนนต์เครื่องมือแก้ไขเทมเพลตพรอมต์ (Prompt Editor) * มีระบบตรวจเช็คตัวแปร/เพลสโฮลเดอร์ (Placeholder Validation) ในตัวแบบเรียลไทม์ */ export default function PromptEditor({ promptType, initialTemplate, onSave, isSaving, }: PromptEditorProps) { const [template, setTemplate] = useState(initialTemplate); const [manualNote, setManualNote] = useState(''); const [validationErrors, setValidationErrors] = useState([]); useEffect(() => { setTemplate(initialTemplate); setManualNote(''); }, [initialTemplate, promptType]); // ตรวจสอบตัวแปรที่ต้องมีในพรอมต์เทมเพลต (Real-time Validation) useEffect(() => { const requirements = PLACEHOLDER_REQUIREMENTS[promptType] || []; const missing = requirements.filter((req) => !template.includes(req)); setValidationErrors(missing); }, [template, promptType]); const handleSave = () => { if (validationErrors.length > 0) return; onSave(template, manualNote); }; const getFriendlyTypeName = (type: PromptType) => { switch (type) { case 'ocr_extraction': return 'สกัดข้อความ OCR (OCR Extraction)'; case 'rag_query_prompt': return 'ค้นหาข้อมูล RAG (RAG Query)'; case 'rag_prep_prompt': return 'เตรียมข้อมูล RAG (RAG Prep)'; case 'classification_prompt': return 'จำแนกประเภทเอกสาร (Classification)'; } }; return ( แก้ไขพรอมต์เทมเพลต ({getFriendlyTypeName(promptType)})
โครงสร้างเทมเพลต (Template Body) 4000 ? 'text-destructive font-semibold' : 'text-muted-foreground'}> {template.length} / 4000 อักขระ