From c2e8872bca48a7cb64364de82deb73778d6eddd4 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Tue, 21 Jul 2026 23:15:50 -0500 Subject: [PATCH] style: make updates use shared popup --- .../global-banner/GlobalBannerRenderer.css.ts | 5 +- src/app/pages/client/ClientNonUIFeatures.tsx | 2 + src/app/pages/client/DesktopUpdater.tsx | 139 ++++++++++++++---- src/app/pages/client/WebUpdater.tsx | 62 ++++++++ src/app/state/swUpdate.ts | 3 + src/serviceWorkerBootstrap.ts | 13 +- 6 files changed, 182 insertions(+), 42 deletions(-) create mode 100644 src/app/pages/client/WebUpdater.tsx create mode 100644 src/app/state/swUpdate.ts diff --git a/src/app/components/global-banner/GlobalBannerRenderer.css.ts b/src/app/components/global-banner/GlobalBannerRenderer.css.ts index a8ee18c97..37b9677cf 100644 --- a/src/app/components/global-banner/GlobalBannerRenderer.css.ts +++ b/src/app/components/global-banner/GlobalBannerRenderer.css.ts @@ -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)', diff --git a/src/app/pages/client/ClientNonUIFeatures.tsx b/src/app/pages/client/ClientNonUIFeatures.tsx index 5c373eed0..b39ec1512 100644 --- a/src/app/pages/client/ClientNonUIFeatures.tsx +++ b/src/app/pages/client/ClientNonUIFeatures.tsx @@ -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) { + diff --git a/src/app/pages/client/DesktopUpdater.tsx b/src/app/pages/client/DesktopUpdater.tsx index 88595335b..35e54decd 100644 --- a/src/app/pages/client/DesktopUpdater.tsx +++ b/src/app/pages/client/DesktopUpdater.tsx @@ -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 { - 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(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(() => { + 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; } diff --git a/src/app/pages/client/WebUpdater.tsx b/src/app/pages/client/WebUpdater.tsx new file mode 100644 index 000000000..810143b8d --- /dev/null +++ b/src/app/pages/client/WebUpdater.tsx @@ -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(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(() => { + 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; +} diff --git a/src/app/state/swUpdate.ts b/src/app/state/swUpdate.ts new file mode 100644 index 000000000..ee4c7ea1c --- /dev/null +++ b/src/app/state/swUpdate.ts @@ -0,0 +1,3 @@ +import { atom } from 'jotai'; + +export const swUpdateRegistrationAtom = atom(undefined); diff --git a/src/serviceWorkerBootstrap.ts b/src/serviceWorkerBootstrap.ts index 68b362cbb..5b515f9bf 100644 --- a/src/serviceWorkerBootstrap.ts +++ b/src/serviceWorkerBootstrap.ts @@ -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 = () => {