Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Allow per project push notifications channel

Blue Mouseff02c01ddfe0
diff --git a/web/app/components/NotificationChannels/EnableWebPushButton.tsx b/web/app/components/NotificationChannels/EnableWebPushButton.tsx
index 2cf915c9c..83ba5d4ca 100644
--- a/web/app/components/NotificationChannels/EnableWebPushButton.tsx
+++ b/web/app/components/NotificationChannels/EnableWebPushButton.tsx
@@ -32,11 +32,17 @@ const arrayBufferToBase64 = (buffer: ArrayBuffer | null): string => {
interface EnableWebPushButtonProps {
onSubscribed?: () => void
+ /** Render style. "button" (default) shows a Button; "link" shows an underlined inline link. */
+ variant?: 'button' | 'link'
+ className?: string
}
-const EnableWebPushButton = ({ onSubscribed }: EnableWebPushButtonProps) => {
+const EnableWebPushButton = ({
+ onSubscribed,
+ variant = 'button',
+ className,
+}: EnableWebPushButtonProps) => {
const { t } = useTranslation('common')
- const keyFetcher = useFetcher<NotificationChannelActionData>()
const subscribeFetcher = useFetcher<NotificationChannelActionData>()
const lastSubscribeData = useRef<NotificationChannelActionData | null>(null)
const [busy, setBusy] = useState(false)
@@ -90,6 +96,12 @@ const EnableWebPushButton = ({ onSubscribed }: EnableWebPushButtonProps) => {
method: 'POST',
body: keyData,
})
+ const contentType = keyResp.headers.get('content-type') || ''
+ if (!keyResp.ok || !contentType.includes('application/json')) {
+ toast.error(t('notificationChannels.webpush.subscribeFailed'))
+ setBusy(false)
+ return
+ }
const keyJson = (await keyResp.json()) as {
publicKey?: string | null
error?: string
@@ -148,12 +160,33 @@ const EnableWebPushButton = ({ onSubscribed }: EnableWebPushButtonProps) => {
if (!supported) return null
+ const loading = busy || subscribeFetcher.state !== 'idle'
+
+ if (variant === 'link') {
+ return (
+ <button
+ type='button'
+ onClick={onClick}
+ disabled={loading}
+ className={
+ className ||
+ 'font-medium text-amber-900 underline decoration-dashed underline-offset-2 hover:decoration-solid disabled:opacity-50 dark:text-amber-50'
+ }
+ >
+ {loading
+ ? t('common.loading')
+ : t('notificationChannels.webpush.enable')}
+ </button>
+ )
+ }
+
return (
<Button
secondary
small
onClick={onClick}
- loading={busy || keyFetcher.state !== 'idle'}
+ loading={loading}
+ className={className}
>
<span className='inline-flex items-center gap-1'>
<BellRingingIcon className='size-4' aria-hidden />
diff --git a/web/app/components/NotificationChannels/NotificationChannels.tsx b/web/app/components/NotificationChannels/NotificationChannels.tsx
index 56c46afcc..b3990d391 100644
--- a/web/app/components/NotificationChannels/NotificationChannels.tsx
+++ b/web/app/components/NotificationChannels/NotificationChannels.tsx
@@ -17,6 +17,7 @@ import type {
NotificationChannelType,
} from '~/lib/models/NotificationChannel'
import type { NotificationChannelActionData } from '~/routes/notification-channel'
+import Alert from '~/ui/Alert'
import { Badge } from '~/ui/Badge'
import Button from '~/ui/Button'
import Input from '~/ui/Input'
@@ -303,8 +304,6 @@ const NotificationChannels = ({
[allowedTypes, t],
)
- const showWebpushButton = allowedTypes.includes('webpush') && scope === 'user'
-
const headingTitle = title || t('notificationChannels.heading')
const headingDescription =
description || t('notificationChannels.description')
@@ -330,9 +329,6 @@ const NotificationChannels = ({
</Text>
</div>
<div className='flex flex-wrap items-center gap-2'>
- {showWebpushButton ? (
- <EnableWebPushButton onSubscribed={triggerList} />
- ) : null}
<Button
small
primary
@@ -547,16 +543,18 @@ const NotificationChannels = ({
</div>
) : null}
- <div className='mt-4'>
- <Input
- label={t('common.name')}
- value={form.name}
- onChange={(e) =>
- setForm((prev) => ({ ...prev, name: e.target.value }))
- }
- placeholder='My alerts channel'
- />
- </div>
+ {form.type !== 'webpush' || !!editing ? (
+ <div className='mt-4'>
+ <Input
+ label={t('common.name')}
+ value={form.name}
+ onChange={(e) =>
+ setForm((prev) => ({ ...prev, name: e.target.value }))
+ }
+ placeholder='My alerts channel'
+ />
+ </div>
+ ) : null}
<div className='mt-3 grid grid-cols-1 gap-3'>
{form.type === 'email' ? (
@@ -626,10 +624,21 @@ const NotificationChannels = ({
) : null}
</>
) : null}
- {form.type === 'webpush' ? (
- <Text as='p' size='sm' colour='secondary'>
- {t('notificationChannels.webpush.useButton')}
- </Text>
+ {form.type === 'webpush' && !editing ? (
+ <Alert
+ variant='info'
+ title={t('notificationChannels.webpush.alertTitle')}
+ >
+ <p>{t('notificationChannels.webpush.alertDescription')}</p>
+ <div className='mt-3'>
+ <EnableWebPushButton
+ onSubscribed={() => {
+ setCreating(false)
+ triggerList()
+ }}
+ />
+ </div>
+ </Alert>
) : null}
</div>
</div>
diff --git a/web/app/pages/Project/Settings/ProjectSettings.tsx b/web/app/pages/Project/Settings/ProjectSettings.tsx
index 06c9c30c4..68136ce09 100644
--- a/web/app/pages/Project/Settings/ProjectSettings.tsx
+++ b/web/app/pages/Project/Settings/ProjectSettings.tsx
@@ -1312,6 +1312,7 @@ const ProjectSettings = () => {
'discord',
'slack',
'webhook',
+ 'webpush',
]}
/>
</>
diff --git a/web/app/routes/notification-channel.tsx b/web/app/routes/notification-channel.ts
similarity index 99%
rename from web/app/routes/notification-channel.tsx
rename to web/app/routes/notification-channel.ts
index c7732c1dd..a2869e751 100644
--- a/web/app/routes/notification-channel.tsx
+++ b/web/app/routes/notification-channel.ts
@@ -324,7 +324,3 @@ export async function action({ request }: ActionFunctionArgs) {
)
}
}
-
-export default function NotificationChannelResource() {
- return null
-}
diff --git a/web/public/locales/de.json b/web/public/locales/de.json
index 228229a4f..88b80b843 100644
--- a/web/public/locales/de.json
+++ b/web/public/locales/de.json
@@ -1277,11 +1277,12 @@
"webpush": {
"enable": "Browser-Benachrichtigungen aktivieren",
"enabled": "Browser-Benachrichtigungen sind auf diesem Gerät aktiviert.",
- "useButton": "Verwende die Schaltfläche \"Browser-Benachrichtigungen aktivieren\", um dieses Gerät zu abonnieren.",
"unsupported": "Dieser Browser unterstützt keine Push-Benachrichtigungen.",
"permissionDenied": "Die Berechtigung für Benachrichtigungen wurde verweigert.",
"notConfigured": "Web-Push ist auf diesem Server nicht konfiguriert.",
- "subscribeFailed": "Konnte Push-Benachrichtigungen nicht abonnieren."
+ "subscribeFailed": "Konnte Push-Benachrichtigungen nicht abonnieren.",
+ "alertTitle": "Browser-Benachrichtigungen benötigen eine Berechtigung",
+ "alertDescription": "Um Web-Push-Warnungen auf diesem Gerät zu erhalten, erlauben Sie Swetrix, Ihnen Browser-Benachrichtigungen zu senden. Wir registrieren einen mit diesem Browser verknüpften Benachrichtigungskanal."
}
},
"goals": {
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 700143cb1..19c5642cf 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -1277,11 +1277,12 @@
"webpush": {
"enable": "Enable browser notifications",
"enabled": "Browser notifications are enabled on this device.",
- "useButton": "Use the \"Enable browser notifications\" button to subscribe this device.",
"unsupported": "This browser does not support push notifications.",
"permissionDenied": "Notification permission was denied.",
"notConfigured": "Web push is not configured on this server.",
- "subscribeFailed": "Could not subscribe to push notifications."
+ "subscribeFailed": "Could not subscribe to push notifications.",
+ "alertTitle": "Browser notifications need permission",
+ "alertDescription": "To receive web push alerts on this device, allow Swetrix to send you browser notifications. We'll register a notification channel tied to this browser."
}
},
"goals": {
diff --git a/web/public/locales/fr.json b/web/public/locales/fr.json
index 677640dd0..6ca71d11b 100644
--- a/web/public/locales/fr.json
+++ b/web/public/locales/fr.json
@@ -1277,11 +1277,12 @@
"webpush": {
"enable": "Activer les notifications du navigateur",
"enabled": "Les notifications du navigateur sont activées sur cet appareil.",
- "useButton": "Utilisez le bouton \"Activer les notifications du navigateur\" pour abonner cet appareil.",
"unsupported": "Ce navigateur ne prend pas en charge les notifications push.",
"permissionDenied": "L'autorisation de notification a été refusée.",
"notConfigured": "Le push web n'est pas configuré sur ce serveur.",
- "subscribeFailed": "Impossible de s'abonner aux notifications push."
+ "subscribeFailed": "Impossible de s'abonner aux notifications push.",
+ "alertTitle": "Les notifications du navigateur nécessitent une autorisation",
+ "alertDescription": "Pour recevoir des alertes web push sur cet appareil, autorisez Swetrix à vous envoyer des notifications de navigateur. Nous enregistrerons un canal de notification lié à ce navigateur."
}
},
"goals": {
diff --git a/web/public/locales/pl.json b/web/public/locales/pl.json
index a7e756501..0d540c42b 100644
--- a/web/public/locales/pl.json
+++ b/web/public/locales/pl.json
@@ -1277,11 +1277,12 @@
"webpush": {
"enable": "Włącz powiadomienia w przeglądarce",
"enabled": "Powiadomienia w przeglądarce są włączone na tym urządzeniu.",
- "useButton": "Użyj przycisku \"Włącz powiadomienia w przeglądarce\", aby zasubskrybować to urządzenie.",
"unsupported": "Ta przeglądarka nie obsługuje powiadomień push.",
"permissionDenied": "Odmówiono uprawnień do powiadomień.",
"notConfigured": "Web push nie jest skonfigurowany na tym serwerze.",
- "subscribeFailed": "Nie udało się zasubskrybować powiadomień push."
+ "subscribeFailed": "Nie udało się zasubskrybować powiadomień push.",
+ "alertTitle": "Powiadomienia przeglądarki wymagają uprawnień",
+ "alertDescription": "Aby otrzymywać alerty web push na tym urządzeniu, zezwól Swetrix na wysyłanie powiadomień w przeglądarce. Zarejestrujemy kanał powiadomień powiązany z tą przeglądarką."
}
},
"goals": {
diff --git a/web/public/locales/uk.json b/web/public/locales/uk.json
index 097a5bc99..7b9869bb5 100644
--- a/web/public/locales/uk.json
+++ b/web/public/locales/uk.json
@@ -1277,11 +1277,12 @@
"webpush": {
"enable": "Увімкнути сповіщення в браузері",
"enabled": "Сповіщення в браузері ввімкнено на цьому пристрої.",
- "useButton": "Використовуйте кнопку \"Увімкнути сповіщення в браузері\", щоб підписати цей пристрій.",
"unsupported": "Цей браузер не підтримує push-сповіщення.",
"permissionDenied": "Дозвіл на сповіщення було відхилено.",
"notConfigured": "Web push не налаштовано на цьому сервері.",
- "subscribeFailed": "Не вдалося підписатися на push-сповіщення."
+ "subscribeFailed": "Не вдалося підписатися на push-сповіщення.",
+ "alertTitle": "Для сповіщень у браузері потрібен дозвіл",
+ "alertDescription": "Щоб отримувати веб-push сповіщення на цьому пристрої, дозвольте Swetrix надсилати вам сповіщення у браузері. Ми зареєструємо канал сповіщень, прив'язаний до цього браузера."
}
},
"goals": {