import { useMemo } from 'react'; import type { WawpDashboardData, Notice } from '../types'; export interface ExtendedNotice extends Notice { id: string; title: string; description: string; icon: string; iconUrl?: string; buttonText: string; buttonColor?: string; badge?: string; badgeColor?: string; color?: string; } interface Instance { status?: string; } interface SmtpAccount { is_active: boolean | number; validated: boolean | number; } interface SendersData { enabled: Record; } interface CronEvent { status: string; } interface DbTable { missing?: string | boolean; status?: string; } interface SystemInfoData { cron?: { enabled: boolean; events: CronEvent[]; }; database?: DbTable[]; } export const useNotices = (data: WawpDashboardData) => { const { notices: backendNotices = [], i18n, global, sidebarData, instancesData, firebaseSenderData, smtpSenderData } = data; const isEnabled = (val: unknown) => { if (val && typeof val === 'object' && 'enabled' in val) { const obj = val as { enabled: string | number | boolean }; return Number(obj.enabled) === 1 || obj.enabled === true; } return Number(val) === 1 || val === true; }; return useMemo(() => { const senderData = data['sendersData'] as SendersData | undefined; const enabledSenders = sidebarData?.enabledSenders || senderData?.enabled || { email: 1, wa: 1, meta: 1, firebase: 1, block: 1 }; const { wooStatus = { active: false, installed: false }, urls = {} } = data; const iconBase = `${global?.pluginUrl || ''}assets/icons/`; const senderImgBase = `${global?.pluginUrl || ''}assets/img/senders/`; const notices: ExtendedNotice[] = []; // 0. WooCommerce Notice (Global) if (!wooStatus.active) { notices.push({ id: 'woo-missing', type: 'offline', url: (wooStatus.installed ? urls.wooActivate : urls.wooInstall) || '#', title: 'WooCommerce Required', description: (i18n.wooMissingNotice as string) || 'To enable Order Notifications and Checkout Verification, WooCommerce must be installed and active.', icon: 'ri-shopping-cart-2-fill', iconUrl: `${iconBase}woocommerce.svg`, buttonText: (wooStatus.installed ? (i18n.wooActivate as string || 'Activate WooCommerce') : (i18n.wooInstall as string || 'Install WooCommerce')), badge: 'Required', badgeColor: '#ef4444', color: '#7f54b3' }); } // 1. WhatsApp Notice const isWaEnabled = isEnabled(enabledSenders.wa); const isWaSection = global.section === 'whatsapp-web-sender'; const currentInstances = (instancesData && Array.isArray(instancesData.instances)) ? instancesData.instances as Instance[] : (Array.isArray(data.instances) ? data.instances as Instance[] : (Array.isArray(data.data) ? data.data as Instance[] : [])); const hasOnlineInstance = currentInstances.some((i: Instance) => { const status = (i.status || '').toLowerCase(); return ['online', 'working', 'ready'].includes(status); }); // Hide if at least one is online if ((isWaEnabled || isWaSection) && !hasOnlineInstance) { notices.push({ id: 'wa-offline', type: 'offline', url: 'admin.php?page=wawp&wawp_section=whatsapp-web-sender', title: 'WhatsApp is Disconnected', description: 'WhatsApp is enabled but offline. Connect your number to start using WhatsApp features.', icon: 'ri-error-warning-fill', iconUrl: `${iconBase}whatsapp.svg`, buttonText: 'Connect Number', badge: 'Offline', badgeColor: '#ef4444', color: '#25d366' }); } // 2. Firebase Notice const isFirebaseEnabled = isEnabled(enabledSenders.firebase); const firebaseAccounts = firebaseSenderData?.allAccounts || []; const hasActiveFirebase = firebaseAccounts.some(acc => Number(acc.is_active) === 1 && Number(acc.validated) === 1); // Hide if at least one active and validated account exists if (isFirebaseEnabled && !hasActiveFirebase) { notices.push({ id: 'firebase-pending', type: 'offline', url: 'admin.php?page=wawp&wawp_section=firebase-otp', title: 'Firebase SMS Pending Setup', description: 'Firebase SMS is enabled but not configured. Complete setup to start sending SMS.', icon: 'ri-fire-fill', iconUrl: `${senderImgBase}firebase.svg`, buttonText: 'Configure Firebase', badge: 'Setup Required', badgeColor: '#ef4444', color: '#f59e0b' }); } // 3. SMTP Notice const isSmtpEnabled = isEnabled(enabledSenders.email); const smtpAccounts = (smtpSenderData as unknown as { allAccounts?: SmtpAccount[] })?.allAccounts || []; const hasActiveSmtp = smtpAccounts.some((acc: SmtpAccount) => (Number(acc.is_active) === 1 || acc.is_active === true) && (Number(acc.validated) === 1 || acc.validated === true) ); // Hide if at least one active and validated account exists if (isSmtpEnabled && !hasActiveSmtp) { notices.push({ id: 'smtp-pending', type: 'offline', url: 'admin.php?page=wawp&wawp_section=smtp-sender', title: 'SMTP Server Pending Setup', description: 'SMTP is enabled but no active mail server is configured. Set up SMTP to send emails.', icon: 'ri-mail-send-fill', iconUrl: `${senderImgBase}gmail.svg`, buttonText: 'Configure SMTP', badge: 'Setup Required', badgeColor: '#ef4444', color: '#3b82f6' }); } // 4. Update Notices (from Backend) backendNotices.forEach((n, idx) => { if (n.type === 'update') { notices.push({ id: `update-${idx}`, ...n, title: (i18n.ready || '').replace('%s', n.version || ''), description: (i18n.updatePrompt || '').replace('%s', global.version || ''), icon: 'ri-rocket-2-fill', iconUrl: `${iconBase}wordpress.svg`, buttonText: i18n.updateNow || 'Update Now', badge: 'Update' }); } }); // 5. System Info Notices const sysInfo = data.systemInfo as SystemInfoData | undefined; if (sysInfo) { // WP Cron Check const cronEvents = Array.isArray(sysInfo.cron?.events) ? sysInfo.cron.events : []; const hasCronIssues = sysInfo.cron?.enabled === false || cronEvents.some((e: CronEvent) => e.status?.toLowerCase().includes('not scheduled')); if (hasCronIssues) { notices.push({ id: 'system-cron-issue', type: 'offline', url: 'admin.php?page=wawp&wawp_section=system_info', title: 'Automated Tasks Need Attention', description: 'A problem was detected with the scheduled tasks (Cron) system. Please visit the System Infrastructure page and use the "Auto Fix" button to ensure automation works correctly.', icon: 'ri-time-line', iconUrl: `${iconBase}wawp.svg`, buttonText: 'View Details & Repair', badge: 'Issue Found', badgeColor: '#ef4444', color: '#004449' }); } // Database Tables Check const dbTables = Array.isArray(sysInfo.database) ? sysInfo.database : []; const hasDbIssues = dbTables.some((t: DbTable) => !!t.missing || t.status?.toLowerCase().includes('error')); if (hasDbIssues) { notices.push({ id: 'system-db-issue', type: 'offline', url: 'admin.php?page=wawp&wawp_section=system_info', title: 'Database Tables Need Attention', description: 'Missing or corrupted database tables were detected. Please visit the System Infrastructure page and use the "Auto Fix" button to avoid data loss or feature failure.', icon: 'ri-database-2-line', iconUrl: `${iconBase}wawp.svg`, buttonText: 'View Details & Repair', badge: 'Issue Found', badgeColor: '#ef4444', color: '#004449' }); } } // 6. HPOS Recommendation Notice const hpos = data.hposStatus; if (hpos && !hpos.dismissed && (!hpos.hposEnabled || !hpos.compatModeEnabled)) { notices.push({ id: 'hpos-recommendation', type: 'offline', url: 'admin.php?page=wc-settings&tab=advanced§ion=features', title: 'Performance Recommendation', description: 'Wawp recommends enabling "High-performance order storage" and "Enable compatibility mode" in WooCommerce for better performance.', icon: 'ri-flashlight-fill', iconUrl: `${iconBase}wawp.svg`, buttonText: 'Enable these options', badge: 'Recommended', badgeColor: '#f59e0b', color: '#004449' }); } return notices; }, [data, global, i18n, instancesData, firebaseSenderData, smtpSenderData, backendNotices, sidebarData]); };;