mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-06 00:32:26 -08:00
feat(tauri): disable web push notifications
This commit is contained in:
4 files changed
+56
-23
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { MatrixClient } from '$types/matrix-sdk';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { ClientConfig } from '../../../hooks/useClientConfig';
|
||||
|
||||
type PushSubscriptionState = [
|
||||
@@ -23,6 +24,9 @@ export async function enablePushNotifications(
|
||||
clientConfig: ClientConfig,
|
||||
pushSubscriptionAtom: PushSubscriptionState
|
||||
): Promise<void> {
|
||||
if (isTauri()) {
|
||||
throw new Error('Push notifications are disabled in Tauri runtime.');
|
||||
}
|
||||
if (!('serviceWorker' in navigator) || !('PushManager' in window)) {
|
||||
throw new Error('Push messaging is not supported in this browser.');
|
||||
}
|
||||
@@ -113,6 +117,8 @@ export async function disablePushNotifications(
|
||||
clientConfig: ClientConfig,
|
||||
pushSubscriptionAtom: PushSubscriptionState
|
||||
): Promise<void> {
|
||||
if (isTauri()) return;
|
||||
|
||||
const [pushSubAtom] = pushSubscriptionAtom;
|
||||
|
||||
const pusherData = {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useCallback, useEffect, useState } from 'react';
|
||||
import { Box, Text, Switch, Button, color, Spinner, config } from 'folds';
|
||||
import { IPusherRequest } from '$types/matrix-sdk';
|
||||
import { useAtom } from 'jotai';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { SequenceCard } from '$components/sequence-card';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
@@ -98,6 +99,7 @@ function EmailNotification() {
|
||||
function WebPushNotificationSetting() {
|
||||
const mx = useMatrixClient();
|
||||
const clientConfig = useClientConfig();
|
||||
const isTauriApp = isTauri();
|
||||
const [isLoading, setIsLoading] = useState<boolean>(true);
|
||||
const [usePushNotifications, setPushNotifications] = useSetting(
|
||||
settingsAtom,
|
||||
@@ -109,7 +111,16 @@ function WebPushNotificationSetting() {
|
||||
useEffect(() => {
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (isTauriApp && usePushNotifications) {
|
||||
setPushNotifications(false);
|
||||
}
|
||||
}, [isTauriApp, usePushNotifications, setPushNotifications]);
|
||||
|
||||
const handleRequestPermissionAndEnable = async () => {
|
||||
if (isTauriApp) return;
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const permissionResult = await requestBrowserNotificationPermission();
|
||||
@@ -123,6 +134,8 @@ function WebPushNotificationSetting() {
|
||||
};
|
||||
|
||||
const handlePushSwitchChange = async (wantsPush: boolean) => {
|
||||
if (isTauriApp && wantsPush) return;
|
||||
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
@@ -141,7 +154,11 @@ function WebPushNotificationSetting() {
|
||||
<SettingTile
|
||||
title="Background Push Notifications"
|
||||
description={
|
||||
browserPermission === 'denied' ? (
|
||||
isTauriApp ? (
|
||||
<Text as="span" style={{ color: color.Warning.Main }} size="T200">
|
||||
Unavailable in Tauri runtime.
|
||||
</Text>
|
||||
) : browserPermission === 'denied' ? (
|
||||
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
|
||||
Permission blocked. Please allow notifications in your browser settings.
|
||||
</Text>
|
||||
@@ -153,11 +170,20 @@ function WebPushNotificationSetting() {
|
||||
isLoading ? (
|
||||
<Spinner variant="Secondary" />
|
||||
) : browserPermission === 'prompt' ? (
|
||||
<Button size="300" radii="300" onClick={handleRequestPermissionAndEnable}>
|
||||
<Button
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={handleRequestPermissionAndEnable}
|
||||
disabled={isTauriApp}
|
||||
>
|
||||
<Text size="B300">Enable</Text>
|
||||
</Button>
|
||||
) : browserPermission === 'granted' ? (
|
||||
<Switch value={usePushNotifications} onChange={handlePushSwitchChange} />
|
||||
<Switch
|
||||
value={usePushNotifications}
|
||||
onChange={handlePushSwitchChange}
|
||||
disabled={isTauriApp && !usePushNotifications}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import { ReactNode, useCallback, useEffect, useRef } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import {
|
||||
MatrixEventEvent,
|
||||
PushProcessor,
|
||||
@@ -124,7 +125,7 @@ function FaviconUpdater() {
|
||||
} else {
|
||||
navigator.clearAppBadge();
|
||||
}
|
||||
if (usePushNotifications) {
|
||||
if (usePushNotifications && registration) {
|
||||
if (total === 0) {
|
||||
// All rooms read — clear every notification.
|
||||
registration.getNotifications().then((notifs) => notifs.forEach((n) => n.close()));
|
||||
@@ -538,7 +539,7 @@ export function HandleNotificationClick() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
if (!('serviceWorker' in navigator)) return undefined;
|
||||
if (!('serviceWorker' in navigator) || isTauri()) return undefined;
|
||||
|
||||
const handleMessage = (ev: MessageEvent) => {
|
||||
const { data } = ev;
|
||||
@@ -594,7 +595,7 @@ function SyncNotificationSettingsWithServiceWorker() {
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
if (!('serviceWorker' in navigator) || isTauri()) return undefined;
|
||||
// notificationSoundEnabled is intentionally excluded: push notification sound
|
||||
// is governed by the push rule's tweakSound alone (OS/Sygnal handles it).
|
||||
// The in-app sound setting only controls the in-page <audio> playback above.
|
||||
|
||||
+17
-17
@@ -1,5 +1,6 @@
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import { enableMapSet } from 'immer';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import '@fontsource-variable/nunito';
|
||||
import '@fontsource-variable/nunito/wght-italic.css';
|
||||
import '@fontsource/space-mono/400.css';
|
||||
@@ -59,21 +60,6 @@ if ('serviceWorker' in navigator) {
|
||||
}
|
||||
};
|
||||
|
||||
navigator.serviceWorker.register(swUrl, swRegisterOptions).then((registration) => {
|
||||
registration.addEventListener('updatefound', () => {
|
||||
const installingWorker = registration.installing;
|
||||
if (installingWorker) {
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
showUpdateAvailablePrompt(registration);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const sendSessionToSW = () => {
|
||||
// Use the active session from the new multi-session store, fall back to legacy
|
||||
const sessions = getLocalStorageItem<Sessions>(MATRIX_SESSIONS_KEY, []);
|
||||
@@ -84,8 +70,22 @@ if ('serviceWorker' in navigator) {
|
||||
};
|
||||
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(sendSessionToSW)
|
||||
.register(swUrl, swRegisterOptions)
|
||||
.then((registration) => {
|
||||
registration.addEventListener('updatefound', () => {
|
||||
const installingWorker = registration.installing;
|
||||
if (installingWorker) {
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (!isTauri() && navigator.serviceWorker.controller) {
|
||||
showUpdateAvailablePrompt(registration);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
sendSessionToSW();
|
||||
})
|
||||
.catch((err) => {
|
||||
log.warn('SW registration failed:', err);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user