import { Link, Outlet, useLocation, useNavigate } from 'react-router-dom'; import { useEffect, useState, useCallback } from 'react'; import { AuthApi, SyncApi, AnalyticsApi } from '../api/client.js'; import Logo from './Logo.jsx'; import DevBanner from './DevBanner.jsx'; // ── Folder definitions ──────────────────────────────────────────────────────── const MAILBOX = [ { slug: 'inbox', icon: '📥', label: 'Inbox', countKey: 'inbox' }, { slug: 'allmail', icon: '📬', label: 'All Mail', countKey: 'allMail' }, { slug: 'starred', icon: '⭐', label: 'Starred', countKey: 'starred' }, { slug: 'sent', icon: '📤', label: 'Sent', countKey: 'sent' }, { slug: 'drafts', icon: '✏️', label: 'Drafts', countKey: 'drafts' }, { slug: 'archive', icon: '📦', label: 'Archive', countKey: null }, { slug: 'spam', icon: '🚫', label: 'Spam', countKey: 'spam' }, { slug: 'trash', icon: '🗑️', label: 'Trash', countKey: 'trash' }, { slug: 'unlabeled', icon: '🏷️', label: 'Unlabeled', countKey: null }, { slug: 'pinned', icon: '📌', label: 'Pinned', countKey: null }, { slug: 'readlater', icon: '🔖', label: 'Read Later', countKey: null }, ]; // Special items always available as favorites (not smart folders) const FAV_SPECIALS = { inbox: { slug: 'inbox', icon: '📥', label: 'Inbox', countKey: 'inbox', type: 'mailbox' }, unread: { slug: 'unread', icon: '🔵', label: 'Unread Mail', countKey: 'unread', type: 'filter' }, large: { slug: 'large', icon: '📎', label: 'Large Mail', countKey: 'large', type: 'filter' }, old: { slug: 'old', icon: '🕰️', label: 'Old Mail', countKey: 'old', type: 'filter' }, readlater: { slug: 'readlater', icon: '🔖', label: 'Read Later', countKey: null, type: 'filter' }, }; const DEFAULT_FAV_SLUGS = ['inbox', 'unread', 'large', 'old', 'readlater']; const SMART_FOLDERS = [ { slug: 'automated', icon: '🤖', label: 'Automated', countKey: 'automated' }, { slug: 'noreply', icon: '🔇', label: 'No-Reply', countKey: 'noreply' }, { slug: 'shopping', icon: '🛍️', label: 'Online Shopping', countKey: 'shopping' }, { slug: 'gaming', icon: '🎮', label: 'Gaming', countKey: 'gaming' }, { slug: 'finance', icon: '💳', label: 'Finance & Insurance', countKey: 'finance' }, { slug: 'sales', icon: '🏷️', label: 'Seasonal Sales', countKey: 'sales' }, { slug: 'ridesharing', icon: '🚗', label: 'Ride Sharing', countKey: 'ridesharing' }, { slug: 'food', icon: '🍕', label: 'Food Delivery', countKey: 'food' }, { slug: 'social', icon: '📱', label: 'Social Notifications',countKey: 'social' }, { slug: 'wellness', icon: '🏃', label: 'Wellness & Sport', countKey: 'wellness' }, ]; // ── Helpers ─────────────────────────────────────────────────────────────────── const fmtCount = (n) => { if (!n) return null; return n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n); }; const LS_FAV = 'ii:fav-slugs'; const LS_OPEN = 'ii:sidebar-open'; const LS_SECTS = 'ii:sidebar-sections'; const loadFavs = () => { try { return JSON.parse(localStorage.getItem(LS_FAV)) ?? DEFAULT_FAV_SLUGS; } catch { return DEFAULT_FAV_SLUGS; } }; const saveFavs = (v) => localStorage.setItem(LS_FAV, JSON.stringify(v)); const loadSections = () => { try { return JSON.parse(localStorage.getItem(LS_SECTS)) ?? { fav: true, mailbox: true, smart: true }; } catch { return { fav: true, mailbox: true, smart: true }; } }; const saveSections = (v) => localStorage.setItem(LS_SECTS, JSON.stringify(v)); // ── Sub-components ──────────────────────────────────────────────────────────── function SectionHead({ label, open, onToggle, collapsed }) { return ( ); } function FolderLink({ item, active, count, collapsed, extra }) { return ( {item.icon} {!collapsed && ( <> {item.label} {count && {count}} {extra} > )} ); } // ── Main Layout ─────────────────────────────────────────────────────────────── export default function Layout() { const [user, setUser] = useState(null); const [sidebarOpen, setSidebarOpen] = useState(() => { try { return localStorage.getItem(LS_OPEN) !== 'false'; } catch { return true; } }); const [sections, setSections] = useState(loadSections); const [favSlugs, setFavSlugs] = useState(loadFavs); const [counts, setCounts] = useState(null); const loc = useLocation(); const navigate = useNavigate(); useEffect(() => { AuthApi.me().then(setUser).catch(() => {}); }, []); useEffect(() => { AnalyticsApi.sidebarCounts().then(setCounts).catch(() => {}); }, []); const toggleSidebar = () => { setSidebarOpen((o) => { localStorage.setItem(LS_OPEN, String(!o)); return !o; }); }; const toggleSection = (key) => { setSections((s) => { const next = { ...s, [key]: !s[key] }; saveSections(next); return next; }); }; const toggleFav = useCallback((slug) => { setFavSlugs((prev) => { const next = prev.includes(slug) ? prev.filter((s) => s !== slug) : [...prev, slug]; saveFavs(next); return next; }); }, []); const nav = [ { to: '/app', label: 'Dashboard', end: true }, { to: '/app/senders', label: 'Senders' }, { to: '/app/cleanup', label: 'Cleanup' }, { to: '/app/unsubscribe', label: 'Unsubscribe' } ]; const isNavActive = (n) => (n.end ? loc.pathname === n.to : loc.pathname.startsWith(n.to)); const activeSlug = loc.pathname.startsWith('/app/folder/') ? loc.pathname.split('/app/folder/')[1] : null; const logout = async () => { try { await AuthApi.logout(); } catch { /* ignore */ } navigate('/'); }; const startSync = async () => { try { await SyncApi.incremental(); } catch { /* ignore */ } if (loc.pathname !== '/app') navigate('/app'); window.dispatchEvent(new Event('inboxintel:sync-started')); }; // Count getters const mailboxCount = (item) => fmtCount(item.countKey ? counts?.[item.countKey] : null); const smartCount = (item) => fmtCount(counts?.smartFolders?.[item.slug]); // Build favorites list: specials first (in default order), then pinned smart folders const favItems = favSlugs.map((slug) => { if (FAV_SPECIALS[slug]) return { ...FAV_SPECIALS[slug], source: 'special' }; const sf = SMART_FOLDERS.find((f) => f.slug === slug); return sf ? { ...sf, source: 'smart' } : null; }).filter(Boolean); const favCount = (item) => { if (item.source === 'smart') return smartCount(item); return fmtCount(item.countKey ? counts?.[item.countKey] : null); }; // Smart folders sorted by count desc const sortedSmart = [...SMART_FOLDERS].sort((a, b) => { const ca = counts?.smartFolders?.[a.slug] ?? 0; const cb = counts?.smartFolders?.[b.slug] ?? 0; return cb - ca; }); const isPinnedSmart = (slug) => favSlugs.includes(slug); return (