feat: 增加忽略本次更新

This commit is contained in:
Wxw-Gu committed 2026-11-09 16:51:03 +08:00
1 parent 6cadcb9a5a
commit 7dcda8554e
7 files changed
+219 -5

No files matched your search

+13
View File
@@ -108,6 +108,7 @@ import {
saveSettings, saveSettings,
updateSettings, updateSettings,
getSettingsPath, getSettingsPath,
normalizeSkippedUpdateVersion,
AppSettings, AppSettings,
validateDbRoot validateDbRoot
} from './services/settings-store' } from './services/settings-store'
@@ -1473,6 +1474,18 @@ app.whenReady().then(async () => {
ipcMain.handle('app-update:download', () => appUpdateService.download()) ipcMain.handle('app-update:download', () => appUpdateService.download())
ipcMain.handle('app-update:install', () => appUpdateService.install()) ipcMain.handle('app-update:install', () => appUpdateService.install())
ipcMain.handle('app-update:openDownloadPage', () => appUpdateService.openDownloadPage()) 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() appUpdateService.scheduleStartupCheck()
ipcMain.handle('cache:getSummary', () => getCacheSummary()) ipcMain.handle('cache:getSummary', () => getCacheSummary())
ipcMain.handle('cache:openKnowledgeDirectory', () => openKnowledgeDirectory()) ipcMain.handle('cache:openKnowledgeDirectory', () => openKnowledgeDirectory())
+24 -2
View File
@@ -60,6 +60,21 @@ export interface AppSettings {
* 设置页随时可以改回来。 * 设置页随时可以改回来。
*/ */
closeAction: CloseAction 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 { function getDefaultDbRoot(): string {
@@ -145,7 +160,8 @@ const DEFAULT_SETTINGS: AppSettings = {
windowsWechatPort: '', windowsWechatPort: '',
reportImagePostfixText: '今日日报', reportImagePostfixText: '今日日报',
queryAgentEnabled: true, queryAgentEnabled: true,
closeAction: 'ask' closeAction: 'ask',
skippedUpdateVersion: ''
} }
const SETTINGS_FILE = path.join( const SETTINGS_FILE = path.join(
@@ -192,9 +208,15 @@ export function loadSettings(): AppSettings {
} }
// 关闭行为可能来自旧版本或手改过的 settings.json —— 非法值一律收敛回 `ask`。 // 关闭行为可能来自旧版本或手改过的 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.closeAction = 'ask'
} }
// 版本号是拿来逐字比对的:脏值会让「忽略」永远命中不了同一条记录。
cache.skippedUpdateVersion = normalizeSkippedUpdateVersion(cache.skippedUpdateVersion)
// 防撤回已下线(设置入口已隐藏):历史版本可能把它持久化为 true。 // 防撤回已下线(设置入口已隐藏):历史版本可能把它持久化为 true。
// 这里强制收敛为 false 并回写磁盘,确保旧的撤回监听与撤回日志不会继续运行。 // 这里强制收敛为 false 并回写磁盘,确保旧的撤回监听与撤回日志不会继续运行。
if (cache.recallProtectionEnabled || retiredKeepProcess !== undefined) { if (cache.recallProtectionEnabled || retiredKeepProcess !== undefined) {
+2
View File
@@ -239,6 +239,8 @@ declare global {
downloadAppUpdate: () => Promise<AppUpdateCheckResult> downloadAppUpdate: () => Promise<AppUpdateCheckResult>
installAppUpdate: () => Promise<AppUpdateInstallResult> installAppUpdate: () => Promise<AppUpdateInstallResult>
openAppUpdateDownloadPage: () => Promise<AppUpdateOpenDownloadPageResult> openAppUpdateDownloadPage: () => Promise<AppUpdateOpenDownloadPageResult>
getIgnoredUpdateVersion: () => Promise<string>
setIgnoredUpdateVersion: (version: string | null) => Promise<string>
onAppUpdateState: (callback: (state: AppUpdateState) => void) => () => void onAppUpdateState: (callback: (state: AppUpdateState) => void) => () => void
getCacheSummary: () => Promise<CacheSummary> getCacheSummary: () => Promise<CacheSummary>
clearCache: (scope: CacheClearScope) => Promise<CacheSummary> clearCache: (scope: CacheClearScope) => Promise<CacheSummary>
+5
View File
@@ -142,6 +142,11 @@ const api = {
downloadAppUpdate: () => ipcRenderer.invoke('app-update:download'), downloadAppUpdate: () => ipcRenderer.invoke('app-update:download'),
installAppUpdate: () => ipcRenderer.invoke('app-update:install'), installAppUpdate: () => ipcRenderer.invoke('app-update:install'),
openAppUpdateDownloadPage: () => ipcRenderer.invoke('app-update:openDownloadPage'), openAppUpdateDownloadPage: () => ipcRenderer.invoke('app-update:openDownloadPage'),
getIgnoredUpdateVersion: (): Promise<string> =>
ipcRenderer.invoke('app-update:getIgnoredVersion'),
/** 传 `null` 清除忽略记录并恢复启动提示;返回写入后的值(非法版本会被收敛成空串)。 */
setIgnoredUpdateVersion: (version: string | null): Promise<string> =>
ipcRenderer.invoke('app-update:setIgnoredVersion', version),
onAppUpdateState: (callback: (state: AppUpdateState) => void) => { onAppUpdateState: (callback: (state: AppUpdateState) => void) => {
const listener = (_event: Electron.IpcRendererEvent, state: AppUpdateState): void => const listener = (_event: Electron.IpcRendererEvent, state: AppUpdateState): void =>
callback(state) callback(state)
@@ -1,4 +1,4 @@
import { useState } from 'react' import { useEffect, useState } from 'react'
import { import {
AlertDialog, AlertDialog,
AlertDialogAction, AlertDialogAction,
@@ -21,14 +21,53 @@ export function AppUpdatePrompt({
}): React.ReactElement { }): React.ReactElement {
const update = useAppUpdateState() const update = useAppUpdateState()
const [dismissedVersion, setDismissedVersion] = useState<string>() const [dismissedVersion, setDismissedVersion] = useState<string>()
/**
* 已忽略的版本号。`null` 表示还没读回来 —— 这期间不弹,否则用户会看到
* 弹窗闪一下就消失(启动时状态先到、设置后到)。
*/
const [ignoredVersion, setIgnoredVersion] = useState<string | null>(null)
useEffect(() => {
let active = true
void window.api
.getIgnoredUpdateVersion()
.then((version) => {
if (active) setIgnoredVersion(version || '')
})
.catch(() => {
// 读不到就按"没有忽略"处理:宁可多弹一次,也不要静默吞掉更新提示。
if (active) setIgnoredVersion('')
})
return () => {
active = false
}
}, [])
const shouldPrompt = const shouldPrompt =
update.status === 'available' && update.status === 'available' &&
update.source === 'startup' && update.source === 'startup' &&
Boolean(update.version) && Boolean(update.version) &&
ignoredVersion !== null &&
update.version !== ignoredVersion &&
update.version !== dismissedVersion update.version !== dismissedVersion
const dismiss = (): void => setDismissedVersion(update.version) const dismiss = (): void => setDismissedVersion(update.version)
const ignoreVersion = async (): Promise<void> => {
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<void> => { const continueUpdate = async (): Promise<void> => {
dismiss() dismiss()
if (update.delivery === 'release-page') { if (update.delivery === 'release-page') {
@@ -55,6 +94,9 @@ export function AppUpdatePrompt({
</AlertDialogDescription> </AlertDialogDescription>
</AlertDialogHeader> </AlertDialogHeader>
<AlertDialogFooter> <AlertDialogFooter>
<Button variant="ghost" className="sm:mr-auto" onClick={() => void ignoreVersion()}>
忽略此版本
</Button>
<AlertDialogCancel asChild> <AlertDialogCancel asChild>
<Button variant="outline" onClick={dismiss}> <Button variant="outline" onClick={dismiss}>
取消 取消
@@ -1,4 +1,4 @@
import { useMemo, useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import { Button, Progress } from '../../../components/ui' import { Button, Progress } from '../../../components/ui'
import { useAppUpdateState } from '../../app-update/useAppUpdateState' import { useAppUpdateState } from '../../app-update/useAppUpdateState'
@@ -19,6 +19,30 @@ export function AboutPage({
}): React.ReactElement { }): React.ReactElement {
const update = useAppUpdateState() const update = useAppUpdateState()
const [busy, setBusy] = useState(false) const [busy, setBusy] = useState(false)
const [ignoredUpdateVersion, setIgnoredUpdateVersion] = useState('')
useEffect(() => {
let active = true
void window.api
.getIgnoredUpdateVersion()
.then((version) => {
if (active) setIgnoredUpdateVersion(version || '')
})
.catch(() => undefined)
return () => {
active = false
}
}, [])
const restoreUpdatePrompt = async (): Promise<void> => {
try {
await window.api.setIgnoredUpdateVersion(null)
setIgnoredUpdateVersion('')
onNotice('已恢复启动时的更新提示')
} catch (error) {
onNotice(error instanceof Error ? error.message : '恢复更新提示失败')
}
}
const action = useMemo(() => { const action = useMemo(() => {
if (update.status === 'available') { if (update.status === 'available') {
@@ -153,6 +177,20 @@ export function AboutPage({
</div> </div>
</section> </section>
{ignoredUpdateVersion ? (
<section className="settings-card flex flex-wrap items-center justify-between gap-3">
<div>
<strong>已忽略 v{ignoredUpdateVersion} 的更新提示</strong>
<span className="mt-1 block text-sm text-muted-foreground">
该版本不会在启动时弹窗;更高版本仍会提醒。
</span>
</div>
<Button variant="outline" size="sm" onClick={() => void restoreUpdatePrompt()}>
恢复提示
</Button>
</section>
) : null}
<h2 className="settings-section-heading">支持</h2> <h2 className="settings-section-heading">支持</h2>
<section className="settings-card about-links-card"> <section className="settings-card about-links-card">
<a href={RELEASES_URL} target="_blank" rel="noreferrer"> <a href={RELEASES_URL} target="_blank" rel="noreferrer">
+93 -1
View File
@@ -16,13 +16,18 @@ const api = {
updateListener = listener updateListener = listener
return vi.fn() 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', () => { describe('app update UI', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks() vi.clearAllMocks()
updateListener = undefined updateListener = undefined
// clearAllMocks 不清实现,逐个用例重设,避免上一条的"已忽略版本"串到下一题。
api.getIgnoredUpdateVersion.mockResolvedValue('')
api.setIgnoredUpdateVersion.mockImplementation(async (version: string | null) => version ?? '')
Object.defineProperty(window, 'api', { configurable: true, value: api }) 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(<AppUpdatePrompt onDownloadStart={vi.fn()} onNotice={vi.fn()} />)
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(<AppUpdatePrompt onDownloadStart={vi.fn()} onNotice={vi.fn()} />)
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(<AppUpdatePrompt onDownloadStart={vi.fn()} onNotice={onNotice} />)
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(<AboutPage onNotice={onNotice} />)
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 () => { it('renders live transfer details and keeps simulated install inside the app', async () => {
const user = userEvent.setup() const user = userEvent.setup()
api.getAppUpdateState.mockResolvedValue({ api.getAppUpdateState.mockResolvedValue({