import React, { useMemo } from 'react'; import { License } from '../types'; import { BellIcon, ExclamationIcon, CalendarIcon } from './Icons'; interface DashboardAlertsProps { licenses: License[]; alertDays: number; } const DashboardAlerts: React.FC = ({ licenses = [], alertDays = 45 }) => { const alerts = useMemo(() => { if (!Array.isArray(licenses)) return []; const today = new Date(); today.setHours(0, 0, 0, 0); const futureThreshold = new Date(today); futureThreshold.setDate(today.getDate() + alertDays); const upcomingEvents: Array<{ id: string; licenseName: string; date: Date; type: 'Renewal' | 'Expiration'; daysRemaining: number; }> = []; licenses.forEach(lic => { if (!lic) return; // Check Contract End Date if (lic.endDate) { const endDate = new Date(lic.endDate); if (!isNaN(endDate.getTime())) { endDate.setHours(0, 0, 0, 0); if (endDate >= today && endDate <= futureThreshold) { const diffTime = endDate.getTime() - today.getTime(); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); upcomingEvents.push({ id: lic.id, licenseName: lic.licenseName, date: endDate, type: 'Expiration', daysRemaining: diffDays }); } } } // Check Renewals if (Array.isArray(lic.renewals)) { lic.renewals.forEach(r => { if (!r || !r.renewalDate) return; const renewalDate = new Date(r.renewalDate); if (!isNaN(renewalDate.getTime())) { renewalDate.setHours(0, 0, 0, 0); if (renewalDate >= today && renewalDate <= futureThreshold) { const diffTime = renewalDate.getTime() - today.getTime(); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); upcomingEvents.push({ id: lic.id, licenseName: lic.licenseName, date: renewalDate, type: 'Renewal', daysRemaining: diffDays }); } } }); } }); return upcomingEvents.sort((a, b) => a.date.getTime() - b.date.getTime()).slice(0, 5); }, [licenses, alertDays]); if (alerts.length === 0) return null; return (

Upcoming Expirations & Renewals

Next {alertDays} Days
{alerts.map((alert, idx) => (
{alert.type === 'Expiration' ? : }
{alert.daysRemaining === 0 ? 'Today' : `${alert.daysRemaining} days`}

{alert.licenseName}

{alert.type}

{alert.date.toLocaleDateString()}

))}
); }; export default DashboardAlerts;