// ─────────────────────────────────────────────────────────── // Huio Control Panel — Documents & Group Icons View (cp-documents-view.jsx) // ─────────────────────────────────────────────────────────── if (!window.CP_STYLES) throw new Error('[cp-documents-view] window.CP_STYLES not loaded (check cp-styles.jsx)'); function DocumentsViewComponent({ lang = 'vi', t: rawT, appStyles: rawAppStyles, }) { const appStyles = rawAppStyles || window.CP_STYLES || {}; const t = typeof rawT === 'function' ? rawT : (k) => k; // Active top-level subtab: 'gallery' (Documents) | 'icons' (Group Icons) const [activeTab, setActiveTab] = React.useState('gallery'); // Manifest and loading state const [manifest, setManifest] = React.useState(null); const [isLoading, setIsLoading] = React.useState(true); const [loadError, setLoadError] = React.useState(null); // Gallery Filters const [searchQuery, setSearchQuery] = React.useState(''); const [selectedCategory, setSelectedCategory] = React.useState('all'); const [selectedLang, setSelectedLang] = React.useState('all'); // 'all', 'vi', 'en' const [selectedFormat, setSelectedFormat] = React.useState('all'); // 'all', 'Dọc', 'Ngang' const [selectedSkin, setSelectedSkin] = React.useState('all'); // 'all', 'classic', 'cute_cat', etc. // Modal Viewer State const [modalSheet, setModalSheet] = React.useState(null); const [activePageIndex, setActivePageIndex] = React.useState(0); // Reset page index whenever modalSheet changes React.useEffect(() => { setActivePageIndex(0); }, [modalSheet?.id, modalSheet?.file]); // Icons Tab State const [iconSearch, setIconSearch] = React.useState(''); const [selectedIconCat, setSelectedIconCat] = React.useState('all'); const [selectedIconColor, setSelectedIconColor] = React.useState('#B85C38'); const [copiedToast, setCopiedToast] = React.useState(null); // Fetch Manifest on mount const loadManifest = React.useCallback(async () => { setIsLoading(true); setLoadError(null); try { const res = await fetch('./docs-gallery/manifest.json'); if (!res.ok) { throw new Error(`HTTP ${res.status}: Không thể tải manifest.json`); } const data = await res.json(); setManifest(data); } catch (err) { console.error('[cp-documents-view] Error loading manifest:', err); setLoadError(err.message || 'Lỗi khi tải dữ liệu thư viện tài liệu'); } finally { setIsLoading(false); } }, []); React.useEffect(() => { loadManifest(); }, [loadManifest]); // Keyboard navigation for Modal Viewer React.useEffect(() => { if (!modalSheet || !manifest?.sheets) return; const handleKeyDown = (e) => { if (e.key === 'Escape') { setModalSheet(null); } else if (e.key === 'ArrowLeft') { if (modalSheet.pages && modalSheet.pages.length > 1 && activePageIndex > 0) { setActivePageIndex((idx) => idx - 1); } else { navigateModal(-1); } } else if (e.key === 'ArrowRight') { if (modalSheet.pages && modalSheet.pages.length > 1 && activePageIndex < modalSheet.pages.length - 1) { setActivePageIndex((idx) => idx + 1); } else { navigateModal(1); } } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [modalSheet, manifest, activePageIndex, navigateModal]); // Filtered sheets const filteredSheets = React.useMemo(() => { if (!manifest?.sheets) return []; return manifest.sheets.filter((sheet) => { // Search filter if (searchQuery.trim()) { const q = searchQuery.toLowerCase().trim(); const matchTitleVi = (sheet.docTitleVi || '').toLowerCase().includes(q); const matchTitleEn = (sheet.docTitleEn || '').toLowerCase().includes(q); const matchFile = (sheet.file || '').toLowerCase().includes(q); const matchDocId = (sheet.docId || '').includes(q); const matchPrefix = (sheet.docPrefix || '').toLowerCase().includes(q); if (!matchTitleVi && !matchTitleEn && !matchFile && !matchDocId && !matchPrefix) { return false; } } // Category filter if (selectedCategory !== 'all' && sheet.docId !== selectedCategory) { return false; } // Lang filter if (selectedLang !== 'all' && sheet.lang !== selectedLang) { return false; } // Format filter if (selectedFormat !== 'all' && sheet.format !== selectedFormat) { return false; } // Skin filter if (selectedSkin !== 'all' && sheet.theme !== selectedSkin) { return false; } return true; }); }, [manifest, searchQuery, selectedCategory, selectedLang, selectedFormat, selectedSkin]); // Unique document generators count in current filtered view const uniqueDocsCount = React.useMemo(() => { const ids = new Set(filteredSheets.map((s) => s.docId || s.generator)); return ids.size; }, [filteredSheets]); // Navigate Modal Previous / Next const navigateModal = (delta) => { if (!modalSheet || !filteredSheets.length) return; const currentIndex = filteredSheets.findIndex((s) => s.file === modalSheet.file); if (currentIndex === -1) return; const nextIndex = currentIndex + delta; if (nextIndex >= 0 && nextIndex < filteredSheets.length) { setModalSheet(filteredSheets[nextIndex]); } }; // Find theme variants for currently selected document in modal const modalDocThemeVariants = React.useMemo(() => { if (!modalSheet || !manifest?.sheets) return []; return manifest.sheets.filter( (s) => s.docId === modalSheet.docId && s.format === modalSheet.format && s.lang === modalSheet.lang ); }, [modalSheet, manifest]); // Filtered Icons const filteredIcons = React.useMemo(() => { if (!manifest?.icons?.list) return []; return manifest.icons.list.filter((ic) => { if (selectedIconCat !== 'all' && ic.categoryId !== selectedIconCat) { return false; } if (iconSearch.trim()) { const q = iconSearch.toLowerCase().trim(); const matchName = ic.name.toLowerCase().includes(q); const matchVi = (ic.titleVi || '').toLowerCase().includes(q); const matchEn = (ic.titleEn || '').toLowerCase().includes(q); if (!matchName && !matchVi && !matchEn) return false; } return true; }); }, [manifest, selectedIconCat, iconSearch]); // Copy Icon URI helper const handleCopyIcon = (iconName) => { const uri = `icon:${iconName}:${selectedIconColor}`; if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(uri).catch(() => {}); } setCopiedToast(uri); setTimeout(() => { setCopiedToast((cur) => (cur === uri ? null : cur)); }, 2200); }; // Contrast helper for icon preview const getIconContrast = (bgHex) => { const norm = (bgHex || '').toUpperCase(); if (norm === '#C19433' || norm === '#A89684' || norm === '#F4EFE6' || norm === '#FFFFFF') { return '#1C1A15'; } return '#FFFFFF'; }; // Loading state if (isLoading) { return (
{lang === 'vi' ? 'Đang tải dữ liệu thư viện tài liệu R5...' : 'Loading R5 documents gallery...'}
{loadError}
{copiedToast}
{t('docs.subtitle') || (lang === 'vi' ? 'Xem toàn bộ 17 loại tài liệu Huio trên dữ liệu mẫu chuẩn R5, kiểm tra các giao diện (skin) và định dạng dọc/ngang.' : 'View all 17 Huio documents rendered with canonical R5 sample data, inspect theme skins and portrait/landscape formats.')}
{t('docs.empty_search') || (lang === 'vi' ? 'Không tìm thấy tài liệu nào phù hợp với bộ lọc.' : 'No documents found matching the active filters.')}
{lang === 'vi' ? (sheet.docDescVi || sheet.docTitleEn) : (sheet.docDescEn || sheet.docTitleVi)}
{ic.name}
{/* Friendly Title */}
{lang === 'vi' ? ic.titleVi : ic.titleEn}
{lang === 'vi' ? ic.categoryVi : ic.categoryEn}
{modalSheet.file}
MD5: {modalSheet.md5}