// ─────────────────────────────────────────────────────────── // Huio Control Panel — Live Support View (cp-support-view.jsx) // ─────────────────────────────────────────────────────────── if (!window.CP_STYLES) throw new Error('[cp-support-view] window.CP_STYLES not loaded'); function renderSupport({ lang, t, appStyles: rawAppStyles, supportQueue = [], supportLoading, supportFilter, setSupportFilter, supportSearchQuery, setSupportSearchQuery, selectedSupportConv, setSelectedSupportConv, fetchSupportQueue, supabaseClient, }) { const appStyles = rawAppStyles || window.CP_STYLES || {}; // Safe data array & pre-normalized filter criteria (O(1) loop invariant hoisting) const queueList = Array.isArray(supportQueue) ? supportQueue : []; const q = (typeof supportSearchQuery === 'string') ? supportSearchQuery.trim().toLowerCase() : ''; const totalCount = queueList.length; // Single-pass computation of metrics (0 intermediate array allocations) let waitingCount = 0; for (let i = 0; i < totalCount; i++) { if (queueList[i]?.is_waiting_reply) waitingCount++; } // Filters with defensive null/type safety on string properties const filteredQueue = queueList.filter(conv => { if (!conv) return false; if (!q) return true; const name = (conv.user_name || '').toLowerCase(); const phone = String(conv.user_phone || ''); const msg = (conv.last_message_text || '').toLowerCase(); return name.includes(q) || phone.includes(q) || msg.includes(q); }); return (

{t('support.title') || 'Support Queue'}

{t('support.sub') || 'Manage all live support conversations with users.'}

{/* Quick Metrics Bar */}
⏳
{t('support.stats.waiting') || 'Waiting for reply'}
0 ? 'var(--hu-rust)' : 'var(--hu-fg)' }}> {waitingCount}
💬
{t('support.stats.total') || 'Total conversations'}
{totalCount}
{/* Controls Bar */}
{/* Filter Buttons */}
{/* Search query */}
setSupportSearchQuery(e.target.value)} placeholder={t('common.search')} className="hu-input" style={{ height: '100%', fontSize: '13px' }} />
{/* Support Queue List Table */}
{supportLoading && queueList.length === 0 ? ( ) : filteredQueue.length === 0 ? ( ) : ( filteredQueue.map((conv, index) => { const rawDate = conv.last_message_at || conv.created_at; const dateObj = rawDate ? new Date(rawDate) : null; const isInvalidDate = !dateObj || isNaN(dateObj.getTime()); const timeStr = isInvalidDate ? '—' : dateObj.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); const dateStr = isInvalidDate ? '—' : dateObj.toLocaleDateString(lang === 'vi' ? 'vi-VN' : 'en-US', { month: 'short', day: 'numeric' }); const unreadNum = Number(conv.unread_count || 0); const rowKey = conv.conversation_id || `conv_${conv.user_id || index}_${index}`; const displayName = conv.user_name || t('support.anonymous') || 'Anonymous'; const avatarInitial = String(displayName).charAt(0).toUpperCase() || 'U'; return ( setSelectedSupportConv(conv)} onMouseEnter={(e) => { e.currentTarget.style.background = conv.is_waiting_reply ? 'rgba(217, 83, 79, 0.08)' : 'var(--hu-bg-elev)'; }} onMouseLeave={(e) => { e.currentTarget.style.background = conv.is_waiting_reply ? 'rgba(217, 83, 79, 0.03)' : 'inherit'; }} > ); }) )}
{t('support.col.user') || 'User'} {t('support.col.last_message') || 'Last message'} {t('support.col.time') || 'Time'} {t('support.col.status') || 'Status'} {lang === 'vi' ? 'Hành động' : 'Actions'}
⏳
{lang === 'vi' ? 'Đang tải hàng đợi hỗ trợ...' : 'Loading support queue...'}
🎉
{supportFilter === 'waiting' ? (lang === 'vi' ? 'Không có yêu cầu nào đang chờ phản hồi!' : 'All support requests have been answered!') : (t('support.empty') || 'No support conversations found')}
{lang === 'vi' ? 'Hệ thống hỗ trợ đang hoạt động bình thường.' : 'All incoming messages are handled.'}
{conv.user_avatar ? ( {displayName} ) : ( avatarInitial )}
{displayName} {unreadNum > 0 && ( {unreadNum} )}
{conv.user_phone ? conv.user_phone : 'No phone'}
{conv.last_message_text || ( {lang === 'vi' ? 'Chưa có tin nhắn' : 'No messages'} )}
{timeStr}
{dateStr}
{conv.is_waiting_reply ? ( 🔴 {t('support.status.waiting') || 'Waiting'} ) : ( ✓ {t('support.status.replied') || 'Replied'} )}
{/* Support Chat Modal */} setSelectedSupportConv(null)} title={lang === 'vi' ? 'Hỗ trợ khách hàng' : 'Live Support'} userName={selectedSupportConv?.user_name} userPhone={selectedSupportConv?.user_phone} waitingBadge={selectedSupportConv?.is_waiting_reply} > {selectedSupportConv && ( fetchSupportQueue(supportFilter)} /> )}
); } window.CpSupportView = { renderSupport, };