diff --git a/src/components/Beeper.tsx b/src/components/Beeper.tsx index 3aa037c..818408a 100644 --- a/src/components/Beeper.tsx +++ b/src/components/Beeper.tsx @@ -41,6 +41,31 @@ const sendTelegramNotification = async ( } }; + + +/** + * Sends a notification to NTFY (https://ntfy.sh). + * (e.g. "https://ntfy.sh/notify-fe-5080-usa"). + */ +const sendNtfyNotification = async (topicOrUrl: string, message: string) => { + if (!topicOrUrl) return; + try { + const url = topicOrUrl.startsWith("http") + ? topicOrUrl + : `https://ntfy.sh/${topicOrUrl}`; + + const currentUrl = + typeof window !== "undefined" ? window.location.href : ""; + + const body = `Notify-FE Alarm: ${message}\n\nClick to open: ${currentUrl}`; + + await fetch(url, { method: "POST", body }); + console.log("NTFY notification sent"); + } catch (error) { + console.error("Error sending NTFY notification:", error); + } +}; + // Instead of being a simple exported function, we wrap it in a React component hook // Alternatively, you can export a function that accepts settings as argument, // but here we create a hook function for consistency. @@ -70,6 +95,24 @@ export const usePlaySound = () => { await sendTelegramNotification(telegramApiUrl, message); } + // --- NTFY push --- + // hardcode a shared public topic + const NTFY_TOPIC = "notify-fe-5080-usa"; + + if (message) { + // Read current region from the URL (e.g. ...?region=en-us) + const isBrowser = typeof window !== "undefined"; + const regionOK = + isBrowser && + new URL(window.location.href).searchParams.get("region")?.toLowerCase() === "en-us"; + + const is5080 = /\b5080\b/i.test(message); + + if (regionOK && is5080) { + void sendNtfyNotification(NTFY_TOPIC, message).catch(console.error); + } + } + // Use 1 play when forceSingle is true, otherwise use the repetitions from settings. const playCount = forceSingle ? 1 : repetitions; for (let i = 0; i < playCount; i++) {