feat: add global weather support
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import webPush, { type PushSubscription } from "web-push";
|
||||
import type { Language } from "@/lib/i18n";
|
||||
import { formatProvinceName } from "@/lib/provinces";
|
||||
import type { WeatherBrief } from "@/lib/weather-brief";
|
||||
import type { WeatherWarning } from "@/types/imgw";
|
||||
import type { WarningPushSubscription } from "@/types/notifications";
|
||||
import type { WeatherBrief } from "@/lib/weather-brief";
|
||||
|
||||
const VAPID_PUBLIC_KEY = process.env.WEB_PUSH_VAPID_PUBLIC_KEY;
|
||||
const VAPID_PRIVATE_KEY = process.env.WEB_PUSH_VAPID_PRIVATE_KEY;
|
||||
@@ -49,13 +49,13 @@ function formatWarningValidity(warning: WeatherWarning, language: Language) {
|
||||
}
|
||||
|
||||
function buildWarningPayload(preference: WarningPushSubscription, warning: WeatherWarning) {
|
||||
const province = formatProvinceName(preference.province, preference.language);
|
||||
const region = preference.province ? formatProvinceName(preference.province, preference.language) : preference.locationName ?? "wtr.";
|
||||
const title = warning.title || (preference.language === "pl" ? "Ostrzeżenie meteorologiczne" : "Weather warning");
|
||||
const validity = formatWarningValidity(warning, preference.language);
|
||||
const level = getWarningLevelLabel(warning, preference.language);
|
||||
const bodyParts = preference.language === "pl"
|
||||
? [`${province}: ${level}`, validity, warning.probability !== null ? `prawdopodobieństwo ${warning.probability}%` : null]
|
||||
: [`${province}: ${level}`, validity, warning.probability !== null ? `${warning.probability}% probability` : null];
|
||||
? [`${region}: ${level}`, validity, warning.probability !== null ? `prawdopodobieństwo ${warning.probability}%` : null]
|
||||
: [`${region}: ${level}`, validity, warning.probability !== null ? `${warning.probability}% probability` : null];
|
||||
|
||||
return {
|
||||
title: `IMGW: ${title}`,
|
||||
@@ -73,12 +73,12 @@ export async function sendWarningNotification(preference: WarningPushSubscriptio
|
||||
|
||||
export async function sendTestNotification(preference: WarningPushSubscription) {
|
||||
ensureWebPushConfigured();
|
||||
const province = formatProvinceName(preference.province, preference.language);
|
||||
const location = preference.province ? formatProvinceName(preference.province, preference.language) : preference.locationName ?? "Open-Meteo";
|
||||
const payload = JSON.stringify({
|
||||
title: preference.language === "pl" ? "wtr.: test powiadomień IMGW" : "wtr.: IMGW notification test",
|
||||
title: preference.language === "pl" ? "wtr.: test powiadomień" : "wtr.: notification test",
|
||||
body: preference.language === "pl"
|
||||
? `Powiadomienia są aktywne dla województwa: ${province}.`
|
||||
: `Notifications are active for: ${province}.`,
|
||||
? `Powiadomienia są aktywne dla: ${location}.`
|
||||
: `Notifications are active for: ${location}.`,
|
||||
url: "/settings",
|
||||
});
|
||||
await webPush.sendNotification(preference.subscription as PushSubscription, payload);
|
||||
|
||||
Reference in New Issue
Block a user