From a8839633146846b1bc77b22e2c3679cc93ff1189 Mon Sep 17 00:00:00 2001 From: Gigiaj Date: Sun, 8 Jun 2025 20:15:45 -0500 Subject: [PATCH] Adds the client state listener for toggling whether to use push notifs or app notifs --- src/app/pages/client/ClientRoot.tsx | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/app/pages/client/ClientRoot.tsx b/src/app/pages/client/ClientRoot.tsx index 846d8ff3..8eac63bd 100644 --- a/src/app/pages/client/ClientRoot.tsx +++ b/src/app/pages/client/ClientRoot.tsx @@ -37,6 +37,7 @@ import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback'; import { useSyncState } from '../../hooks/useSyncState'; import { stopPropagation } from '../../utils/keyboard'; import { SyncStatus } from './SyncStatus'; +import { togglePusher } from '../../features/settings/notifications/PushNotifications'; function ClientRootLoading() { return ( @@ -124,6 +125,18 @@ function ClientRootOptions({ mx }: { mx?: MatrixClient }) { ); } +const pushNotificationListener = (mx: MatrixClient) => { + navigator.serviceWorker.ready.then((registration) => { + registration.pushManager.getSubscription().then((subscription) => { + document.addEventListener('visibilitychange', () => { + console.log('Check check baby'); + togglePusher(mx, subscription, document.visibilityState === 'visible'); + }); + togglePusher(mx, subscription, true); + }); + }); +}; + const useLogoutListener = (mx?: MatrixClient) => { useEffect(() => { const handleLogout: HttpApiEventHandlerMap[HttpApiEvent.SessionLoggedOut] = async () => { @@ -171,13 +184,16 @@ export function ClientRoot({ children }: ClientRootProps) { useSyncState( mx, - useCallback((state) => { - if (state === 'PREPARED') { - setLoading(false); - } - }, []) + useCallback( + (state) => { + if (state === 'PREPARED') { + setLoading(false); + pushNotificationListener(mx); + } + }, + [mx] + ) ); - return ( {mx && }