File size: 4,579 Bytes
4a57073 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | "use client";
import { useState, useEffect, useCallback, useMemo } from "react";
import {
todayISO,
loadMedications,
loadMedicationLogs,
loadAppointments,
type Medication,
type Appointment,
} from "../health-store";
export interface Notification {
id: string;
type: "medication" | "appointment" | "reminder" | "info";
title: string;
message: string;
time: string; // "HH:MM"
read: boolean;
urgent: boolean;
}
const DISMISSED_KEY = "medos_dismissed_notifications";
function loadDismissed(): Set<string> {
try {
const raw = localStorage.getItem(DISMISSED_KEY);
return raw ? new Set(JSON.parse(raw)) : new Set();
} catch {
return new Set();
}
}
function saveDismissed(ids: Set<string>): void {
try {
localStorage.setItem(DISMISSED_KEY, JSON.stringify([...ids]));
} catch {}
}
/**
* Generates today's notifications from the user's health data.
*
* Notifications are derived, not stored: every time the hook runs it
* scans medications + appointments for today and creates reminders
* for anything that's due or overdue. The user can dismiss individual
* notifications (persisted in localStorage).
*
* This is a client-side-only system — no push notifications, no server
* involvement. In the future this can be upgraded to Web Push / service
* worker notifications.
*/
export function useNotifications() {
const [dismissed, setDismissed] = useState<Set<string>>(new Set());
const [tick, setTick] = useState(0);
// Refresh every minute.
useEffect(() => {
setDismissed(loadDismissed());
const id = setInterval(() => setTick((t) => t + 1), 60000);
return () => clearInterval(id);
}, []);
const today = todayISO();
const now = new Date();
const nowMinutes = now.getHours() * 60 + now.getMinutes();
const notifications: Notification[] = useMemo(() => {
const items: Notification[] = [];
const meds = loadMedications().filter((m) => m.active);
const logs = loadMedicationLogs();
const appts = loadAppointments().filter(
(a) => a.date === today && a.status === "upcoming",
);
// Medication reminders — for each scheduled time today.
for (const med of meds) {
for (const time of med.times) {
const [h, m] = time.split(":").map(Number);
const medMinutes = (h || 0) * 60 + (m || 0);
const taken = logs.some(
(l) =>
l.medicationId === med.id &&
l.date === today &&
l.time === time &&
l.taken,
);
if (taken) continue; // Already taken — no notification.
const overdue = nowMinutes > medMinutes + 30;
const dueSoon = !overdue && nowMinutes >= medMinutes - 15;
if (overdue || dueSoon) {
items.push({
id: `med-${med.id}-${time}-${today}`,
type: "medication",
title: overdue ? `Overdue: ${med.name}` : `Due now: ${med.name}`,
message: `${med.dose} scheduled at ${time}`,
time,
read: false,
urgent: overdue,
});
}
}
}
// Appointment reminders — 30 min before.
for (const appt of appts) {
const [h, m] = appt.time.split(":").map(Number);
const apptMinutes = (h || 0) * 60 + (m || 0);
const in30 = apptMinutes - nowMinutes <= 30 && apptMinutes >= nowMinutes;
const overdue = nowMinutes > apptMinutes;
if (in30 || overdue) {
items.push({
id: `appt-${appt.id}-${today}`,
type: "appointment",
title: overdue ? `Missed: ${appt.title}` : `Coming up: ${appt.title}`,
message: `${appt.time}${appt.doctor ? ` · ${appt.doctor}` : ""}${appt.location ? ` · ${appt.location}` : ""}`,
time: appt.time,
read: false,
urgent: overdue,
});
}
}
return items.sort((a, b) => (a.urgent === b.urgent ? 0 : a.urgent ? -1 : 1));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [today, tick]);
const active = notifications.filter((n) => !dismissed.has(n.id));
const count = active.length;
const dismiss = useCallback(
(id: string) => {
const next = new Set(dismissed);
next.add(id);
setDismissed(next);
saveDismissed(next);
},
[dismissed],
);
const dismissAll = useCallback(() => {
const next = new Set(dismissed);
for (const n of notifications) next.add(n.id);
setDismissed(next);
saveDismissed(next);
}, [dismissed, notifications]);
return { notifications: active, count, dismiss, dismissAll };
}
|