mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 09:31:17 -08:00
140 lines
5.0 KiB
TypeScript
140 lines
5.0 KiB
TypeScript
import { useEffect, useMemo, useRef } from 'react';
|
|
import { isTauri } from '@tauri-apps/api/core';
|
|
import { createLogger } from '$utils/debug';
|
|
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 } from './UnifiedPushNotifications';
|
|
import { enableNativePush } from './NativePushNotifications';
|
|
import {
|
|
NotificationTransportRuntime,
|
|
type NotificationTransportRuntimeContext,
|
|
} from './NotificationTransportRuntime';
|
|
import {
|
|
type NotificationTransportPlatform,
|
|
normalizeNotificationTransportMode,
|
|
resolvePreferredNotificationTransportProvider,
|
|
} from './NotificationTransport';
|
|
|
|
const log = createLogger('NotificationTransportRuntimeFeature');
|
|
|
|
function currentPlatform(): NotificationTransportPlatform {
|
|
if (!isTauri()) return 'web';
|
|
const platform = osType();
|
|
if (platform === 'android') return 'android';
|
|
if (platform === 'ios') return 'ios';
|
|
return 'desktop';
|
|
}
|
|
|
|
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');
|
|
const [showEncryptedMessageContent] = useSetting(
|
|
settingsAtom,
|
|
'showMessageContentInEncryptedNotifications'
|
|
);
|
|
|
|
const runtimeRef = useRef<NotificationTransportRuntime>();
|
|
if (!runtimeRef.current) runtimeRef.current = new NotificationTransportRuntime();
|
|
|
|
// Read fresh by the listener for each incoming push, so toggling display
|
|
// settings doesn't tear the listener down.
|
|
const contextRef = useRef<NotificationTransportRuntimeContext>({
|
|
mx,
|
|
showMessageContent,
|
|
showEncryptedMessageContent,
|
|
notificationSoundEnabled: isNotificationSounds,
|
|
useInAppNotifications,
|
|
});
|
|
contextRef.current = {
|
|
mx,
|
|
showMessageContent,
|
|
showEncryptedMessageContent,
|
|
notificationSoundEnabled: isNotificationSounds,
|
|
useInAppNotifications,
|
|
};
|
|
|
|
const platform = currentPlatform();
|
|
const provider = useMemo(() => {
|
|
if (!backgroundPushEnabled) return null;
|
|
const preferred = resolvePreferredNotificationTransportProvider(
|
|
normalizeNotificationTransportMode(pushTransportMode, platform),
|
|
platform
|
|
);
|
|
return backgroundPushProvider ?? preferred;
|
|
}, [backgroundPushEnabled, backgroundPushProvider, pushTransportMode, platform]);
|
|
|
|
useEffect(() => {
|
|
void runtimeRef.current?.sync(provider, () => contextRef.current);
|
|
}, [provider]);
|
|
|
|
const upConfigRef = useRef<{
|
|
unifiedPushAppID?: string;
|
|
unifiedPushGatewayUrl?: string;
|
|
vapidPublicKey?: string;
|
|
webPushAppID?: string;
|
|
pushNotifyUrl?: string;
|
|
}>({});
|
|
upConfigRef.current = {
|
|
unifiedPushAppID:
|
|
pushTransportOverride?.unifiedPushAppID ??
|
|
clientConfig.pushNotificationDetails?.unifiedPushAppID,
|
|
unifiedPushGatewayUrl:
|
|
pushTransportOverride?.unifiedPushGatewayUrl ??
|
|
clientConfig.pushNotificationDetails?.unifiedPushGatewayUrl,
|
|
vapidPublicKey: clientConfig.pushNotificationDetails?.vapidPublicKey,
|
|
webPushAppID: clientConfig.pushNotificationDetails?.webPushAppID,
|
|
pushNotifyUrl: clientConfig.pushNotificationDetails?.pushNotifyUrl,
|
|
};
|
|
|
|
// 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 clientConfigRef = useRef(clientConfig);
|
|
clientConfigRef.current = clientConfig;
|
|
|
|
const lastEndpointRef = useRef<string | null>(null);
|
|
useEffect(() => {
|
|
if (!mx) return undefined;
|
|
|
|
if (provider === 'unifiedpush') {
|
|
enableUnifiedPush(mx, upConfigRef.current)
|
|
.then((result) => {
|
|
lastEndpointRef.current = result.endpoint;
|
|
})
|
|
.catch((error) => {
|
|
log.warn('UnifiedPush pusher registration failed at startup', error);
|
|
});
|
|
} else if (provider === 'native') {
|
|
enableNativePush(mx, clientConfigRef.current)
|
|
.then((token) => {
|
|
lastEndpointRef.current = token;
|
|
})
|
|
.catch((error) => {
|
|
log.warn('Native push pusher registration failed at startup', error);
|
|
});
|
|
}
|
|
|
|
return () => {};
|
|
}, [provider, mx]);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
void runtimeRef.current?.dispose();
|
|
},
|
|
[]
|
|
);
|
|
|
|
return null;
|
|
}
|