import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Line, Doughnut } from 'react-chartjs-2';
import { Inbox } from 'lucide-react';
import { AnalyticsApi } from '../api/client.js';
import { Skeleton } from './ui/skeleton.jsx';
import { EmptyState } from './ui/misc.jsx';
import {
Chart as ChartJS, CategoryScale, LinearScale, PointElement,
LineElement, ArcElement, Tooltip, Legend
} from 'chart.js';
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, ArcElement, Tooltip, Legend);
const fmtBytes = (b) => {
if (!b) return '0 B';
const u = ['B', 'KB', 'MB', 'GB']; let i = 0; let n = b;
while (n >= 1024 && i < u.length - 1) { n /= 1024; i++; }
return `${n.toFixed(1)} ${u[i]}`;
};
export function StatCard({ label, value, to }) {
const navigate = useNavigate();
return (
navigate(to) : undefined} title={to ? `View ${label}` : undefined}>
{value}
{label}
);
}
export function HealthWidget({ health }) {
if (!health) return ;
return (
Inbox Health
{health.score}/100
Grade {health.grade}
{health.recommendations.map((r, i) => - {r}
)}
);
}
export function TopSendersWidget({ senders }) {
const navigate = useNavigate();
if (!senders) return ;
return (
Top Senders
{senders.map((s) => (
navigate(`/app/search?q=${encodeURIComponent(`from:${s.address}`)}`)}
title={`Search emails from ${s.address}`}
>
| {s.displayName || s.address} |
{s.emailCount} |
))}
);
}
export function VolumeWidget({ volume }) {
if (!volume) return ;
const data = {
labels: volume.map((p) => p.day),
datasets: [{ label: 'Emails', data: volume.map((p) => p.count), borderColor: '#4f8cff', tension: 0.3 }]
};
return (
Email Volume
);
}
// Maps EmailCategory enum name → folder slug or search query
const CAT_SLUG = {
Finance: '/app/folder/finance',
Social: '/app/folder/social',
Notification: '/app/folder/automated',
Promotional: '/app/folder/shopping',
Shopping: '/app/folder/shopping',
Gaming: '/app/folder/gaming',
RideSharing: '/app/folder/ridesharing',
FoodDelivery: '/app/folder/food',
SeasonalSales: '/app/folder/sales',
Wellness: '/app/folder/wellness',
Travel: '/app/folder/travel',
Subscriptions: '/app/folder/subscriptions',
Parcels: '/app/folder/parcels',
Recruitment: '/app/folder/recruitment',
Events: '/app/folder/events',
SecurityAlerts: '/app/folder/security',
Healthcare: '/app/folder/healthcare',
Education: '/app/folder/education',
NewsMedia: '/app/folder/news',
PropertyUtilities:'/app/folder/property',
Charity: '/app/folder/charity',
Government: '/app/folder/government',
CryptoInvesting: '/app/folder/crypto',
FamilySchool: '/app/folder/family',
Spam: '/app/folder/spam',
Newsletter: '/app/search?q=newsletter',
Personal: '/app/search?q=is:unread',
Unknown: '/app/folder/allmail',
};
// "Emails by Category" — ranked horizontal bar list. Aggregates the
// category×day-of-week cells into per-category totals; each bar links to
// that category's folder via CAT_SLUG.
export function CategoryBreakdownWidget() {
const [cells, setCells] = useState(null);
const navigate = useNavigate();
useEffect(() => { AnalyticsApi.categoryHeatmap().then(setCells).catch(() => setCells([])); }, []);
if (!cells) {
return (
Emails by Category
{Array.from({ length: 6 }, (_, i) => )}
);
}
const totals = {};
cells.forEach((c) => { totals[c.category] = (totals[c.category] || 0) + c.count; });
const ranked = Object.entries(totals)
.map(([category, count]) => ({ category, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 8);
if (ranked.length === 0) {
return (
Emails by Category
);
}
const max = Math.max(1, ...ranked.map((r) => r.count));
return (
Emails by Category
{ranked.map(({ category, count }) => {
const dest = CAT_SLUG[category];
return (
);
})}
);
}
export function AttachmentsWidget({ attachments }) {
if (!attachments) return ;
const data = {
labels: attachments.map((a) => a.mimeBucket),
datasets: [{ data: attachments.map((a) => a.totalBytes), backgroundColor: ['#4f8cff', '#6fcf97', '#f2c94c', '#eb5757', '#bb6bd9', '#56ccf2', '#a0a0a0'] }]
};
return (
Attachments by Type
Total: {fmtBytes(attachments.reduce((s, a) => s + a.totalBytes, 0))}
);
}
export function StorageWidget({ bytes }) {
return ;
}
function Empty() {
return (
);
}