From 7dcda8554e328b82b77fa428f76879adbdb02dc2 Mon Sep 17 00:00:00 2001 From: Wxw-Gu <84499436+Wxw-Gu@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:51:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E6=9C=AC=E6=AC=A1=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.ts | 13 +++ src/main/services/settings-store.ts | 26 ++++- src/preload/index.d.ts | 2 + src/preload/index.ts | 5 + .../features/app-update/AppUpdatePrompt.tsx | 44 ++++++++- .../src/features/settings/pages/AboutPage.tsx | 40 +++++++- tests/component/app-update.test.tsx | 94 ++++++++++++++++++- 7 files changed, 219 insertions(+), 5 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 0c4027a..034b839 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -108,6 +108,7 @@ import { saveSettings, updateSettings, getSettingsPath, + normalizeSkippedUpdateVersion, AppSettings, validateDbRoot } from './services/settings-store' @@ -1473,6 +1474,18 @@ app.whenReady().then(async () => { ipcMain.handle('app-update:download', () => appUpdateService.download()) ipcMain.handle('app-update:install', () => appUpdateService.install()) ipcMain.handle('app-update:openDownloadPage', () => appUpdateService.openDownloadPage()) + ipcMain.handle('app-update:getIgnoredVersion', () => loadSettings().skippedUpdateVersion) + /** + * 记录 / 清除「忽略此版本」。 + * + * 传 null 或非法版本号 = 清除。校验放在主进程:渲染层传来的字符串不可信, + * 一个脏值会让「忽略」永远命中不了同一条记录,表现为每次都弹。 + */ + ipcMain.handle('app-update:setIgnoredVersion', (_, version: unknown) => { + const normalized = normalizeSkippedUpdateVersion(version) + saveSettings({ ...loadSettings(), skippedUpdateVersion: normalized }) + return normalized + }) appUpdateService.scheduleStartupCheck() ipcMain.handle('cache:getSummary', () => getCacheSummary()) ipcMain.handle('cache:openKnowledgeDirectory', () => openKnowledgeDirectory()) diff --git a/src/main/services/settings-store.ts b/src/main/services/settings-store.ts index 85eed4c..a2d3fa2 100644 --- a/src/main/services/settings-store.ts +++ b/src/main/services/settings-store.ts @@ -60,6 +60,21 @@ export interface AppSettings { * 设置页随时可以改回来。 */ closeAction: CloseAction + /** + * 用户点过「忽略此版本」的更新版本号(不含 `v` 前缀)。 + * + * 只影响**启动时的自动弹窗**:同一版本不再打扰。手动检查更新与「设置 → 关于」 + * 始终展示最新版本 —— 忽略的是提示,不是版本本身。空串表示没有忽略任何版本。 + */ + skippedUpdateVersion: string +} + +/** 只接受 `1.2.3` / `1.2.3-beta.1` 这类版本号;其它输入一律按"没有忽略"处理。 */ +const UPDATE_VERSION_PATTERN = /^\d{1,6}\.\d{1,6}\.\d{1,6}(?:[-+][0-9A-Za-z.-]{1,32})?$/ + +export function normalizeSkippedUpdateVersion(value: unknown): string { + const text = String(value ?? '').trim() + return UPDATE_VERSION_PATTERN.test(text) ? text : '' } function getDefaultDbRoot(): string { @@ -145,7 +160,8 @@ const DEFAULT_SETTINGS: AppSettings = { windowsWechatPort: '', reportImagePostfixText: '今日日报', queryAgentEnabled: true, - closeAction: 'ask' + closeAction: 'ask', + skippedUpdateVersion: '' } const SETTINGS_FILE = path.join( @@ -192,9 +208,15 @@ export function loadSettings(): AppSettings { } // 关闭行为可能来自旧版本或手改过的 settings.json —— 非法值一律收敛回 `ask`。 // 放任非法值会造成最难排查的一种表现:点关闭窗口什么也不发生。 - if (cache.closeAction !== 'ask' && cache.closeAction !== 'tray' && cache.closeAction !== 'quit') { + if ( + cache.closeAction !== 'ask' && + cache.closeAction !== 'tray' && + cache.closeAction !== 'quit' + ) { cache.closeAction = 'ask' } + // 版本号是拿来逐字比对的:脏值会让「忽略」永远命中不了同一条记录。 + cache.skippedUpdateVersion = normalizeSkippedUpdateVersion(cache.skippedUpdateVersion) // 防撤回已下线(设置入口已隐藏):历史版本可能把它持久化为 true。 // 这里强制收敛为 false 并回写磁盘,确保旧的撤回监听与撤回日志不会继续运行。 if (cache.recallProtectionEnabled || retiredKeepProcess !== undefined) { diff --git a/src/preload/index.d.ts b/src/preload/index.d.ts index 2cfa820..f1dd223 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -239,6 +239,8 @@ declare global { downloadAppUpdate: () => Promise installAppUpdate: () => Promise openAppUpdateDownloadPage: () => Promise + getIgnoredUpdateVersion: () => Promise + setIgnoredUpdateVersion: (version: string | null) => Promise onAppUpdateState: (callback: (state: AppUpdateState) => void) => () => void getCacheSummary: () => Promise clearCache: (scope: CacheClearScope) => Promise diff --git a/src/preload/index.ts b/src/preload/index.ts index a4c17e3..06deec7 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -142,6 +142,11 @@ const api = { downloadAppUpdate: () => ipcRenderer.invoke('app-update:download'), installAppUpdate: () => ipcRenderer.invoke('app-update:install'), openAppUpdateDownloadPage: () => ipcRenderer.invoke('app-update:openDownloadPage'), + getIgnoredUpdateVersion: (): Promise => + ipcRenderer.invoke('app-update:getIgnoredVersion'), + /** 传 `null` 清除忽略记录并恢复启动提示;返回写入后的值(非法版本会被收敛成空串)。 */ + setIgnoredUpdateVersion: (version: string | null): Promise => + ipcRenderer.invoke('app-update:setIgnoredVersion', version), onAppUpdateState: (callback: (state: AppUpdateState) => void) => { const listener = (_event: Electron.IpcRendererEvent, state: AppUpdateState): void => callback(state) diff --git a/src/renderer/src/features/app-update/AppUpdatePrompt.tsx b/src/renderer/src/features/app-update/AppUpdatePrompt.tsx index 13fcb51..38d3c1b 100644 --- a/src/renderer/src/features/app-update/AppUpdatePrompt.tsx +++ b/src/renderer/src/features/app-update/AppUpdatePrompt.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useEffect, useState } from 'react' import { AlertDialog, AlertDialogAction, @@ -21,14 +21,53 @@ export function AppUpdatePrompt({ }): React.ReactElement { const update = useAppUpdateState() const [dismissedVersion, setDismissedVersion] = useState() + /** + * 已忽略的版本号。`null` 表示还没读回来 —— 这期间不弹,否则用户会看到 + * 弹窗闪一下就消失(启动时状态先到、设置后到)。 + */ + const [ignoredVersion, setIgnoredVersion] = useState(null) + + useEffect(() => { + let active = true + void window.api + .getIgnoredUpdateVersion() + .then((version) => { + if (active) setIgnoredVersion(version || '') + }) + .catch(() => { + // 读不到就按"没有忽略"处理:宁可多弹一次,也不要静默吞掉更新提示。 + if (active) setIgnoredVersion('') + }) + return () => { + active = false + } + }, []) + const shouldPrompt = update.status === 'available' && update.source === 'startup' && Boolean(update.version) && + ignoredVersion !== null && + update.version !== ignoredVersion && update.version !== dismissedVersion const dismiss = (): void => setDismissedVersion(update.version) + const ignoreVersion = async (): Promise => { + const version = update.version + if (!version) return + setIgnoredVersion(version) + dismiss() + try { + await window.api.setIgnoredUpdateVersion(version) + onNotice(`已忽略 v${version} 的更新提示,可在「设置 → 关于」恢复`) + } catch (error) { + // 没写进磁盘就当作没忽略,否则用户以为设置生效了,下次启动照样弹。 + setIgnoredVersion('') + onNotice(error instanceof Error ? error.message : '忽略更新提示失败') + } + } + const continueUpdate = async (): Promise => { dismiss() if (update.delivery === 'release-page') { @@ -55,6 +94,9 @@ export function AppUpdatePrompt({ + + + ) : null} +

支持

diff --git a/tests/component/app-update.test.tsx b/tests/component/app-update.test.tsx index ea63220..1f6d31d 100644 --- a/tests/component/app-update.test.tsx +++ b/tests/component/app-update.test.tsx @@ -16,13 +16,18 @@ const api = { updateListener = listener return vi.fn() }), - revealAppLog: vi.fn() + revealAppLog: vi.fn(), + getIgnoredUpdateVersion: vi.fn(async () => ''), + setIgnoredUpdateVersion: vi.fn(async (version: string | null) => version ?? '') } describe('app update UI', () => { beforeEach(() => { vi.clearAllMocks() updateListener = undefined + // clearAllMocks 不清实现,逐个用例重设,避免上一条的"已忽略版本"串到下一题。 + api.getIgnoredUpdateVersion.mockResolvedValue('') + api.setIgnoredUpdateVersion.mockImplementation(async (version: string | null) => version ?? '') Object.defineProperty(window, 'api', { configurable: true, value: api }) }) @@ -54,6 +59,93 @@ describe('app update UI', () => { ) }) + it('stays quiet on startup for a version the user already ignored', async () => { + api.getAppUpdateState.mockResolvedValue({ + status: 'available', + currentVersion: '1.9.0', + delivery: 'automatic', + version: '2.0.0', + source: 'startup', + isSimulation: true + }) + api.getIgnoredUpdateVersion.mockResolvedValue('2.0.0') + + render() + + await waitFor(() => expect(api.getIgnoredUpdateVersion).toHaveBeenCalled()) + await act(async () => { + await Promise.resolve() + }) + expect(screen.queryByRole('alertdialog', { name: '发现新版本 v2.0.0' })).not.toBeInTheDocument() + }) + + it('prompts again once a newer version supersedes the ignored one', async () => { + api.getAppUpdateState.mockResolvedValue({ + status: 'available', + currentVersion: '1.9.0', + delivery: 'automatic', + version: '2.1.0', + source: 'startup', + isSimulation: true + }) + api.getIgnoredUpdateVersion.mockResolvedValue('2.0.0') + + render() + + await screen.findByRole('alertdialog', { name: '发现新版本 v2.1.0' }) + }) + + it('persists the ignored version and closes the prompt immediately', async () => { + const user = userEvent.setup() + api.getAppUpdateState.mockResolvedValue({ + status: 'available', + currentVersion: '1.9.0', + delivery: 'automatic', + version: '2.0.0', + source: 'startup', + isSimulation: true + }) + const onNotice = vi.fn() + + render() + + await screen.findByRole('alertdialog', { name: '发现新版本 v2.0.0' }) + await user.click(screen.getByRole('button', { name: '忽略此版本' })) + + expect(api.setIgnoredUpdateVersion).toHaveBeenCalledWith('2.0.0') + await waitFor(() => + expect( + screen.queryByRole('alertdialog', { name: '发现新版本 v2.0.0' }) + ).not.toBeInTheDocument() + ) + expect(onNotice).toHaveBeenCalledWith(expect.stringContaining('v2.0.0')) + expect(api.downloadAppUpdate).not.toHaveBeenCalled() + }) + + it('restores the startup prompt from About', async () => { + const user = userEvent.setup() + api.getAppUpdateState.mockResolvedValue({ + status: 'up-to-date', + currentVersion: '2.2.2', + delivery: 'release-page', + message: '已是最新版本' + }) + api.getIgnoredUpdateVersion.mockResolvedValue('2.2.3') + api.setIgnoredUpdateVersion.mockResolvedValue('') + const onNotice = vi.fn() + + render() + + await screen.findByText('已忽略 v2.2.3 的更新提示') + await user.click(screen.getByRole('button', { name: '恢复提示' })) + + expect(api.setIgnoredUpdateVersion).toHaveBeenCalledWith(null) + expect(onNotice).toHaveBeenCalledWith('已恢复启动时的更新提示') + await waitFor(() => + expect(screen.queryByText('已忽略 v2.2.3 的更新提示')).not.toBeInTheDocument() + ) + }) + it('renders live transfer details and keeps simulated install inside the app', async () => { const user = userEvent.setup() api.getAppUpdateState.mockResolvedValue({