// File: frontend/components/admin/ai/ContextConfigEditor.tsx // Change Log: // - 2026-06-14: Created ContextConfigEditor component with project/contract loaders and selectors (conforming to task T028) import React, { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { CheckCircle2, Settings } from 'lucide-react'; import { ContextConfig } from '@/lib/types/ai-prompts'; import { projectService } from '@/lib/services/project.service'; import { contractService } from '@/lib/services/contract.service'; interface ContextConfigEditorProps { initialConfig: ContextConfig | null; onSave: (config: ContextConfig) => Promise; isSaving: boolean; } interface ProjectOption { publicId: string; projectName: string; } interface ContractOption { publicId: string; contractName: string; project?: { publicId?: string; }; } /** * คอมโพเนนต์ฟอร์มสำหรับแก้ไขบริบทข้อมูล (Context Configuration) * จัดการตัวเลือกการกรองข้อมูลรายโครงการ (Project Filter) และรายสัญญา (Contract Filter) รวมทั้งภาษาและจำนวนประวัติการดึงข้อมูล */ export default function ContextConfigEditor({ initialConfig, onSave, isSaving, }: ContextConfigEditorProps) { const [projects, setProjects] = useState([]); const [contracts, setContracts] = useState([]); const [filteredContracts, setFilteredContracts] = useState([]); // State ฟอร์ม const [projectId, setProjectId] = useState('all'); const [contractId, setContractId] = useState('all'); const [pageSize, setPageSize] = useState(3); const [language, setLanguage] = useState('th'); const [outputLanguage, setOutputLanguage] = useState('th'); useEffect(() => { const loadData = async () => { try { const projList = await projectService.getAll(); setProjects( Array.isArray(projList) ? (projList as unknown as Record[]).map((p) => ({ publicId: String(p.publicId || ''), projectName: String(p.projectName || ''), })) : [] ); const contrList = await contractService.getAll(); setContracts( Array.isArray(contrList) ? (contrList as unknown as Record[]).map((c) => ({ publicId: String(c.publicId || ''), contractName: String(c.contractName || ''), project: c.project ? { publicId: String((c.project as unknown as Record).publicId || ''), } : undefined, })) : [] ); } catch (_err) { // Error handling silently - backend logs via NestJS Logger } }; loadData(); }, []); // พรีโหลดค่าตั้งต้น useEffect(() => { if (initialConfig) { setProjectId(initialConfig.filter?.projectId || 'all'); setContractId(initialConfig.filter?.contractId || 'all'); setPageSize(initialConfig.pageSize || 3); setLanguage(initialConfig.language || 'th'); setOutputLanguage(initialConfig.outputLanguage || 'th'); } else { setProjectId('all'); setContractId('all'); setPageSize(3); setLanguage('th'); setOutputLanguage('th'); } }, [initialConfig]); // กรองรายการสัญญาตามโครงการที่เลือก useEffect(() => { if (projectId && projectId !== 'all') { const filtered = contracts.filter((c) => c.project?.publicId === projectId); setFilteredContracts(filtered); // รีเซ็ตสัญญาถ้าไม่ได้ผูกกับโครงการที่เลือก const isStillValid = filtered.some((c) => c.publicId === contractId); if (!isStillValid && contractId !== 'all') { setContractId('all'); } } else { setFilteredContracts(contracts); } }, [projectId, contracts, contractId]); const handleSave = () => { const config: ContextConfig = { filter: { projectId: projectId === 'all' ? null : projectId, contractId: contractId === 'all' ? null : contractId, }, pageSize: Number(pageSize), language, outputLanguage, }; onSave(config); }; return ( การตั้งค่าบริบทข้อมูล (Context Configuration) {/* เลือกล็อคโครงการ */}
{/* เลือกล็อคสัญญา */}
{/* ปริมาณเอกสารอ้างอิงและภาษา */}
setPageSize(Math.max(1, Number(e.target.value)))} className="bg-background/50 border-border/50 text-sm focus-visible:ring-primary/30" />
การตั้งค่านี้จะผูกกับเวอร์ชันของพรอมต์โดยตรง
); }