import { useEffect, useRef, useState } from "react" import { Pill, Check, Clock, SkipForward, X } from "lucide-react" import { getToday, respondDose } from "../api" import { useAuth } from "../auth/AuthContext" import { useI18n } from "../i18n/I18nContext" // In-app "alarm": polls today's doses and pops a modal (with a beep) when a // dose becomes due. Works on every authenticated page (mounted in AppShell). const POLL_MS = 30000 function beep() { try { const Ctx = window.AudioContext || window.webkitAudioContext if (!Ctx) return const ctx = new Ctx() const play = (freq, start) => { const o = ctx.createOscillator(); const g = ctx.createGain() o.connect(g); g.connect(ctx.destination) o.type = "sine"; o.frequency.value = freq g.gain.setValueAtTime(0.0001, ctx.currentTime + start) g.gain.exponentialRampToValueAtTime(0.25, ctx.currentTime + start + 0.05) g.gain.exponentialRampToValueAtTime(0.0001, ctx.currentTime + start + 0.5) o.start(ctx.currentTime + start); o.stop(ctx.currentTime + start + 0.55) } play(880, 0); play(660, 0.3); play(880, 0.6) } catch { /* autoplay may be blocked; ignore */ } } export default function DoseReminder() { const { isAuthenticated } = useAuth() const { t } = useI18n() const [active, setActive] = useState(null) // the due dose to show const alarmed = useRef(new Set()) // dose ids already alarmed this session const isDue = (d) => { if (d.status !== "pending") return false const [h, m] = d.time.split(":").map(Number) const due = new Date(); due.setHours(h, m, 0, 0) return due.getTime() <= Date.now() } useEffect(() => { if (!isAuthenticated) return let stop = false const check = async () => { try { const doses = await getToday() if (stop) return const due = doses.find((d) => isDue(d) && !alarmed.current.has(d.id)) if (due && !active) { alarmed.current.add(due.id) beep() setActive(due) } } catch { /* ignore */ } } check() const id = setInterval(check, POLL_MS) return () => { stop = true; clearInterval(id) } }, [isAuthenticated, active]) const act = async (action) => { if (!active) return try { await respondDose(active.id, action) } catch { /* ignore */ } setActive(null) window.dispatchEvent(new CustomEvent("doses-changed")) } if (!active) return null return (
🔔
{t("reminder_title")}
{t("reminder_take_now")}
{active.drug_name} {active.time}
) } const overlay = { position: "fixed", inset: 0, background: "rgba(15,23,42,0.55)", display: "flex", alignItems: "center", justifyContent: "center", zIndex: 2000, backdropFilter: "blur(2px)" } const card = { position: "relative", width: "90%", maxWidth: 380, background: "#fff", borderRadius: 20, padding: "28px 24px", boxShadow: "0 20px 60px rgba(0,0,0,0.3)", animation: "pop .25s ease-out" } const closeBtn = { position: "absolute", insetInlineEnd: 14, top: 14, background: "transparent", border: "none", color: "#94a3b8", cursor: "pointer" } const bell = { fontSize: 44, textAlign: "center", marginBottom: 8 } const pillRow = { display: "flex", alignItems: "center", justifyContent: "center", gap: 10, marginTop: 18, background: "#f0fdf4", border: "1px solid #bbf7d0", borderRadius: 12, padding: "12px 16px" } const timeChip = { background: "#1B4332", color: "#fff", fontSize: 13, fontWeight: 700, padding: "4px 10px", borderRadius: 8 } const bigBtn = (bg, color, primary) => ({ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", gap: 4, padding: "12px 8px", borderRadius: 12, background: bg, color, border: primary ? "none" : "1px solid #e2e8f0", fontSize: 13, fontWeight: 600, cursor: "pointer", })