160 lines
5.3 KiB
TypeScript
160 lines
5.3 KiB
TypeScript
import { useState, useEffect, useRef } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { AlertTriangle, Clock, CheckCircle2, ExternalLink } from "lucide-react";
|
|
import { notificationsApi } from "@/api/notifications";
|
|
import { useAuthStore } from "@/hooks/useAuth";
|
|
import { formatDate } from "@/lib/format";
|
|
import { Modal } from "@/components/ui/dialog";
|
|
import { Button } from "@/components/ui/button";
|
|
import { toast } from "@/lib/toast";
|
|
|
|
export function CalibrationReminderModal() {
|
|
const navigate = useNavigate();
|
|
const qc = useQueryClient();
|
|
const { user, token } = useAuthStore();
|
|
const [open, setOpen] = useState(false);
|
|
const promptedTokensRef = useRef<Set<string>>(new Set());
|
|
|
|
const sessionKey = token ? `cal_popup_shown:${token.slice(-16)}` : null;
|
|
|
|
const calNotifsQuery = useQuery({
|
|
queryKey: ["notifications", "calibration-login-popup", user?.id],
|
|
queryFn: () =>
|
|
notificationsApi.list({
|
|
type: "calibration_due",
|
|
unread_only: true,
|
|
limit: 25,
|
|
}),
|
|
enabled: Boolean(user && token),
|
|
});
|
|
|
|
const items = calNotifsQuery.data?.items ?? [];
|
|
|
|
useEffect(() => {
|
|
if (!token || !user || items.length === 0) return;
|
|
const alreadyShownInRef = promptedTokensRef.current.has(token);
|
|
let alreadyShownInSession = false;
|
|
try {
|
|
alreadyShownInSession = Boolean(
|
|
sessionKey && sessionStorage.getItem(sessionKey)
|
|
);
|
|
} catch {
|
|
alreadyShownInSession = false;
|
|
}
|
|
|
|
if (!alreadyShownInRef && !alreadyShownInSession) {
|
|
promptedTokensRef.current.add(token);
|
|
try {
|
|
if (sessionKey) sessionStorage.setItem(sessionKey, "1");
|
|
} catch {
|
|
// ignore storage errors
|
|
}
|
|
setOpen(true);
|
|
}
|
|
}, [token, user, items.length, sessionKey]);
|
|
|
|
const acknowledgeMutation = useMutation({
|
|
mutationFn: () => notificationsApi.markAllRead("calibration_due"),
|
|
onSuccess: async () => {
|
|
await qc.invalidateQueries({ queryKey: ["notifications"] });
|
|
await qc.invalidateQueries({ queryKey: ["dashboard"] });
|
|
toast.success("Calibration reminders acknowledged");
|
|
setOpen(false);
|
|
},
|
|
onError: () => {
|
|
toast.error("Failed to acknowledge calibration reminders");
|
|
},
|
|
});
|
|
|
|
if (!open || items.length === 0) return null;
|
|
|
|
return (
|
|
<Modal
|
|
open={open}
|
|
onOpenChange={(o) => !o && setOpen(false)}
|
|
title={`Calibration Reminders (${items.length} Unread)`}
|
|
className="max-w-xl"
|
|
>
|
|
<div className="space-y-4">
|
|
<p className="text-sm text-gray-600">
|
|
The following equipment calibration schedules are due or overdue and require
|
|
attention:
|
|
</p>
|
|
|
|
<div className="max-h-72 overflow-y-auto divide-y divide-gray-100 border border-gray-200 rounded-lg">
|
|
{items.map((n) => {
|
|
const isRed = n.severity === "red";
|
|
return (
|
|
<div
|
|
key={n.id}
|
|
className="p-3 flex items-start gap-2.5 bg-white hover:bg-gray-50"
|
|
>
|
|
{isRed ? (
|
|
<AlertTriangle className="h-4 w-4 text-red-600 shrink-0 mt-0.5" />
|
|
) : (
|
|
<Clock className="h-4 w-4 text-amber-600 shrink-0 mt-0.5" />
|
|
)}
|
|
<div className="flex-1 min-w-0">
|
|
<div className="flex items-center justify-between gap-2">
|
|
<span className="text-sm font-semibold text-gray-900 truncate">
|
|
{n.title}
|
|
</span>
|
|
<span
|
|
className={`text-[10px] font-semibold px-2 py-0.5 rounded-full uppercase ${
|
|
isRed
|
|
? "bg-red-100 text-red-800"
|
|
: "bg-amber-100 text-amber-800"
|
|
}`}
|
|
>
|
|
{n.severity}
|
|
</span>
|
|
</div>
|
|
<p className="text-xs text-gray-600 mt-0.5">{n.message}</p>
|
|
{n.due_date && (
|
|
<div className="text-[11px] text-gray-500 mt-1">
|
|
Due Date: <strong>{formatDate(n.due_date)}</strong>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2 pt-1">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => {
|
|
setOpen(false);
|
|
navigate("/calibration");
|
|
}}
|
|
>
|
|
<ExternalLink className="h-3.5 w-3.5 mr-1.5" />
|
|
Open Calibration Register
|
|
</Button>
|
|
<div className="flex items-center gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={() => setOpen(false)}
|
|
>
|
|
Remind Me Later
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
loading={acknowledgeMutation.isPending}
|
|
onClick={() => acknowledgeMutation.mutate()}
|
|
>
|
|
<CheckCircle2 className="h-4 w-4 mr-1.5" />
|
|
Acknowledge & Mark Read
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
);
|
|
}
|