Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/components/Beeper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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++) {
Expand Down