'use client'; import { useEffect, useState } from 'react'; interface LicenseData { version: string; latest: string; update_available: boolean; changelog: string; announcement: { id: number; title: string; content: string } | null; } export default function HubNotice() { const [data, setData] = useState(null); const [visible, setVisible] = useState(false); const [mode, setMode] = useState<'ann' | 'upd'>('ann'); useEffect(() => { const openHandler = () => { // 铃铛手动打开:有授权公告就显示公告,无公告则显示版本信息兜底 fetch('/api/license-config', { cache: 'no-store' }) .then((r) => r.json()) .then((j) => { if (!j || j.code !== 200 || !j.data) return; const d = j.data as LicenseData; setData(d); if (d.announcement) { setMode('ann'); setVisible(true); } else if (Array.isArray((d as any).announcements) && (d as any).announcements.length) { const first = (d as any).announcements[0]; setData({ ...d, announcement: first }); setMode('ann'); setVisible(true); } else { setMode('upd'); setVisible(true); } }) .catch(() => {}); }; window.addEventListener('open-hub-notice', openHandler); const t = setTimeout(() => { fetch('/api/license-config', { cache: 'no-store' }) .then((r) => r.json()) .then((j) => { if (!j || j.code !== 200 || !j.data) return; const d = j.data as LicenseData; setData(d); let ann = null; try { const seen = JSON.parse(localStorage.getItem('hub_seen') || '{}'); if (d.announcement && !seen['a' + d.announcement.id]) ann = d.announcement; } catch {} let upd = null; try { upd = d.update_available && localStorage.getItem('hub_ver_dismiss') !== String(d.latest || '') ? d : null; } catch {} if (!ann && !upd) return; setMode(ann ? 'ann' : 'upd'); setVisible(true); }) .catch(() => {}); }, 1500); return () => { clearTimeout(t); window.removeEventListener('open-hub-notice', openHandler); }; }, []); if (!visible || !data) return null; const title = mode === 'ann' && data.announcement ? String(data.announcement.title || '📢 公告') : data.latest ? '发现新版本 v' + String(data.latest || '') : '米视TV v' + String(data.version || ''); const body = mode === 'ann' && data.announcement ? String(data.announcement.content || '') : String(data.changelog || '当前已是最新版本,暂无授权中心公告。').slice(0, 600); const close = (dismissVer = false) => { try { if (mode === 'ann' && data.announcement) { const s = JSON.parse(localStorage.getItem('hub_seen') || '{}'); s['a' + data.announcement.id] = 1; localStorage.setItem('hub_seen', JSON.stringify(s)); } if (dismissVer && mode === 'upd') { localStorage.setItem('hub_ver_dismiss', String(data.latest || '')); } } catch {} setVisible(false); }; return (

{title}

{body}
当前版本 v{String(data.version || '')}
); }