import React, { useCallback, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { Box, Button, Checkbox, Chip, LinearProgress, Menu, MenuItem, Paper, Stack, Typography, } from "@mui/material"; import useMediaQuery from "@mui/material/useMediaQuery"; import { alpha, useTheme } from "@mui/material/styles"; import TuneIcon from "@mui/icons-material/Tune"; import TrendingUpIcon from "@mui/icons-material/TrendingUp"; import MailOutlineIcon from "@mui/icons-material/MailOutline"; import BusinessOutlinedIcon from "@mui/icons-material/BusinessOutlined"; import AutoGraphIcon from "@mui/icons-material/AutoGraph"; import { api } from "../api"; import ViewStateNotice from "./ViewStateNotice"; import OnboardingChecklist from "./OnboardingChecklist"; import { getUserKeyFromToken } from "../themePrefs"; import { useI18n } from "../i18n/I18nProvider"; import { statusLabel } from "../pipeline"; import { buildWorkflowPath, getWorkflowAction } from "../jobWorkflowSignals"; import { JobApplication } from "../types"; import { useViewResource } from "../hooks/useViewResource"; interface JobStats { total: number; active: number; deleted: number; byStatus: Record; appliedLast30Days: number; averageDaysSinceApplied: number; } type ReminderJob = JobApplication; type AnalyticsPoint = { month: string; applied: number; responses: number }; type TagPoint = { tag: string; count: number }; type OverviewAnalytics = { funnel: { label: string; count: number }[]; responseRateBySource: { label: string; total: number; responses: number; rate: number }[]; topCompanies: { companyId: number; company: string; count: number; responses: number; responseRate: number }[]; medianDaysToFirstResponse?: number | null; totalResponses: number; totalActive: number; timeInStage?: { stage: string; medianDays: number; count: number }[]; }; type TagTrendResponse = { months: string[]; series: { tag: string; counts: number[] }[] }; type Prefs = { cards: boolean; activity: boolean; funnel: boolean; companies: boolean; skills: boolean; }; function prefsKey() { return `dashboardPrefs:${getUserKeyFromToken()}`; } function loadPrefs(): Prefs { try { const raw = window.localStorage.getItem(prefsKey()); if (!raw) return { cards: true, activity: true, funnel: true, companies: true, skills: true }; return { cards: true, activity: true, funnel: true, companies: true, skills: true, ...JSON.parse(raw) }; } catch { return { cards: true, activity: true, funnel: true, companies: true, skills: true }; } } function savePrefs(next: Prefs) { window.localStorage.setItem(prefsKey(), JSON.stringify(next)); } function clamp(n: number, a: number, b: number) { return Math.max(a, Math.min(b, n)); } function buildLinePath(values: number[], width: number, height: number) { if (!values.length) return ""; const min = Math.min(...values); const max = Math.max(...values); const step = width / Math.max(1, values.length - 1); const yFor = (value: number) => { const t = max === min ? 0.5 : (value - min) / (max - min); return height - t * height; }; return values .map((value, index) => `${index === 0 ? "M" : "L"} ${Math.round(index * step)} ${Math.round(yFor(value))}`) .join(" "); } function MiniSpark({ values, color }: { values: number[]; color: string }) { const width = 180; const height = 52; const path = buildLinePath(values, width, height); return ( ); } function SectionCard({ children, sx = {} }: { children: React.ReactNode; sx?: any }) { return ( {children} ); } export default function DashboardView() { const theme = useTheme(); const isMobile = useMediaQuery("(max-width:767.95px)"); const navigate = useNavigate(); const { t } = useI18n(); const [months, setMonths] = useState<6 | 12 | 24>(12); const [prefs, setPrefs] = useState(() => loadPrefs()); const [prefsAnchor, setPrefsAnchor] = useState(null); const loadSummary = useCallback(async () => { const [statsResponse, overviewResponse, remindersResponse] = await Promise.all([ api.get("/jobapplications/stats"), api.get("/jobapplications/analytics-overview"), api.get("/jobapplications/reminders", { params: { upcomingDays: 14 } }), ]); return { stats: statsResponse.data, overview: overviewResponse.data, reminderJobs: Array.isArray(remindersResponse.data) ? remindersResponse.data : [], }; }, []); const summaryResource = useViewResource( loadSummary, { initialData: { stats: null as JobStats | null, overview: null as OverviewAnalytics | null, reminderJobs: [] as ReminderJob[] }, errorMessage: "Unable to load dashboard summary data right now.", deps: [], }, ); const loadTrends = useCallback(async () => { const params = { months }; const [analyticsResponse, tagsResponse, trendsResponse] = await Promise.all([ api.get("/jobapplications/analytics", { params }), api.get("/jobapplications/tags", { params: { limit: 10, ...params } }), api.get("/jobapplications/tag-trends", { params: { months, limit: 5 } }), ]); return { analytics: analyticsResponse.data ?? [], tags: tagsResponse.data ?? [], tagTrends: trendsResponse.data, }; }, [months]); const trendsResource = useViewResource( loadTrends, { initialData: { analytics: [] as AnalyticsPoint[], tags: [] as TagPoint[], tagTrends: null as TagTrendResponse | null }, errorMessage: "Unable to load dashboard trends right now.", deps: [months], }, ); const stats = summaryResource.data.stats; const overview = summaryResource.data.overview; const reminderJobs = summaryResource.data.reminderJobs; const analytics = trendsResource.data.analytics; const tags = trendsResource.data.tags; const tagTrends = trendsResource.data.tagTrends; const tagColors = useMemo( () => [theme.palette.primary.main, theme.palette.success.main, theme.palette.warning.main, theme.palette.info.main, theme.palette.error.main], [theme.palette.error.main, theme.palette.info.main, theme.palette.primary.main, theme.palette.success.main, theme.palette.warning.main], ); const summaryView = useMemo(() => { const topSource = overview?.responseRateBySource?.[0]; const missingCvCount = reminderJobs.filter((job) => job.workflowSignal?.hasPackageGap).length; return { funnelMax: overview?.funnel?.length ? Math.max(...overview.funnel.map((item) => item.count)) : 0, topSource, missingCvCount, priorityJobs: reminderJobs.slice(0, 5), }; }, [overview, reminderJobs]); const trendsView = useMemo(() => { const appliedValues = analytics.map((x) => x.applied); const responseValues = analytics.map((x) => x.responses); const chartWidth = isMobile ? Math.max(420, analytics.length * 70) : 860; const chartHeight = isMobile ? 210 : 250; const totalApplied = appliedValues.reduce((sum, value) => sum + value, 0); const totalResponses = responseValues.reduce((sum, value) => sum + value, 0); const maxTagCount = Math.max(...tags.map((item) => item.count), 1); return { appliedValues, responseValues, chartWidth, chartHeight, appliedPath: buildLinePath(appliedValues, chartWidth, chartHeight), responsePath: buildLinePath(responseValues, chartWidth, chartHeight), totalApplied, totalResponses, responseRate: totalApplied > 0 ? Math.round((totalResponses / totalApplied) * 100) : 0, maxTagCount, }; }, [analytics, isMobile, tags]); const metricCards = useMemo(() => ([ { label: t("dashboardActiveApplications"), value: stats?.active ?? 0, sub: t("dashboardCurrentlyInProgress"), icon: , tone: theme.palette.primary.main, spark: trendsView.appliedValues, }, { label: t("dashboardApplied30Days"), value: stats?.appliedLast30Days ?? 0, sub: t("dashboardNewApplications"), icon: , tone: theme.palette.success.main, spark: trendsView.appliedValues.slice(-6), }, { label: t("dashboardMedianFirstResponse"), value: overview?.medianDaysToFirstResponse ?? "—", sub: t("dashboardDaysUntilFirstReply"), icon: , tone: theme.palette.info.main, spark: trendsView.responseValues, }, { label: t("dashboardResponsesLogged"), value: overview?.totalResponses ?? 0, sub: t("dashboardAcrossActiveJobs"), icon: , tone: theme.palette.warning.main, spark: trendsView.responseValues.slice(-6), }, ]), [overview?.medianDaysToFirstResponse, overview?.totalResponses, stats?.active, stats?.appliedLast30Days, t, theme.palette.info.main, theme.palette.primary.main, theme.palette.success.main, theme.palette.warning.main, trendsView.appliedValues, trendsView.responseValues]); const togglePref = useCallback((key: keyof Prefs) => { setPrefs((current) => { const next = { ...current, [key]: !current[key] }; savePrefs(next); return next; }); }, []); const getReminderAction = useCallback((job: ReminderJob) => getWorkflowAction(job, { packageWork: t("jobTablePackageWork"), followUp: t("jobTableFollowUp"), interviewPrep: t("jobTableInterviewStage"), readiness: t("jobTableReadiness"), }), [t]); const openReminderJob = useCallback((job: ReminderJob) => { navigate(buildWorkflowPath(job)); }, [navigate]); return ( 0} /> {t("dashboardHeroLabel")} {t("dashboardOverviewTitle")} {t("dashboardOverviewBody")} {([6, 12, 24] as const).map((m) => ( ))} setPrefsAnchor(null)}> {[ ["cards", t("dashboardSummaryCards")], ["activity", t("dashboardActivityChart")], ["funnel", t("dashboardConversionFunnel")], ["companies", t("dashboardTopCompanies")], ["skills", t("dashboardSkillsInsights")], ].map(([key, label]) => ( togglePref(key as keyof Prefs)}> {label} ))} {!summaryResource.loading && !summaryResource.error && prefs.cards ? ( {metricCards.map((card) => ( {card.label} {card.value} {card.sub} {card.icon} ))} ) : null} {!summaryResource.loading && !summaryResource.error && prefs.activity ? ( {t("dashboardApplicationActivity")} {t("dashboardMonthlyApplicationsResponses")} {[0.2, 0.4, 0.6, 0.8].map((tick) => ( ))} {trendsView.responsePath ? : null} {trendsView.appliedPath ? : null} {analytics.map((point) => ( {point.month.slice(5)} ))} ) : null} {!summaryResource.loading && !summaryResource.error ? ( {t("dashboardConversionFunnelTitle")} {t("dashboardResponseSources")} {(overview?.funnel ?? []).map((item) => { const width = summaryView.funnelMax ? clamp((item.count / summaryView.funnelMax) * 100, 0, 100) : 0; return ( {statusLabel(t, item.label)} {item.count} ); })} {overview?.timeInStage?.length ? ( {t("dashboardTimeInStageTitle")} {overview.timeInStage.map((item) => ( {statusLabel(t, item.stage)} {t("dashboardTimeInStageValue", { days: item.medianDays, count: item.count })} ))} ) : null} {summaryView.topSource?.label ?? t("dashboardResponseSources")} {summaryView.topSource ? `${summaryView.topSource.rate}%` : "—"} {summaryView.topSource ? t("dashboardResponseConversion", { responses: summaryView.topSource.responses, total: summaryView.topSource.total }) : t("dashboardNoSourceData")} ) : null} {!summaryResource.loading && !summaryResource.error ? ( {t("remindersTitle")} {t("remindersSubtitle")} {summaryView.priorityJobs.length === 0 ? ( {t("remindersNothing")} ) : ( {summaryView.priorityJobs.map((job) => { const action = getReminderAction(job); return ( {job.company?.name ?? t("jobTableCompany")} • {job.jobTitle} {action?.detail ?? job.workflowSignal?.reason ?? job.followUpReason ?? t("remindersFollowUpLabel")} ); })} )} ) : null} {!summaryResource.loading && !summaryResource.error && prefs.companies ? ( {t("dashboardTopCompaniesByActivity")} {(overview?.topCompanies ?? []).map((item, index) => ( {item.company} {t("dashboardCompanyJobsResponses", { jobs: item.count, responses: item.responses })} = 50 ? "success" : item.responseRate >= 25 ? "warning" : "default"} variant="outlined" /> ))} ) : null} {!trendsResource.loading && !trendsResource.error && prefs.skills ? ( {t("dashboardTopSkills")} {tags.length === 0 ? ( {t("dashboardNoTagsYet")} ) : ( {tags.slice(0, 8).map((tag, index) => { const max = Math.max(...tags.map((item) => item.count), 1); const width = (tag.count / max) * 100; return ( {tag.tag} {tag.count} ); })} )} {t("dashboardSkillTrends")} {!tagTrends || tagTrends.series.length === 0 ? ( {t("dashboardNoTagTrendData")} ) : ( {tagTrends.series.map((series, index) => ( {series.tag} {series.counts.reduce((sum, value) => sum + value, 0)} total {series.counts.map((count, i) => ( 0 ? alpha(tagColors[index % tagColors.length], 0.22 + Math.min(0.6, count / 10)) : alpha(theme.palette.text.primary, 0.05), }} title={`${tagTrends.months[i]}: ${count}`} /> ))} ))} )} ) : null} ); }