fix(notifications): establish and refresh the unifiedpush pusher via endpoint lifecycle

This commit is contained in:
Erwan Leboucher committed 2026-07-17 21:38:13 +02:00
1 parent 7fbb64e02b
commit 2da28be481
1 file changed
+49
@@ -2,8 +2,10 @@ import { useEffect, useMemo, useRef } from 'react';
import { isTauri } from '@tauri-apps/api/core';
import { type as osType } from '@tauri-apps/plugin-os';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useClientConfig } from '$hooks/useClientConfig';
import { useSetting } from '$state/hooks/settings';
import { settingsAtom } from '$state/settings';
import { enableUnifiedPush, listenForUnifiedPushEndpointChanges } from './UnifiedPushNotifications';
import {
NotificationTransportRuntime,
type NotificationTransportRuntimeContext,
@@ -24,9 +26,11 @@ function currentPlatform(): NotificationTransportPlatform {
export function NotificationTransportRuntimeFeature() {
const mx = useMatrixClient();
const clientConfig = useClientConfig();
const [backgroundPushEnabled] = useSetting(settingsAtom, 'backgroundPushEnabled');
const [backgroundPushProvider] = useSetting(settingsAtom, 'backgroundPushProvider');
const [pushTransportMode] = useSetting(settingsAtom, 'pushTransportMode');
const [pushTransportOverride] = useSetting(settingsAtom, 'pushTransportOverride');
const [useInAppNotifications] = useSetting(settingsAtom, 'useInAppNotifications');
const [isNotificationSounds] = useSetting(settingsAtom, 'isNotificationSounds');
const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications');
@@ -69,6 +73,51 @@ export function NotificationTransportRuntimeFeature() {
void runtimeRef.current?.sync(provider, () => contextRef.current);
}, [provider]);
const upConfigRef = useRef<{ unifiedPushAppID?: string; unifiedPushGatewayUrl?: string }>({});
upConfigRef.current = {
unifiedPushAppID:
pushTransportOverride?.unifiedPushAppID ??
clientConfig.pushNotificationDetails?.unifiedPushAppID,
unifiedPushGatewayUrl:
pushTransportOverride?.unifiedPushGatewayUrl ??
clientConfig.pushNotificationDetails?.unifiedPushGatewayUrl,
};
// Keep the pusher current: establish it when UnifiedPush becomes the active
// transport (so it survives a fresh install/session, not just a manual toggle)
// and refresh it whenever the distributor rotates the endpoint. Deduped by
// endpoint so re-registration can't loop.
const lastEndpointRef = useRef<string | null>(null);
useEffect(() => {
if (provider !== 'unifiedpush' || !mx) return undefined;
const establish = () => {
enableUnifiedPush(mx, upConfigRef.current)
.then((result) => {
lastEndpointRef.current = result.endpoint;
})
.catch(() => undefined);
};
establish();
let cancelled = false;
let listener: { unregister: () => Promise<void> | void } | undefined;
void listenForUnifiedPushEndpointChanges((endpoint) => {
if (endpoint === lastEndpointRef.current) return;
lastEndpointRef.current = endpoint;
establish();
}).then((handle) => {
if (cancelled) void handle.unregister();
else listener = handle;
});
return () => {
cancelled = true;
void listener?.unregister();
};
}, [provider, mx]);
useEffect(
() => () => {
void runtimeRef.current?.dispose();