mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-24 11:01:17 -08:00
style: make updates use shared popup
This commit is contained in:
1 parent
b16f0d7de5
commit
c2e8872bca
6 files changed
+182
-42
No files matched your search
@@ -16,9 +16,10 @@ export const Banner = style({
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
gap: config.space.S300,
|
||||
backgroundColor: 'color-mix(in srgb, var(--sable-surface-var-container, #e4e4e7) 78%, transparent)',
|
||||
backgroundColor:
|
||||
'color-mix(in srgb, var(--sable-surface-var-container, #e4e4e7) 78%, transparent)',
|
||||
color: color.SurfaceVariant.OnContainer,
|
||||
border: `${config.borderWidth.B300} solid color-mix(in srgb, var(--sable-surface-var-container-line, #71717a) 40%, transparent)`,
|
||||
border: `${config.borderWidth.B300} solid color-mix(in srgb, var(--sable-surface-var-container-line, #71717a) 70%, transparent)`,
|
||||
borderRadius: toRem(16),
|
||||
padding: config.space.S400,
|
||||
boxShadow: '0 10px 32px rgba(0, 0, 0, 0.22)',
|
||||
|
||||
@@ -73,6 +73,7 @@ import { incomingCallAtom, mutedCallRoomIdAtom } from '$state/callEmbed';
|
||||
import { getInboxInvitesPath } from '../pathUtils';
|
||||
import { BackgroundNotifications } from './BackgroundNotifications';
|
||||
import { DesktopUpdater } from './DesktopUpdater';
|
||||
import { WebUpdater } from './WebUpdater';
|
||||
import { NotificationTransportRuntimeFeature } from '$features/settings/notifications/NotificationTransportRuntimeFeature';
|
||||
import { UnverifiedNoticeBanner } from '$components/unverified-notice';
|
||||
import { GlobalBannerRenderer } from '$components/global-banner/GlobalBannerRenderer';
|
||||
@@ -1015,6 +1016,7 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
||||
<NativeNotificationClickRouting />
|
||||
<BackgroundNotifications />
|
||||
<DesktopUpdater />
|
||||
<WebUpdater />
|
||||
<NotificationTransportRuntimeFeature />
|
||||
<SyncNotificationSettingsWithServiceWorker />
|
||||
<HandleDecryptPushEvent />
|
||||
|
||||
@@ -1,51 +1,126 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { type as osType } from '@tauri-apps/plugin-os';
|
||||
import type { Update } from '@tauri-apps/plugin-updater';
|
||||
import { autoUpdateCheckAtom } from '$state/autoUpdateCheck';
|
||||
import { createLogger } from '$utils/debug';
|
||||
import { ArrowUp } from '$components/icons/phosphor';
|
||||
import { useRegisterGlobalBanner, type GlobalBanner } from '$state/globalBanners';
|
||||
|
||||
const log = createLogger('DesktopUpdater');
|
||||
|
||||
const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']);
|
||||
const isDesktopTauri = (): boolean => isTauri() && DESKTOP_TAURI_OS.has(osType());
|
||||
|
||||
async function checkForDesktopUpdate(): Promise<void> {
|
||||
const { check } = await import('@tauri-apps/plugin-updater');
|
||||
const update = await check();
|
||||
if (!update) return;
|
||||
|
||||
log.log(`update ${update.version} available`);
|
||||
const { ask } = await import('@tauri-apps/plugin-dialog');
|
||||
|
||||
const shouldInstall = await ask(`Sable ${update.version} is available. Install it now?`, {
|
||||
title: 'Update available',
|
||||
kind: 'info',
|
||||
okLabel: 'Install',
|
||||
cancelLabel: 'Later',
|
||||
});
|
||||
if (!shouldInstall) return;
|
||||
|
||||
await update.downloadAndInstall();
|
||||
|
||||
const { relaunch } = await import('@tauri-apps/plugin-process');
|
||||
const shouldRestart = await ask('Update installed. Restart Sable to finish?', {
|
||||
title: 'Restart required',
|
||||
kind: 'info',
|
||||
okLabel: 'Restart',
|
||||
cancelLabel: 'Later',
|
||||
});
|
||||
if (shouldRestart) await relaunch();
|
||||
}
|
||||
|
||||
export function DesktopUpdater() {
|
||||
const autoUpdateCheck = useAtomValue(autoUpdateCheckAtom);
|
||||
const [updateInfo, setUpdateInfo] = useState<Update | null>(null);
|
||||
const [isInstalling, setIsInstalling] = useState(false);
|
||||
const [isDownloaded, setIsDownloaded] = useState(false);
|
||||
const [dismissed, setDismissed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isDesktopTauri()) return;
|
||||
if (!autoUpdateCheck) return;
|
||||
checkForDesktopUpdate().catch((err) => log.error('update check failed', err));
|
||||
if (!isDesktopTauri()) return undefined;
|
||||
if (!autoUpdateCheck) return undefined;
|
||||
|
||||
let isMounted = true;
|
||||
async function checkUpdate() {
|
||||
try {
|
||||
const { check } = await import('@tauri-apps/plugin-updater');
|
||||
const update = await check();
|
||||
if (update && isMounted) {
|
||||
log.log(`Desktop update ${update.version} available`);
|
||||
setUpdateInfo(update);
|
||||
}
|
||||
} catch (err) {
|
||||
log.error('Desktop update check failed', err);
|
||||
}
|
||||
}
|
||||
|
||||
checkUpdate();
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, [autoUpdateCheck]);
|
||||
|
||||
const handleInstall = useCallback(async () => {
|
||||
if (!updateInfo) return;
|
||||
try {
|
||||
setIsInstalling(true);
|
||||
await updateInfo.downloadAndInstall();
|
||||
setIsInstalling(false);
|
||||
setIsDownloaded(true);
|
||||
} catch (err) {
|
||||
log.error('Failed to download and install update', err);
|
||||
setIsInstalling(false);
|
||||
}
|
||||
}, [updateInfo]);
|
||||
|
||||
const handleRestart = useCallback(async () => {
|
||||
try {
|
||||
const { relaunch } = await import('@tauri-apps/plugin-process');
|
||||
await relaunch();
|
||||
} catch (err) {
|
||||
log.error('Failed to relaunch after update', err);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleDismiss = useCallback(() => {
|
||||
setDismissed(true);
|
||||
}, []);
|
||||
|
||||
const bannerData = useMemo<GlobalBanner | null>(() => {
|
||||
if (!updateInfo || dismissed) return null;
|
||||
|
||||
if (isDownloaded) {
|
||||
return {
|
||||
id: 'desktop-update-restart',
|
||||
priority: 200,
|
||||
icon: ArrowUp,
|
||||
title: 'Update Ready',
|
||||
description: `Sable ${updateInfo.version} has been downloaded. Restart the app to finish updating.`,
|
||||
primaryAction: {
|
||||
label: 'Restart Now',
|
||||
variant: 'Primary',
|
||||
onClick: handleRestart,
|
||||
},
|
||||
secondaryAction: {
|
||||
label: 'Later',
|
||||
variant: 'Secondary',
|
||||
onClick: handleDismiss,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id: 'desktop-update-available',
|
||||
priority: 200,
|
||||
icon: ArrowUp,
|
||||
title: 'Desktop Update Available',
|
||||
description: `Sable ${updateInfo.version} is available for installation.`,
|
||||
primaryAction: {
|
||||
label: isInstalling ? 'Installing...' : 'Install & Update',
|
||||
variant: 'Primary',
|
||||
onClick: handleInstall,
|
||||
},
|
||||
secondaryAction: {
|
||||
label: 'Later',
|
||||
variant: 'Secondary',
|
||||
onClick: handleDismiss,
|
||||
},
|
||||
};
|
||||
}, [
|
||||
updateInfo,
|
||||
dismissed,
|
||||
isDownloaded,
|
||||
isInstalling,
|
||||
handleInstall,
|
||||
handleRestart,
|
||||
handleDismiss,
|
||||
]);
|
||||
|
||||
useRegisterGlobalBanner(bannerData);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { ArrowUp } from '$components/icons/phosphor';
|
||||
import { useRegisterGlobalBanner, type GlobalBanner } from '$state/globalBanners';
|
||||
import { createLogger } from '$utils/debug';
|
||||
|
||||
const log = createLogger('WebUpdater');
|
||||
|
||||
export function WebUpdater() {
|
||||
const [registration, setRegistration] = useState<ServiceWorkerRegistration | null>(null);
|
||||
const [dismissed, setDismissed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const handleUpdate = (ev: Event) => {
|
||||
const customEv = ev as CustomEvent<{ registration: ServiceWorkerRegistration }>;
|
||||
if (customEv.detail?.registration) {
|
||||
log.log('Web update available via ServiceWorker');
|
||||
setRegistration(customEv.detail.registration);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('sable-sw-update-available', handleUpdate);
|
||||
return () => window.removeEventListener('sable-sw-update-available', handleUpdate);
|
||||
}, []);
|
||||
|
||||
const handleRefresh = useCallback(() => {
|
||||
if (registration?.waiting) {
|
||||
// oxlint-disable-next-line unicorn/require-post-message-target-origin
|
||||
registration.waiting.postMessage({ type: 'SKIP_WAITING_AND_CLAIM' });
|
||||
}
|
||||
window.location.reload();
|
||||
}, [registration]);
|
||||
|
||||
const handleDismiss = useCallback(() => {
|
||||
setDismissed(true);
|
||||
}, []);
|
||||
|
||||
const bannerData = useMemo<GlobalBanner | null>(() => {
|
||||
if (!registration || dismissed) return null;
|
||||
|
||||
return {
|
||||
id: 'web-app-update',
|
||||
priority: 200, // Top priority for updates
|
||||
icon: ArrowUp,
|
||||
title: 'Update Available',
|
||||
description: 'A new version of Sable is available. Refresh to apply updates.',
|
||||
primaryAction: {
|
||||
label: 'Refresh',
|
||||
variant: 'Primary',
|
||||
onClick: handleRefresh,
|
||||
},
|
||||
secondaryAction: {
|
||||
label: 'Later',
|
||||
variant: 'Secondary',
|
||||
onClick: handleDismiss,
|
||||
},
|
||||
};
|
||||
}, [registration, dismissed, handleRefresh, handleDismiss]);
|
||||
|
||||
useRegisterGlobalBanner(bannerData);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { atom } from 'jotai';
|
||||
|
||||
export const swUpdateRegistrationAtom = atom<ServiceWorkerRegistration | undefined>(undefined);
|
||||
@@ -16,14 +16,11 @@ const showUpdateAvailablePrompt = (registration: ServiceWorkerRegistration) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-alert
|
||||
if (window.confirm('A new version of the app is available. Refresh to update?')) {
|
||||
if (registration.waiting) {
|
||||
// oxlint-disable-next-line unicorn/require-post-message-target-origin
|
||||
registration.waiting.postMessage({ type: 'SKIP_WAITING_AND_CLAIM' });
|
||||
}
|
||||
window.location.reload();
|
||||
}
|
||||
window.dispatchEvent(
|
||||
new CustomEvent<{ registration: ServiceWorkerRegistration }>('sable-sw-update-available', {
|
||||
detail: { registration },
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const sendSessionToSW = () => {
|
||||
|
||||
Reference in new issue
Block a user