mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
feat: add desktop auto-update pill with download progress and manual check
Signed-off-by: hazre <[email protected]>
This commit is contained in:
1 parent
c83522d7e6
commit
1c0950faee
13 files changed
+365
-49
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: minor
|
||||
---
|
||||
|
||||
Desktop app now periodically checks for updates, shows an update pill in the titlebar when one is available, and lets you manually check for updates in Settings > About.
|
||||
@@ -1,3 +1,4 @@
|
||||
import React from 'react';
|
||||
import classNames from 'classnames';
|
||||
import { Box, config, Text } from 'folds';
|
||||
import { AnimatePresence, motion, useReducedMotion, type Variants } from 'framer-motion';
|
||||
@@ -10,6 +11,8 @@ const TITLEBAR_EASE_OUT_SOFT: [number, number, number, number] = [0.24, 0.72, 0.
|
||||
|
||||
type SyncConnectionStatusProps = {
|
||||
status: TitlebarStatusView | null;
|
||||
onClick?: () => void;
|
||||
icon?: React.ReactNode;
|
||||
};
|
||||
|
||||
export function getSyncConnectionStatusView(
|
||||
@@ -157,7 +160,7 @@ export function SyncConnectionStatusBanner({ status }: SyncConnectionStatusProps
|
||||
);
|
||||
}
|
||||
|
||||
export function SyncConnectionStatusTitlebar({ status }: SyncConnectionStatusProps) {
|
||||
export function SyncConnectionStatusTitlebar({ status, onClick, icon }: SyncConnectionStatusProps) {
|
||||
const shouldReduceMotion = useReducedMotion();
|
||||
const progress = status?.progress;
|
||||
const pillVariants = shouldReduceMotion
|
||||
@@ -233,10 +236,12 @@ export function SyncConnectionStatusTitlebar({ status }: SyncConnectionStatusPro
|
||||
};
|
||||
|
||||
return (
|
||||
<AnimatePresence mode="sync" initial={false}>
|
||||
<AnimatePresence mode="popLayout" initial={false}>
|
||||
{status && (
|
||||
<motion.span
|
||||
key={status.variant}
|
||||
role={onClick ? 'button' : undefined}
|
||||
tabIndex={onClick ? 0 : undefined}
|
||||
className={classNames(
|
||||
'tauri-titlebar-status__label',
|
||||
status.variant === 'Success' && 'tauri-titlebar-status__label--success',
|
||||
@@ -244,11 +249,23 @@ export function SyncConnectionStatusTitlebar({ status }: SyncConnectionStatusPro
|
||||
status.variant === 'Critical' && 'tauri-titlebar-status__label--critical',
|
||||
status.variant === 'Primary' && 'tauri-titlebar-status__label--primary'
|
||||
)}
|
||||
onClick={onClick}
|
||||
onKeyDown={
|
||||
onClick
|
||||
? (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault();
|
||||
onClick();
|
||||
}
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
style={{
|
||||
transformOrigin: 'center top',
|
||||
position: 'relative',
|
||||
overflow: 'hidden',
|
||||
willChange: shouldReduceMotion ? 'opacity' : 'transform, opacity, clip-path',
|
||||
cursor: onClick ? 'pointer' : undefined,
|
||||
}}
|
||||
variants={pillVariants}
|
||||
initial="hidden"
|
||||
@@ -256,11 +273,20 @@ export function SyncConnectionStatusTitlebar({ status }: SyncConnectionStatusPro
|
||||
exit="exit"
|
||||
>
|
||||
<motion.span
|
||||
className="tauri-titlebar-status__text"
|
||||
style={{ willChange: 'opacity' }}
|
||||
style={{
|
||||
display: 'inline-flex',
|
||||
alignItems: 'center',
|
||||
gap: '4px',
|
||||
willChange: 'opacity',
|
||||
}}
|
||||
variants={textVariants}
|
||||
>
|
||||
{status.text}
|
||||
{icon && (
|
||||
<span style={{ display: 'flex' }} aria-hidden>
|
||||
{icon}
|
||||
</span>
|
||||
)}
|
||||
<span className="tauri-titlebar-status__text">{status.text}</span>
|
||||
</motion.span>
|
||||
{progress !== undefined && (
|
||||
<div
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
DotsThreeOutlineVerticalIcon,
|
||||
DotsSixVerticalIcon,
|
||||
DownloadIcon,
|
||||
DownloadSimpleIcon,
|
||||
EnvelopeSimpleIcon,
|
||||
EyeIcon,
|
||||
EyeSlashIcon,
|
||||
@@ -346,6 +347,7 @@ export {
|
||||
DotsThreeOutlineVerticalIcon,
|
||||
DotsSixVerticalIcon,
|
||||
DownloadIcon as Download,
|
||||
DownloadSimpleIcon as DownloadSimple,
|
||||
EnvelopeSimpleIcon as EnvelopeSimple,
|
||||
EyeIcon as Eye,
|
||||
EyeSlashIcon as EyeSlash,
|
||||
|
||||
@@ -8,6 +8,7 @@ import { useAtomValue } from 'jotai';
|
||||
import { createLogger } from '$utils/debug';
|
||||
import { titlebarStatusAtom } from '$state/titlebarStatus';
|
||||
import { SyncConnectionStatusTitlebar } from '$components/SyncConnectionStatus';
|
||||
import { DesktopUpdatePill } from './DesktopUpdatePill';
|
||||
import {
|
||||
hideSnapOverlay as hideSnapOverlayCommand,
|
||||
showSnapOverlay as showSnapOverlayCommand,
|
||||
@@ -271,6 +272,9 @@ export function DesktopTitleBar() {
|
||||
<SyncConnectionStatusTitlebar status={titlebarStatus} />
|
||||
</div>
|
||||
|
||||
<div className="tauri-titlebar__update">
|
||||
<DesktopUpdatePill />
|
||||
</div>
|
||||
<div className="tauri-titlebar__controls">
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import { hasCustomDesktopTitlebar } from '$utils/tauriTitlebar';
|
||||
import { useDesktopSetting } from '$state/hooks/desktopSettings';
|
||||
import { updatePhaseAtom, updateBannerVisibleAtom } from '$state/desktopUpdate';
|
||||
import type { UpdatePhase } from '$state/desktopUpdate';
|
||||
import type { TitlebarStatusView } from '$state/titlebarStatus';
|
||||
import { SyncConnectionStatusTitlebar } from '$components/SyncConnectionStatus';
|
||||
import { DownloadSimple } from '$components/icons/phosphor';
|
||||
|
||||
function phaseToStatusView(phase: UpdatePhase): TitlebarStatusView | null {
|
||||
switch (phase.type) {
|
||||
case 'downloading':
|
||||
return { text: 'Update Downloading', variant: 'Warning', progress: phase.progress };
|
||||
case 'ready':
|
||||
return { text: 'Update Available', variant: 'Success' };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function DesktopUpdatePill() {
|
||||
const phase = useAtomValue(updatePhaseAtom);
|
||||
const [bannerVisible, setBannerVisible] = useAtom(updateBannerVisibleAtom);
|
||||
const [useCustomTitleBar] = useDesktopSetting('useCustomTitleBar');
|
||||
const status = !bannerVisible ? phaseToStatusView(phase) : null;
|
||||
|
||||
if (!hasCustomDesktopTitlebar(useCustomTitleBar)) return null;
|
||||
|
||||
return (
|
||||
<SyncConnectionStatusTitlebar
|
||||
status={status}
|
||||
onClick={() => setBannerVisible(true)}
|
||||
icon={<DownloadSimple size={12} />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { type as osType } from '@tauri-apps/plugin-os';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { titlebarStatusAtom } from '$state/titlebarStatus';
|
||||
import { SyncConnectionStatusTitlebar } from '$components/SyncConnectionStatus';
|
||||
import { DesktopUpdatePill } from './DesktopUpdatePill';
|
||||
|
||||
// Draggable strip that reserves space for the native traffic lights (floating
|
||||
// over the content via the transparent titlebar) and hosts the sync pill.
|
||||
@@ -13,10 +14,17 @@ export function MacTitleBar() {
|
||||
if (!isMac) return null;
|
||||
|
||||
return (
|
||||
<nav className="tauri-titlebar tauri-titlebar--mac" data-tauri-drag-region>
|
||||
<nav
|
||||
className="tauri-titlebar tauri-titlebar--mac"
|
||||
data-tauri-drag-region
|
||||
style={{ position: 'relative' }}
|
||||
>
|
||||
<div className="tauri-titlebar__status" data-tauri-drag-region>
|
||||
<SyncConnectionStatusTitlebar status={titlebarStatus} />
|
||||
</div>
|
||||
<div className="tauri-titlebar__update">
|
||||
<DesktopUpdatePill />
|
||||
</div>
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { Box, Text, Scroll, Button, config, toRem, Spinner } from 'folds';
|
||||
import { Code, Heart, menuIcon } from '$components/icons/phosphor';
|
||||
import { PageContent } from '$components/page';
|
||||
@@ -10,6 +10,15 @@ import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { SequenceCardStyle } from '$features/settings/styles.css';
|
||||
import { Method } from '$types/matrix-sdk';
|
||||
import { useOpenBugReportModal } from '$state/hooks/bugReportModal';
|
||||
import { isDesktopTauri } from '$utils/platform';
|
||||
import {
|
||||
updatePhaseAtom,
|
||||
updateBannerVisibleAtom,
|
||||
triggerUpdateCheckAtom,
|
||||
desktopUpdateLastCheckedAtom,
|
||||
} from '$state/desktopUpdate';
|
||||
import dayjs from 'dayjs';
|
||||
import { useAtomValue, useSetAtom } from 'jotai';
|
||||
import { SettingsSectionPage } from '../SettingsSectionPage';
|
||||
|
||||
type VersionResult =
|
||||
@@ -180,6 +189,60 @@ export function About({ requestBack, requestClose }: Readonly<AboutProps>) {
|
||||
const devLabel = IS_RELEASE_TAG ? '' : '-dev';
|
||||
const buildLabel = BUILD_HASH ? ` (${BUILD_HASH})` : '';
|
||||
const openBugReport = useOpenBugReportModal();
|
||||
const updatePhase = useAtomValue(updatePhaseAtom);
|
||||
const setBannerVisible = useSetAtom(updateBannerVisibleAtom);
|
||||
const triggerCheck = useSetAtom(triggerUpdateCheckAtom);
|
||||
const lastChecked = useAtomValue(desktopUpdateLastCheckedAtom);
|
||||
const [checking, setChecking] = useState(false);
|
||||
const [resultText, setResultText] = useState<string | null>(null);
|
||||
const resultTimerRef = useRef<number | null>(null);
|
||||
|
||||
const lastCheckedText = lastChecked
|
||||
? `Last checked: ${dayjs(lastChecked).format('HH:mm')}`
|
||||
: null;
|
||||
|
||||
const clearResultTimer = useCallback(() => {
|
||||
if (resultTimerRef.current !== null) {
|
||||
clearTimeout(resultTimerRef.current);
|
||||
resultTimerRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const showResult = useCallback(
|
||||
(text: string) => {
|
||||
setChecking(false);
|
||||
setResultText(text);
|
||||
clearResultTimer();
|
||||
resultTimerRef.current = window.setTimeout(() => {
|
||||
setResultText(null);
|
||||
resultTimerRef.current = null;
|
||||
}, 3000);
|
||||
},
|
||||
[clearResultTimer]
|
||||
);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
clearResultTimer();
|
||||
},
|
||||
[clearResultTimer]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!checking) return;
|
||||
if (updatePhase.type === 'ready') {
|
||||
showResult(`Update ${updatePhase.version} ready!`);
|
||||
setBannerVisible(true);
|
||||
} else if (updatePhase.type === 'idle') {
|
||||
showResult('Up to date');
|
||||
}
|
||||
}, [updatePhase, checking, showResult, setBannerVisible]);
|
||||
|
||||
const handleCheckForUpdates = useCallback(() => {
|
||||
setChecking(true);
|
||||
setResultText(null);
|
||||
triggerCheck((n) => n + 1);
|
||||
}, [triggerCheck]);
|
||||
|
||||
return (
|
||||
<SettingsSectionPage title="About" requestBack={requestBack} requestClose={requestClose}>
|
||||
@@ -236,6 +299,34 @@ export function About({ requestBack, requestClose }: Readonly<AboutProps>) {
|
||||
</Box>
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Options</Text>
|
||||
{isDesktopTauri() && (
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Check for Updates"
|
||||
focusId="check-for-updates"
|
||||
description={`${resultText || 'Check for a new version.'}${lastCheckedText ? ` ${lastCheckedText}` : ''}`}
|
||||
after={
|
||||
<Button
|
||||
onClick={handleCheckForUpdates}
|
||||
variant="Secondary"
|
||||
fill="Soft"
|
||||
size="300"
|
||||
radii="300"
|
||||
outlined
|
||||
disabled={checking}
|
||||
before={checking ? <Spinner variant="Secondary" size="300" /> : undefined}
|
||||
>
|
||||
<Text size="B300">Check</Text>
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
)}
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { addPluginListener, invoke, isTauri } from '@tauri-apps/api/core';
|
||||
import { type as osType } from '@tauri-apps/plugin-os';
|
||||
|
||||
import { isDesktopTauri } from '$utils/platform';
|
||||
export type NotificationPluginListener = {
|
||||
unregister: () => Promise<void> | void;
|
||||
};
|
||||
@@ -135,10 +135,6 @@ export async function ensureTauriNotificationPermission(): Promise<boolean> {
|
||||
return permissionPromise;
|
||||
}
|
||||
|
||||
// Desktop webviews can't show web notifications (WKWebView lacks the API; the
|
||||
// Linux CEF runtime never grants it), so desktop routes through the native plugin.
|
||||
const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']);
|
||||
export const isDesktopTauri = (): boolean => isTauri() && DESKTOP_TAURI_OS.has(osType());
|
||||
export const isIosTauri = (): boolean => isTauri() && osType() === 'ios';
|
||||
export const isAndroidTauri = (): boolean => isTauri() && osType() === 'android';
|
||||
// Platforms where OS notifications go through the native plugin instead of web APIs.
|
||||
@@ -183,3 +179,5 @@ export async function sendNativeTauriNotification({
|
||||
...(icon ? { icon } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
export { isDesktopTauri };
|
||||
@@ -218,6 +218,7 @@ const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly string[]
|
||||
],
|
||||
about: [
|
||||
'base-url',
|
||||
'check-for-updates',
|
||||
'clear-cache-and-reload',
|
||||
'domain',
|
||||
'federation-url',
|
||||
|
||||
@@ -44,9 +44,8 @@ import {
|
||||
import * as Sentry from '@sentry/react';
|
||||
import { startClient, stopClient } from '$client/initMatrix';
|
||||
import { createSessionTokenRefresher } from '$client/oidcTokenRefresher';
|
||||
import { isDesktopTauri } from '$utils/platform';
|
||||
import { mobileOrTablet } from '$utils/user-agent';
|
||||
import { isTauri } from '@tauri-apps/api/core';
|
||||
import { type as osType } from '@tauri-apps/plugin-os';
|
||||
|
||||
const log = createLogger('BackgroundNotifications');
|
||||
const debugLog = createDebugLogger('BackgroundNotifications');
|
||||
@@ -54,11 +53,6 @@ const debugLog = createDebugLogger('BackgroundNotifications');
|
||||
const BACKGROUND_SYNC_POLL_TIMEOUT_MS = 60_000;
|
||||
const BACKGROUND_STAGGER_DELAY_MS = 5_000;
|
||||
|
||||
// Desktop webviews can't show web notifications (WKWebView lacks the API; the
|
||||
// Linux CEF runtime never grants it), so desktop routes through the native plugin.
|
||||
const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']);
|
||||
const isDesktopTauri = (): boolean => isTauri() && DESKTOP_TAURI_OS.has(osType());
|
||||
|
||||
let desktopNotificationSeq = 1;
|
||||
const nextDesktopNotificationId = (): number => {
|
||||
const id = desktopNotificationSeq;
|
||||
|
||||
@@ -1,61 +1,162 @@
|
||||
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 { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import type { Update } from '@tauri-apps/plugin-updater';
|
||||
import { isDesktopTauri } from '$utils/platform';
|
||||
import { autoUpdateCheckAtom } from '$state/autoUpdateCheck';
|
||||
import { createLogger } from '$utils/debug';
|
||||
import { hasCustomDesktopTitlebar } from '$utils/tauriTitlebar';
|
||||
import { useDesktopSetting } from '$state/hooks/desktopSettings';
|
||||
import { ArrowUp } from '$components/icons/phosphor';
|
||||
import { useRegisterGlobalBanner, type GlobalBanner } from '$state/globalBanners';
|
||||
import {
|
||||
updatePhaseAtom,
|
||||
updateBannerVisibleAtom,
|
||||
triggerUpdateCheckAtom,
|
||||
desktopUpdateLastCheckedAtom,
|
||||
fakeDesktopUpdate,
|
||||
} from '$state/desktopUpdate';
|
||||
|
||||
const log = createLogger('DesktopUpdater');
|
||||
|
||||
const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']);
|
||||
const isDesktopTauri = (): boolean => isTauri() && DESKTOP_TAURI_OS.has(osType());
|
||||
const UPDATE_POLL_INTERVAL_MS = 300_000; // 5 minutes
|
||||
|
||||
export function DesktopUpdater() {
|
||||
const autoUpdateCheck = useAtomValue(autoUpdateCheckAtom);
|
||||
const triggerCount = useAtomValue(triggerUpdateCheckAtom);
|
||||
const setPhase = useSetAtom(updatePhaseAtom);
|
||||
const [bannerVisible, setBannerVisible] = useAtom(updateBannerVisibleAtom);
|
||||
const setLastChecked = useSetAtom(desktopUpdateLastCheckedAtom);
|
||||
const [updateInfo, setUpdateInfo] = useState<Update | null>(null);
|
||||
const [isInstalling, setIsInstalling] = useState(false);
|
||||
const [isDownloaded, setIsDownloaded] = useState(false);
|
||||
const [isInstalled, setIsInstalled] = useState(false);
|
||||
const [isInstalling, setIsInstalling] = useState(false);
|
||||
const [dismissed, setDismissed] = useState(false);
|
||||
const [useCustomTitleBar] = useDesktopSetting('useCustomTitleBar');
|
||||
const hasUpdateRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isDesktopTauri()) return undefined;
|
||||
if (!autoUpdateCheck) return undefined;
|
||||
if (triggerCount === 0 && !autoUpdateCheck && !fakeDesktopUpdate()) return undefined;
|
||||
|
||||
let isMounted = true;
|
||||
async function checkUpdate() {
|
||||
let mounted = true;
|
||||
|
||||
setPhase({ type: 'checking' });
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
if (fakeDesktopUpdate()) {
|
||||
log.log('Fake update: simulating download');
|
||||
setUpdateInfo({ version: '9.9.9', body: 'Fake changelog for testing.' } as Update);
|
||||
setPhase({ type: 'downloading', progress: 0 });
|
||||
for (let pct = 0; pct <= 100; pct += 2) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((r) => setTimeout(r, 40));
|
||||
if (!mounted) return;
|
||||
setPhase({ type: 'downloading', progress: pct });
|
||||
}
|
||||
if (!mounted) return;
|
||||
setIsDownloaded(true);
|
||||
setPhase({ type: 'ready', version: '9.9.9' });
|
||||
setLastChecked(new Date().toISOString());
|
||||
if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const { check } = await import('@tauri-apps/plugin-updater');
|
||||
const update = await check();
|
||||
if (update && isMounted) {
|
||||
log.log(`Desktop update ${update.version} available`);
|
||||
setUpdateInfo(update);
|
||||
if (!mounted) return;
|
||||
|
||||
if (!update) {
|
||||
if (!hasUpdateRef.current) {
|
||||
setPhase({ type: 'idle' });
|
||||
}
|
||||
setLastChecked(new Date().toISOString());
|
||||
return;
|
||||
}
|
||||
|
||||
log.log(`Desktop update ${update.version} available, downloading...`);
|
||||
setUpdateInfo(update);
|
||||
setIsInstalled(false);
|
||||
setDismissed(false);
|
||||
hasUpdateRef.current = true;
|
||||
setPhase({ type: 'downloading', progress: 0 });
|
||||
|
||||
let downloadedBytes = 0;
|
||||
let contentLength = 0;
|
||||
await update.download((event) => {
|
||||
if (event.event === 'Started') {
|
||||
contentLength = event.data.contentLength ?? 0;
|
||||
} else if (event.event === 'Progress') {
|
||||
downloadedBytes += event.data.chunkLength;
|
||||
const pct = contentLength > 0 ? Math.round((downloadedBytes / contentLength) * 100) : 0;
|
||||
setPhase({ type: 'downloading', progress: Math.min(pct, 100) });
|
||||
}
|
||||
});
|
||||
|
||||
if (!mounted) return;
|
||||
log.log(`Update ${update.version} downloaded`);
|
||||
setIsDownloaded(true);
|
||||
setPhase({ type: 'ready', version: update.version });
|
||||
setLastChecked(new Date().toISOString());
|
||||
if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true);
|
||||
} catch (err) {
|
||||
log.error('Desktop update check failed', err);
|
||||
if (mounted) {
|
||||
if (!hasUpdateRef.current) {
|
||||
setPhase({ type: 'idle' });
|
||||
}
|
||||
setLastChecked(new Date().toISOString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkUpdate();
|
||||
run();
|
||||
|
||||
if (autoUpdateCheck) {
|
||||
const interval = setInterval(run, UPDATE_POLL_INTERVAL_MS);
|
||||
return () => {
|
||||
mounted = false;
|
||||
clearInterval(interval);
|
||||
};
|
||||
}
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
mounted = false;
|
||||
};
|
||||
}, [autoUpdateCheck]);
|
||||
}, [
|
||||
autoUpdateCheck,
|
||||
triggerCount,
|
||||
setPhase,
|
||||
setBannerVisible,
|
||||
setLastChecked,
|
||||
useCustomTitleBar,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (bannerVisible && dismissed) {
|
||||
setDismissed(false);
|
||||
}
|
||||
}, [bannerVisible, dismissed]);
|
||||
|
||||
const handleInstall = useCallback(async () => {
|
||||
if (!updateInfo) return;
|
||||
try {
|
||||
setIsInstalling(true);
|
||||
await updateInfo.downloadAndInstall();
|
||||
setPhase({ type: 'installing' });
|
||||
|
||||
if (fakeDesktopUpdate()) {
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
} else {
|
||||
await updateInfo.install();
|
||||
}
|
||||
|
||||
setPhase({ type: 'ready', version: updateInfo.version });
|
||||
setIsInstalling(false);
|
||||
setIsDownloaded(true);
|
||||
setIsInstalled(true);
|
||||
} catch (err) {
|
||||
log.error('Failed to download and install update', err);
|
||||
log.error('Failed to install update', err);
|
||||
setIsInstalling(false);
|
||||
}
|
||||
}, [updateInfo]);
|
||||
}, [updateInfo, setPhase]);
|
||||
|
||||
const handleRestart = useCallback(async () => {
|
||||
try {
|
||||
@@ -68,18 +169,19 @@ export function DesktopUpdater() {
|
||||
|
||||
const handleDismiss = useCallback(() => {
|
||||
setDismissed(true);
|
||||
}, []);
|
||||
setBannerVisible(false);
|
||||
}, [setBannerVisible]);
|
||||
|
||||
const bannerData = useMemo<GlobalBanner | null>(() => {
|
||||
if (!updateInfo || dismissed) return null;
|
||||
if (!bannerVisible || !updateInfo || dismissed) return null;
|
||||
|
||||
if (isDownloaded) {
|
||||
if (isInstalled) {
|
||||
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.`,
|
||||
title: 'Update Installed',
|
||||
description: `Sable ${updateInfo.version} has been installed. Restart the app to finish updating.`,
|
||||
primaryAction: {
|
||||
label: 'Restart Now',
|
||||
variant: 'Primary',
|
||||
@@ -93,12 +195,14 @@ export function DesktopUpdater() {
|
||||
};
|
||||
}
|
||||
|
||||
if (!isDownloaded) return null;
|
||||
|
||||
return {
|
||||
id: 'desktop-update-available',
|
||||
id: 'desktop-update-ready',
|
||||
priority: 200,
|
||||
icon: ArrowUp,
|
||||
title: 'Desktop Update Available',
|
||||
description: `Sable ${updateInfo.version} is available for installation.`,
|
||||
title: 'Update Available',
|
||||
description: `Sable ${updateInfo.version} is ready to install.${updateInfo.body ? `\n${updateInfo.body}` : ''}`,
|
||||
primaryAction: {
|
||||
label: isInstalling ? 'Installing...' : 'Install & Update',
|
||||
variant: 'Primary',
|
||||
@@ -111,9 +215,11 @@ export function DesktopUpdater() {
|
||||
},
|
||||
};
|
||||
}, [
|
||||
bannerVisible,
|
||||
updateInfo,
|
||||
dismissed,
|
||||
isDownloaded,
|
||||
isInstalled,
|
||||
isInstalling,
|
||||
handleInstall,
|
||||
handleRestart,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { atom } from 'jotai';
|
||||
import {
|
||||
atomWithLocalStorage,
|
||||
getLocalStorageItem,
|
||||
setLocalStorageItem,
|
||||
} from './utils/atomWithLocalStorage';
|
||||
|
||||
export type UpdatePhase =
|
||||
| { type: 'idle' }
|
||||
| { type: 'checking' }
|
||||
| { type: 'downloading'; progress: number }
|
||||
| { type: 'ready'; version: string }
|
||||
| { type: 'installing' };
|
||||
|
||||
export const updatePhaseAtom = atom<UpdatePhase>({ type: 'idle' });
|
||||
|
||||
export const updateBannerVisibleAtom = atom(false);
|
||||
|
||||
export const triggerUpdateCheckAtom = atom(0);
|
||||
|
||||
const DESKTOP_UPDATE_LAST_CHECKED_KEY = 'desktopUpdateLastChecked';
|
||||
|
||||
export const desktopUpdateLastCheckedAtom = atomWithLocalStorage<string | null>(
|
||||
DESKTOP_UPDATE_LAST_CHECKED_KEY,
|
||||
(key) => getLocalStorageItem<string | null>(key, null),
|
||||
(key, value) => setLocalStorageItem(key, value)
|
||||
);
|
||||
|
||||
export const fakeDesktopUpdate = (): boolean =>
|
||||
localStorage.getItem('sable_fake_desktop_update') === '1';
|
||||
@@ -6,7 +6,7 @@
|
||||
.tauri-titlebar {
|
||||
height: var(--tauri-titlebar-height);
|
||||
display: grid;
|
||||
grid-template-columns: 1fr auto;
|
||||
grid-template-columns: 1fr auto auto;
|
||||
align-items: stretch;
|
||||
position: relative;
|
||||
background: var(--sable-bg-container);
|
||||
@@ -44,6 +44,21 @@
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
|
||||
.tauri-titlebar__update {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding-inline: 4px;
|
||||
app-region: no-drag;
|
||||
-webkit-app-region: no-drag;
|
||||
}
|
||||
|
||||
.tauri-titlebar--mac .tauri-titlebar__update {
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
top: 0;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.tauri-titlebar__controls {
|
||||
display: flex;
|
||||
app-region: no-drag;
|
||||
|
||||
Reference in new issue
Block a user