dosemate / frontend /src /components /DoseReminder.jsx
DoseMate
Deploy DoseMate to Hugging Face Spaces
a0c836f
Raw History Blame Contribute Delete
5.1 kB
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 (
<div style={overlay}>
<style>{`@keyframes pop { 0% { opacity:0; transform: scale(.9) } 100% { opacity:1; transform: scale(1) } }`}</style>
<div style={card}>
<button onClick={() => setActive(null)} style={closeBtn} aria-label="dismiss"><X size={18} /></button>
<div style={bell}>🔔</div>
<div style={{ fontSize: 18, fontWeight: 700, color: "#0f172a", textAlign: "center" }}>{t("reminder_title")}</div>
<div style={{ fontSize: 13, color: "#64748b", marginTop: 6, textAlign: "center" }}>{t("reminder_take_now")}</div>
<div style={pillRow}>
<Pill size={18} color="#16a34a" />
<span style={{ fontSize: 18, fontWeight: 700, color: "#0f172a" }}>{active.drug_name}</span>
<span style={timeChip}>{active.time}</span>
</div>
<div style={{ display: "flex", gap: 10, marginTop: 22 }}>
<button onClick={() => act("taken")} style={bigBtn("#16a34a", "#fff", true)}>
<Check size={17} /> {t("taken")}
</button>
<button onClick={() => act("snooze")} style={bigBtn("#fff", "#0f172a", false)}>
<Clock size={17} /> {t("snooze")}
</button>
<button onClick={() => act("skip")} style={bigBtn("#fff", "#0f172a", false)}>
<SkipForward size={17} /> {t("skip")}
</button>
</div>
</div>
</div>
)
}
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",
})