// ─────────────────────────────────────────────────────────── // Huio Control Panel — Push Broadcast View (cp-broadcast-view.jsx) // Bilingual broadcast support: Vietnamese (required) & English (optional with fallback) // ─────────────────────────────────────────────────────────── if (!window.CP_STYLES) throw new Error('[cp-broadcast-view] window.CP_STYLES not loaded'); function renderBroadcast({ lang, t, appStyles: rawAppStyles, users = [], logs = [], broadcastTarget, setBroadcastTarget, broadcastUserId, setBroadcastUserId, broadcastTitle, setBroadcastTitle, broadcastBody, setBroadcastBody, broadcastTitleEn = '', setBroadcastTitleEn = () => {}, broadcastBodyEn = '', setBroadcastBodyEn = () => {}, isBroadcasting, handleSendBroadcast, }) { const appStyles = rawAppStyles || window.CP_STYLES || {}; const formatPhone = window.formatPhone || ((p) => p || '—'); const userList = Array.isArray(users) ? users : []; const logList = Array.isArray(logs) ? logs : []; const totalUsersCount = userList.length; // Single-pass telemetry & cohort aggregation with language breakdown let usersWithPushTokenCount = 0; let targetCohort = []; let targetCohortWithPush = 0; let targetCohortViCount = 0; let targetCohortEnCount = 0; for (let i = 0; i < totalUsersCount; i++) { const u = userList[i]; if (!u) continue; if (u.pushToken) { usersWithPushTokenCount++; } let isTarget = false; if (broadcastTarget === 'all') { isTarget = true; } else if (broadcastTarget === 'organizers') { isTarget = (u.role === 'organizer' || u.role === 'both'); } else if (broadcastTarget === 'participants') { isTarget = (u.role === 'participant' || u.role === 'both'); } else if (broadcastTarget === 'subscribers_paid') { isTarget = (u.plan === 'organizer_pro' || u.plan === 'organizer_business') && (u.subscriptionSource === 'payment' || u.hasConfirmedPayment); } else if (broadcastTarget === 'subscribers_all') { isTarget = (u.plan === 'organizer_pro' || u.plan === 'organizer_business'); } else if (broadcastTarget === 'subscribers_pro') { isTarget = (u.plan === 'organizer_pro'); } else if (broadcastTarget === 'subscribers_business') { isTarget = (u.plan === 'organizer_business'); } else if (broadcastTarget === 'user') { isTarget = (u.id === broadcastUserId); } if (isTarget) { targetCohort.push(u); if (u.pushToken) { targetCohortWithPush++; } const uLang = (u.language || 'vi').toLowerCase(); if (uLang === 'en') { targetCohortEnCount++; } else { targetCohortViCount++; } } } const pushReachPercentage = totalUsersCount > 0 ? Math.round((usersWithPushTokenCount / totalUsersCount) * 100) : 0; const targetCohortViPct = targetCohort.length > 0 ? Math.round((targetCohortViCount / targetCohort.length) * 100) : 0; const targetCohortEnPct = targetCohort.length > 0 ? Math.round((targetCohortEnCount / targetCohort.length) * 100) : 0; // Live preview values const previewViTitle = broadcastTitle.trim() || 'Huio'; const previewViBody = broadcastBody.trim() || (lang === 'vi' ? 'Nội dung thông báo tiếng Việt...' : 'Vietnamese message body...'); const isEnBodyCustom = Boolean(broadcastBodyEn.trim()); const isEnTitleCustom = Boolean(broadcastTitleEn.trim()); const previewEnTitle = isEnTitleCustom ? broadcastTitleEn.trim() : previewViTitle; const previewEnBody = isEnBodyCustom ? broadcastBodyEn.trim() : (broadcastBody.trim() || (lang === 'vi' ? 'Nội dung thông báo tiếng Anh...' : 'English message body...')); // Bounded log scanning for recent broadcast activities const broadcastLogs = []; for (let i = logList.length - 1; i >= 0 && broadcastLogs.length < 50; i--) { const l = logList[i]; const txt = typeof l?.text === 'string' ? l.text : ''; if (txt && (txt.includes('Broadcasted') || txt.includes('Đã phát') || txt.includes('[DEMO]'))) { broadcastLogs.push(l); } } return (
{t('broadcast.sub')}
{t('broadcast.preview.sub')}
{lang === 'vi' ? 'Chưa có thông báo nào được gửi trong phiên này.' : 'No broadcasts recorded in this session.'}
) : ({l.text}
{l.details && (