import React, { useMemo } from "react";
import { useNavigate } from "react-router-dom";
import { Box, Button, Chip, Divider, Paper, Typography } from "@mui/material";
import { api } from "../api";
import ViewStateNotice from "./ViewStateNotice";
import { JobApplication } from "../types";
import { buildWorkflowPath, getReminderGroup, getWorkflowAction } from "../jobWorkflowSignals";
import { useToast } from "../toast";
import { useI18n } from "../i18n/I18nProvider";
import { useViewResource } from "../hooks/useViewResource";
type ReminderGroups = {
missingCv: JobApplication[];
missingInterviewNotes: JobApplication[];
overdueFollowUp: JobApplication[];
other: JobApplication[];
};
function groupItems(items: JobApplication[]): ReminderGroups {
const groups: ReminderGroups = { missingCv: [], missingInterviewNotes: [], overdueFollowUp: [], other: [] };
items.forEach((item) => {
const group = getReminderGroup(item);
groups[group].push(item);
});
return groups;
}
function ReminderSection({ title, items, onOpen, onSetFollowUp }: { title: string; items: JobApplication[]; onOpen: (job: JobApplication) => void; onSetFollowUp: (id: number, days: number | null) => void }) {
const { t } = useI18n();
if (items.length === 0) return null;
return (
{title}
{items.map((j) => {
const action = getWorkflowAction(j, {
packageWork: t("jobTablePackageWork"),
followUp: t("jobTableFollowUp"),
interviewPrep: t("jobTableInterviewStage"),
readiness: t("jobTableReadiness"),
});
return (
{j.company?.name ?? ""} • {j.jobTitle}
{j.needsFollowUp ? : null}
{(j.workflowSignal?.reason ?? j.followUpReason) ? : null}
{j.followUpAt ? : null}
);
})}
);
}
export default function RemindersView() {
const navigate = useNavigate();
const { toast } = useToast();
const { t } = useI18n();
const remindersResource = useViewResource(
async () => {
const res = await api.get("/jobapplications/reminders", { params: { upcomingDays: 14 } });
return Array.isArray(res.data) ? res.data : [];
},
{
initialData: [],
errorMessage: "Unable to load reminders right now.",
deps: [],
},
);
const items = remindersResource.data;
const grouped = useMemo(() => groupItems(items), [items]);
const openJob = (job: JobApplication) => {
navigate(buildWorkflowPath(job));
};
const setFollowUp = async (id: number, daysFromNow: number | null) => {
try {
const d = daysFromNow === null ? null : new Date(Date.now() + daysFromNow * 24 * 60 * 60 * 1000).toISOString().slice(0, 10);
await api.patch(`/jobapplications/${id}/followup`, { followUpAt: d });
toast(daysFromNow === null ? t("remindersFollowUpCleared") : t("remindersFollowUpSet"), "success");
await remindersResource.reload();
} catch {
toast(t("remindersFollowUpFailed"), "error");
}
};
return (
{t("remindersTitle")}
{t("remindersSubtitle")}
{!remindersResource.loading && !remindersResource.error ? (
{items.length === 0 ? {t("remindersNothing")} : null}
) : null}
{t("remindersTip")}
);
}