All files BuzzerAlert.tsx

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                               
import { useEffect, useState } from "react";
import { TbAlertHexagonFilled } from "react-icons/tb";
import { useStores } from "~/hooks/useStores";
import logoSvg from "~/assets/logo.svg";
import { IoIosCloseCircleOutline } from "react-icons/io";
 
function triggerEmergencyNotification() {
	new Notification("🚨 BUZZER EMERGENCY ALERT 🚨", {
		body: "Take a moment to ensure everything is okay.",
		icon: logoSvg,
		requireInteraction: true,
		tag: "emergency-buzzer",
		silent: false,
	});
}
export const BuzzerAlert = () => {
	const { hub } = useStores();
	const [buzzerOn, setBuzzerOn] = useState<boolean>(false);
	const [dismissed, setDismissed] = useState<boolean>(false);
 
	useEffect(() => {
		const buzzer = hub.devices?.find((device) =>
			device.name.includes("buzzer"),
		);
 
		const shouldBeOn = !!(buzzer?.switchState && buzzer?.isActive);
 
		setBuzzerOn((prev) => {
			if (prev !== shouldBeOn) {
				return shouldBeOn;
			}
			return prev;
		});
	}, [hub.devices]);
 
	useEffect(() => {
		if (buzzerOn) {
			if (Notification.permission !== "granted") {
				Notification.requestPermission().then((permission) => {
					if (permission === "granted") {
						triggerEmergencyNotification();
					}
				});
			} else {
				triggerEmergencyNotification();
			}
		}
	}, [buzzerOn]);
 
	return (
		<>
			{buzzerOn && !dismissed && (
				<div className="fixed top-24 lg:top-2 left-1/2 transform -translate-x-1/2 z-50 px-4 w-full max-w-lg">
					<div className="flex items-center gap-2 bg-red-600 text-white text-base font-black px-4 py-3 rounded-2xl shadow-lg">
						<TbAlertHexagonFilled size={40} className="shrink-0" />
						<span className="flex-1 text-center">
							Emergency! Buzzer has been activated
						</span>
						<button
							onClick={() => setDismissed(true)}
							className="ml-2 text-white hover:text-red-200 font-bold text-lg leading-none hover:cursor-pointer"
							aria-label="Dismiss"
						>
							<IoIosCloseCircleOutline size={22} />
						</button>
					</div>
				</div>
			)}
		</>
	);
};