import React, { useCallback, useEffect, useState } from 'react'; import { apiAppTemplates, type AppTemplateItem } from '../api'; import { countKeywords } from '../utils/appKeywords'; interface AppTemplatePickerModalProps { open: boolean; onClose: () => void; onSelect: (keywords: string) => void; allowedApps: string; } export const AppTemplatePickerModal: React.FC = ({ open, onClose, onSelect, allowedApps, }) => { const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [search, setSearch] = useState(''); const load = useCallback(async () => { try { setLoading(true); setError(null); const res = await apiAppTemplates.list(); setItems(res.data || []); } catch (e: any) { setItems([]); setError(e?.message || 'Không tải được khung ứng dụng'); } finally { setLoading(false); } }, []); useEffect(() => { if (!open) return; load(); }, [open, load]); useEffect(() => { if (!open) { setSearch(''); setError(null); } }, [open]); if (!open) return null; const q = search.trim().toLowerCase(); const filtered = q ? items.filter( (t) => t.name.toLowerCase().includes(q) || t.description?.toLowerCase().includes(q) || t.keywords.toLowerCase().includes(q), ) : items; return (
e.stopPropagation()}>

Chọn khung ứng dụng

Ghép bộ keyword đã lưu vào danh sách ứng dụng được phép. Quản lý khung tại Hệ thống → Khung ứng dụng.

setSearch(e.target.value)} autoFocus />
{error ? (
{error}
) : loading && items.length === 0 ? (
Đang tải...
) : filtered.length === 0 ? (
{q ? `Không tìm thấy "${search}"` : 'Chưa có khung nào. Tạo tại Hệ thống → Khung ứng dụng.'}
) : (
{filtered.map((tpl) => (
{tpl.name} {countKeywords(tpl.keywords)} keyword
{tpl.description &&

{tpl.description}

} {tpl.keywords}
))}
)}
Đang có {countKeywords(allowedApps)} keyword trong whitelist hiện tại
); };