mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-11-30 16:02:58 +08:00
feat: 增加更新页
This commit is contained in:
1 parent
eec6286a58
commit
9dee0bbd3c
28 files changed
+1079
-135
No files matched your search
@@ -685,6 +685,8 @@ app.whenReady().then(async () => {
|
||||
ipcMain.handle('app-update:check', () => appUpdateService.check())
|
||||
ipcMain.handle('app-update:download', () => appUpdateService.download())
|
||||
ipcMain.handle('app-update:install', () => appUpdateService.install())
|
||||
ipcMain.handle('app-update:openDownloadPage', () => appUpdateService.openDownloadPage())
|
||||
appUpdateService.scheduleStartupCheck()
|
||||
ipcMain.handle('cache:getSummary', () => getCacheSummary())
|
||||
ipcMain.handle('cache:openKnowledgeDirectory', () => openKnowledgeDirectory())
|
||||
ipcMain.handle('cache:clear', async (_, scope: CacheClearScope) => {
|
||||
|
||||
@@ -1,25 +1,108 @@
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import { app, BrowserWindow, shell } from 'electron'
|
||||
import { autoUpdater, type ProgressInfo } from 'electron-updater'
|
||||
import type { AppUpdateCheckResult, AppUpdateState } from '../../shared/app-update'
|
||||
import type {
|
||||
AppUpdateCheckResult,
|
||||
AppUpdateCheckSource,
|
||||
AppUpdateDelivery,
|
||||
AppUpdateInstallResult,
|
||||
AppUpdateOpenDownloadPageResult,
|
||||
AppUpdateState
|
||||
} from '../../shared/app-update'
|
||||
import { APP_UPDATE_RELEASES_URL } from '../../shared/app-update'
|
||||
import { isPackagedRuntime } from '../runtime-mode'
|
||||
|
||||
const SIMULATION_VERSION = '2.0.0'
|
||||
const SIMULATION_TOTAL_BYTES = 60 * 1024 * 1024
|
||||
const SIMULATION_PROGRESS = [0, 5, 12, 21, 33, 46, 58, 69, 78, 86, 93, 97, 100]
|
||||
const DEFAULT_SIMULATION_DURATION_MS = 8_000
|
||||
const STARTUP_CHECK_DELAY_MS = 1_500
|
||||
// Enable only after macOS packages use Developer ID Application signing and notarization.
|
||||
const MAC_AUTO_UPDATE_ENABLED = false
|
||||
|
||||
interface AppUpdateServiceOptions {
|
||||
currentVersion?: () => string
|
||||
packagedRuntime?: () => boolean
|
||||
simulationEnabled?: boolean
|
||||
simulationDurationMs?: number
|
||||
platform?: NodeJS.Platform
|
||||
macAutoUpdateEnabled?: boolean
|
||||
broadcast?: (state: AppUpdateState) => void
|
||||
}
|
||||
|
||||
export class AppUpdateService {
|
||||
private state: AppUpdateState = {
|
||||
status: 'idle',
|
||||
currentVersion: app.getVersion()
|
||||
}
|
||||
private readonly currentVersion: () => string
|
||||
private readonly packagedRuntime: () => boolean
|
||||
private readonly simulationEnabled: boolean
|
||||
private readonly simulationDurationMs: number
|
||||
private readonly delivery: AppUpdateDelivery
|
||||
private readonly broadcast?: (state: AppUpdateState) => void
|
||||
private state: AppUpdateState
|
||||
private listeners = new Set<(state: AppUpdateState) => void>()
|
||||
private activeCheckSource: AppUpdateCheckSource = 'manual'
|
||||
private startupCheckScheduled = false
|
||||
private checkInFlight?: Promise<AppUpdateCheckResult>
|
||||
private downloadInFlight?: Promise<AppUpdateCheckResult>
|
||||
|
||||
constructor(options: AppUpdateServiceOptions = {}) {
|
||||
this.currentVersion = options.currentVersion || (() => app.getVersion())
|
||||
this.packagedRuntime = options.packagedRuntime || isPackagedRuntime
|
||||
this.simulationEnabled =
|
||||
options.simulationEnabled ??
|
||||
(!app.isPackaged && process.env['TRACEMEMO_UPDATE_SIMULATION'] === 'true')
|
||||
const configuredDuration = Number(
|
||||
options.simulationDurationMs ?? process.env['TRACEMEMO_UPDATE_SIMULATION_DURATION_MS']
|
||||
)
|
||||
this.simulationDurationMs = Number.isFinite(configuredDuration)
|
||||
? Math.max(1_000, configuredDuration)
|
||||
: DEFAULT_SIMULATION_DURATION_MS
|
||||
const platform = options.platform || process.platform
|
||||
const macAutoUpdateEnabled = options.macAutoUpdateEnabled ?? MAC_AUTO_UPDATE_ENABLED
|
||||
this.delivery =
|
||||
!this.simulationEnabled && platform === 'darwin' && !macAutoUpdateEnabled
|
||||
? 'release-page'
|
||||
: 'automatic'
|
||||
this.broadcast = options.broadcast
|
||||
this.state = {
|
||||
status: 'idle',
|
||||
currentVersion: this.currentVersion(),
|
||||
delivery: this.delivery,
|
||||
isSimulation: this.simulationEnabled
|
||||
}
|
||||
|
||||
constructor() {
|
||||
autoUpdater.autoDownload = false
|
||||
autoUpdater.autoInstallOnAppQuit = true
|
||||
autoUpdater.on('checking-for-update', () => this.setState({ status: 'checking' }))
|
||||
autoUpdater.autoInstallOnAppQuit = this.delivery === 'automatic'
|
||||
autoUpdater.on('checking-for-update', () =>
|
||||
this.setState({
|
||||
status: 'checking',
|
||||
source: this.activeCheckSource,
|
||||
message: '正在检查更新',
|
||||
version: undefined,
|
||||
percent: undefined,
|
||||
transferred: undefined,
|
||||
total: undefined,
|
||||
bytesPerSecond: undefined,
|
||||
error: undefined
|
||||
})
|
||||
)
|
||||
autoUpdater.on('update-available', (info) =>
|
||||
this.setState({ status: 'available', version: info.version, message: '发现新版本' })
|
||||
this.setState({
|
||||
status: 'available',
|
||||
source: this.activeCheckSource,
|
||||
version: info.version,
|
||||
message: '发现新版本',
|
||||
error: undefined
|
||||
})
|
||||
)
|
||||
autoUpdater.on('update-not-available', () =>
|
||||
this.setState({ status: 'not-available', message: '当前已是最新版本' })
|
||||
this.setState({
|
||||
status: 'up-to-date',
|
||||
source: this.activeCheckSource,
|
||||
message: '已是最新版本',
|
||||
error: undefined
|
||||
})
|
||||
)
|
||||
autoUpdater.on('download-progress', (progress: ProgressInfo) =>
|
||||
autoUpdater.on('download-progress', (progress: ProgressInfo) => {
|
||||
if (this.delivery !== 'automatic') return
|
||||
this.setState({
|
||||
status: 'downloading',
|
||||
percent: progress.percent,
|
||||
@@ -27,94 +110,222 @@ export class AppUpdateService {
|
||||
total: progress.total,
|
||||
bytesPerSecond: progress.bytesPerSecond
|
||||
})
|
||||
)
|
||||
autoUpdater.on('update-downloaded', (info) =>
|
||||
})
|
||||
autoUpdater.on('update-downloaded', (info) => {
|
||||
if (this.delivery !== 'automatic') return
|
||||
this.setState({
|
||||
status: 'downloaded',
|
||||
version: info.version,
|
||||
version: info.version || this.state.version,
|
||||
percent: 100,
|
||||
transferred: this.state.total,
|
||||
message: '更新已下载'
|
||||
})
|
||||
)
|
||||
})
|
||||
autoUpdater.on('error', (error) =>
|
||||
this.setState({ status: 'error', message: error.message || '更新失败' })
|
||||
this.setState({
|
||||
status: 'error',
|
||||
message: '检查更新失败',
|
||||
error: error.message || '更新失败'
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
getState(): AppUpdateState {
|
||||
return { ...this.state, currentVersion: app.getVersion() }
|
||||
return { ...this.state, currentVersion: this.currentVersion() }
|
||||
}
|
||||
|
||||
async check(): Promise<AppUpdateCheckResult> {
|
||||
if (!isPackagedRuntime()) {
|
||||
scheduleStartupCheck(delayMs = STARTUP_CHECK_DELAY_MS): void {
|
||||
if (this.startupCheckScheduled) return
|
||||
if (!this.simulationEnabled && !this.packagedRuntime()) return
|
||||
this.startupCheckScheduled = true
|
||||
setTimeout(() => void this.check('startup'), Math.max(0, delayMs))
|
||||
}
|
||||
|
||||
check(source: AppUpdateCheckSource = 'manual'): Promise<AppUpdateCheckResult> {
|
||||
if (this.checkInFlight) return this.checkInFlight
|
||||
this.checkInFlight = this.runCheck(source).finally(() => {
|
||||
this.checkInFlight = undefined
|
||||
})
|
||||
return this.checkInFlight
|
||||
}
|
||||
|
||||
private async runCheck(source: AppUpdateCheckSource): Promise<AppUpdateCheckResult> {
|
||||
this.activeCheckSource = source
|
||||
if (this.simulationEnabled) {
|
||||
this.setState({
|
||||
status: 'checking',
|
||||
source,
|
||||
isSimulation: true,
|
||||
message: '正在检查模拟更新',
|
||||
version: undefined,
|
||||
percent: undefined,
|
||||
transferred: undefined,
|
||||
total: undefined,
|
||||
bytesPerSecond: undefined,
|
||||
error: undefined
|
||||
})
|
||||
await this.delay(350)
|
||||
const state = this.setState({
|
||||
status: 'available',
|
||||
source,
|
||||
version: SIMULATION_VERSION,
|
||||
message: '发现模拟更新',
|
||||
error: undefined
|
||||
})
|
||||
return { success: true, state }
|
||||
}
|
||||
|
||||
if (!this.packagedRuntime()) {
|
||||
const state = this.setState({
|
||||
status: 'unsupported',
|
||||
message: '开发模式不执行安装包更新,请在正式安装包中检查更新'
|
||||
source,
|
||||
message: '开发模式不执行安装包更新,请在正式安装包中检查更新',
|
||||
error: undefined
|
||||
})
|
||||
return { success: false, state }
|
||||
}
|
||||
try {
|
||||
const result = await autoUpdater.checkForUpdates()
|
||||
if (result?.updateInfo.version) {
|
||||
this.setState({
|
||||
status: 'available',
|
||||
version: result.updateInfo.version,
|
||||
message: '发现新版本'
|
||||
})
|
||||
}
|
||||
await autoUpdater.checkForUpdates()
|
||||
return { success: true, state: this.getState() }
|
||||
} catch (error) {
|
||||
const state = this.setState({
|
||||
status: 'error',
|
||||
message: error instanceof Error ? error.message : String(error)
|
||||
message: '检查更新失败',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
return { success: false, state }
|
||||
}
|
||||
}
|
||||
|
||||
async download(): Promise<AppUpdateCheckResult> {
|
||||
if (!isPackagedRuntime()) {
|
||||
const state = this.setState({ status: 'unsupported', message: '开发模式不能下载更新' })
|
||||
download(): Promise<AppUpdateCheckResult> {
|
||||
if (this.downloadInFlight) return this.downloadInFlight
|
||||
this.downloadInFlight = this.runDownload().finally(() => {
|
||||
this.downloadInFlight = undefined
|
||||
})
|
||||
return this.downloadInFlight
|
||||
}
|
||||
|
||||
private async runDownload(): Promise<AppUpdateCheckResult> {
|
||||
if (this.simulationEnabled) return this.simulateDownload()
|
||||
|
||||
if (this.delivery === 'release-page') {
|
||||
const state = this.setState({
|
||||
status: 'available',
|
||||
message: '有新版本可用,前往 GitHub 下载最新版本。'
|
||||
})
|
||||
return { success: false, state }
|
||||
}
|
||||
|
||||
if (!this.packagedRuntime()) {
|
||||
const state = this.setState({
|
||||
status: 'unsupported',
|
||||
message: '开发模式不能下载更新',
|
||||
error: undefined
|
||||
})
|
||||
return { success: false, state }
|
||||
}
|
||||
try {
|
||||
this.setState({ status: 'downloading', percent: 0 })
|
||||
this.setState({ status: 'downloading', percent: 0, error: undefined })
|
||||
await autoUpdater.downloadUpdate()
|
||||
return { success: true, state: this.getState() }
|
||||
} catch (error) {
|
||||
const state = this.setState({
|
||||
status: 'error',
|
||||
message: error instanceof Error ? error.message : String(error)
|
||||
message: '下载更新失败',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
return { success: false, state }
|
||||
}
|
||||
}
|
||||
|
||||
install(): { success: boolean; error?: string } {
|
||||
install(): AppUpdateInstallResult {
|
||||
if (this.delivery !== 'automatic') {
|
||||
return { success: false, error: '当前 macOS 版本不支持自动安装,请前往 GitHub 下载更新。' }
|
||||
}
|
||||
if (this.state.status !== 'downloaded') {
|
||||
return { success: false, error: '更新包尚未下载完成' }
|
||||
}
|
||||
if (this.simulationEnabled) {
|
||||
const message = '开发模拟模式:更新安装动作已模拟,未实际退出应用。'
|
||||
this.setState({ status: 'downloaded', message })
|
||||
return { success: true, simulated: true, message }
|
||||
}
|
||||
this.setState({ status: 'installing', message: '正在重启并安装更新' })
|
||||
autoUpdater.quitAndInstall()
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
async openDownloadPage(): Promise<AppUpdateOpenDownloadPageResult> {
|
||||
try {
|
||||
await shell.openExternal(APP_UPDATE_RELEASES_URL)
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
return { success: false, error: error instanceof Error ? error.message : String(error) }
|
||||
}
|
||||
}
|
||||
|
||||
handleState(callback: (state: AppUpdateState) => void): () => void {
|
||||
this.listeners.add(callback)
|
||||
callback(this.getState())
|
||||
return () => this.listeners.delete(callback)
|
||||
}
|
||||
|
||||
private listeners = new Set<(state: AppUpdateState) => void>()
|
||||
|
||||
private setState(patch: Partial<AppUpdateState>): AppUpdateState {
|
||||
this.state = { ...this.state, ...patch, currentVersion: app.getVersion() }
|
||||
this.state = { ...this.state, ...patch, currentVersion: this.currentVersion() }
|
||||
const state = this.getState()
|
||||
for (const window of BrowserWindow.getAllWindows()) {
|
||||
if (!window.isDestroyed()) window.webContents.send('app-update:state', state)
|
||||
if (this.broadcast) {
|
||||
this.broadcast(state)
|
||||
} else {
|
||||
for (const window of BrowserWindow.getAllWindows()) {
|
||||
if (!window.isDestroyed()) window.webContents.send('app-update:state', state)
|
||||
}
|
||||
}
|
||||
for (const listener of this.listeners) listener(state)
|
||||
return state
|
||||
}
|
||||
|
||||
private async simulateDownload(): Promise<AppUpdateCheckResult> {
|
||||
const stepDuration = this.simulationDurationMs / (SIMULATION_PROGRESS.length - 1)
|
||||
let previousTransferred = 0
|
||||
this.setState({
|
||||
status: 'downloading',
|
||||
version: SIMULATION_VERSION,
|
||||
percent: 0,
|
||||
transferred: 0,
|
||||
total: SIMULATION_TOTAL_BYTES,
|
||||
bytesPerSecond: 0,
|
||||
message: '正在下载模拟更新'
|
||||
})
|
||||
|
||||
for (const percent of SIMULATION_PROGRESS.slice(1)) {
|
||||
await this.delay(stepDuration)
|
||||
const transferred = Math.round((SIMULATION_TOTAL_BYTES * percent) / 100)
|
||||
const bytesPerSecond = Math.round(((transferred - previousTransferred) * 1000) / stepDuration)
|
||||
previousTransferred = transferred
|
||||
this.setState({
|
||||
status: 'downloading',
|
||||
percent,
|
||||
transferred,
|
||||
total: SIMULATION_TOTAL_BYTES,
|
||||
bytesPerSecond
|
||||
})
|
||||
}
|
||||
|
||||
const state = this.setState({
|
||||
status: 'downloaded',
|
||||
version: SIMULATION_VERSION,
|
||||
percent: 100,
|
||||
transferred: SIMULATION_TOTAL_BYTES,
|
||||
total: SIMULATION_TOTAL_BYTES,
|
||||
bytesPerSecond: undefined,
|
||||
message: '模拟更新已下载'
|
||||
})
|
||||
return { success: true, state }
|
||||
}
|
||||
|
||||
private delay(durationMs: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, durationMs))
|
||||
}
|
||||
}
|
||||
|
||||
export const appUpdateService = new AppUpdateService()
|
||||
@@ -7,7 +7,7 @@ const SKILL_RELATIVE_PATHS = [
|
||||
join('skill', 'tracememo-reader', 'SKILL.md'),
|
||||
join('skill', 'wechatexplorer-reader', 'SKILL.md')
|
||||
]
|
||||
const GITHUB_URL = 'https://github.com/Wxw-Gu/WechatExplorer/tree/main/docs/skill/tracememo-reader'
|
||||
const GITHUB_URL = 'https://github.com/Wxw-Gu/TraceMemo/tree/main/docs/skill/tracememo-reader'
|
||||
const SKILL_VERSION = 'v1.2'
|
||||
|
||||
type SkillResourceSource = 'development' | 'bundled'
|
||||
|
||||
Vendored
+8
-2
@@ -66,7 +66,12 @@ import type {
|
||||
PersonalWechatRuntimeStatus
|
||||
} from '../shared/personal-wechat-runtime'
|
||||
import type { AppLogEntry } from '../shared/app-log'
|
||||
import type { AppUpdateCheckResult, AppUpdateState } from '../shared/app-update'
|
||||
import type {
|
||||
AppUpdateCheckResult,
|
||||
AppUpdateInstallResult,
|
||||
AppUpdateOpenDownloadPageResult,
|
||||
AppUpdateState
|
||||
} from '../shared/app-update'
|
||||
import type { CacheSummary } from '../shared/cache'
|
||||
import type { ExportRequest, ExportJobProgress, ExportResult } from '../shared/export'
|
||||
import type {
|
||||
@@ -173,7 +178,8 @@ declare global {
|
||||
getAppUpdateState: () => Promise<AppUpdateState>
|
||||
checkAppUpdate: () => Promise<AppUpdateCheckResult>
|
||||
downloadAppUpdate: () => Promise<AppUpdateCheckResult>
|
||||
installAppUpdate: () => Promise<{ success: boolean; error?: string }>
|
||||
installAppUpdate: () => Promise<AppUpdateInstallResult>
|
||||
openAppUpdateDownloadPage: () => Promise<AppUpdateOpenDownloadPageResult>
|
||||
onAppUpdateState: (callback: (state: AppUpdateState) => void) => () => void
|
||||
getCacheSummary: () => Promise<CacheSummary>
|
||||
clearCache: (scope: 'bootstrap' | 'electron' | 'knowledge' | 'all') => Promise<CacheSummary>
|
||||
|
||||
@@ -85,6 +85,7 @@ const api = {
|
||||
checkAppUpdate: () => ipcRenderer.invoke('app-update:check'),
|
||||
downloadAppUpdate: () => ipcRenderer.invoke('app-update:download'),
|
||||
installAppUpdate: () => ipcRenderer.invoke('app-update:install'),
|
||||
openAppUpdateDownloadPage: () => ipcRenderer.invoke('app-update:openDownloadPage'),
|
||||
onAppUpdateState: (callback: (state: AppUpdateState) => void) => {
|
||||
const listener = (_event: Electron.IpcRendererEvent, state: AppUpdateState): void =>
|
||||
callback(state)
|
||||
|
||||
@@ -39,6 +39,7 @@ import type { SelectableReportTemplateId } from '../../shared/report-templates'
|
||||
import { switchGeneratedReportTemplate } from './utils/report-template-switch'
|
||||
import { runtimePlatform } from './utils/runtime-environment'
|
||||
import { useToast } from './components/ui'
|
||||
import { AppUpdatePrompt } from './features/app-update/AppUpdatePrompt'
|
||||
|
||||
const SIDEBAR_MIN_WIDTH = 260
|
||||
const SIDEBAR_MAX_WIDTH = 380
|
||||
@@ -72,7 +73,7 @@ interface SelfInfo {
|
||||
accountRoot: string
|
||||
}
|
||||
|
||||
const MAC_KEY_FAQ_URL = 'https://github.com/Wxw-Gu/WechatExplorer/blob/main/docs/mac-disable-sip.md'
|
||||
const MAC_KEY_FAQ_URL = 'https://github.com/Wxw-Gu/TraceMemo/blob/main/docs/mac-disable-sip.md'
|
||||
const FIRST_USE_WELCOME_SEEN_KEY = 'wxe_first_use_welcome_seen'
|
||||
const MESSAGE_MONITOR_DEBOUNCE_MS = 8000
|
||||
const INITIAL_MESSAGE_COUNT = 20
|
||||
@@ -1468,6 +1469,11 @@ function App(): React.ReactElement {
|
||||
setActivePage('settings')
|
||||
}
|
||||
|
||||
const openUpdateSettings = (): void => {
|
||||
setSettingsCategory('about')
|
||||
setActivePage('settings')
|
||||
}
|
||||
|
||||
const openModelSettings = (): void => {
|
||||
setSettingsCategory('ai-model')
|
||||
setActivePage('settings')
|
||||
@@ -2113,6 +2119,7 @@ function App(): React.ReactElement {
|
||||
appearanceTheme={appearanceSettings.theme}
|
||||
compactMode={appearanceSettings.compactMode}
|
||||
>
|
||||
<AppUpdatePrompt onDownloadStart={openUpdateSettings} onNotice={setReportNotice} />
|
||||
{showFirstUseWelcome && (
|
||||
<FirstUseWelcome
|
||||
onDismiss={dismissFirstUseWelcome}
|
||||
|
||||
@@ -3,8 +3,7 @@ import type { DatabaseKeyEnvironment, WechatAccountCandidate } from '../../../sh
|
||||
import { WINDOWS_VC_RUNTIME_DOWNLOAD_URL } from '../../../shared/windows-runtime'
|
||||
import { Button, IconButton, Input, Tabs, TabsList, TabsTrigger } from './ui'
|
||||
|
||||
const GUIDE_URL =
|
||||
'https://github.com/Wxw-Gu/WechatExplorer/blob/main/docs/user-guide/getting-started.md'
|
||||
const GUIDE_URL = 'https://github.com/Wxw-Gu/TraceMemo/blob/main/docs/user-guide/getting-started.md'
|
||||
|
||||
export type DatabaseConnectionMode = 'automatic' | 'manual'
|
||||
export type DatabaseConnectionStatusKind = 'normal' | 'success' | 'error'
|
||||
|
||||
@@ -8,8 +8,7 @@ interface FirstUseWelcomeProps {
|
||||
onOpenAISettings: () => void
|
||||
}
|
||||
|
||||
const GUIDE_URL =
|
||||
'https://github.com/Wxw-Gu/WechatExplorer/blob/main/docs/user-guide/getting-started.md'
|
||||
const GUIDE_URL = 'https://github.com/Wxw-Gu/TraceMemo/blob/main/docs/user-guide/getting-started.md'
|
||||
|
||||
export function FirstUseWelcome({
|
||||
onDismiss,
|
||||
|
||||
@@ -12,6 +12,6 @@ export const AGENT_INSTALL_TARGETS: { value: AgentInstallTarget; label: string }
|
||||
]
|
||||
|
||||
export const githubLatestUrl =
|
||||
'https://github.com/Wxw-Gu/WechatExplorer/tree/main/docs/skill/tracememo-reader'
|
||||
'https://github.com/Wxw-Gu/TraceMemo/tree/main/docs/skill/tracememo-reader'
|
||||
|
||||
export const githubVersionedInstallUrl: string | null = null
|
||||
@@ -0,0 +1,72 @@
|
||||
import { useState } from 'react'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
Button
|
||||
} from '../../components/ui'
|
||||
import { useAppUpdateState } from './useAppUpdateState'
|
||||
|
||||
export function AppUpdatePrompt({
|
||||
onDownloadStart,
|
||||
onNotice
|
||||
}: {
|
||||
onDownloadStart: () => void
|
||||
onNotice: (message: string) => void
|
||||
}): React.ReactElement {
|
||||
const update = useAppUpdateState()
|
||||
const [dismissedVersion, setDismissedVersion] = useState<string>()
|
||||
const shouldPrompt =
|
||||
update.status === 'available' &&
|
||||
update.source === 'startup' &&
|
||||
Boolean(update.version) &&
|
||||
update.version !== dismissedVersion
|
||||
|
||||
const dismiss = (): void => setDismissedVersion(update.version)
|
||||
|
||||
const continueUpdate = async (): Promise<void> => {
|
||||
dismiss()
|
||||
if (update.delivery === 'release-page') {
|
||||
const result = await window.api.openAppUpdateDownloadPage()
|
||||
if (!result.success) onNotice(result.error || '无法打开下载页面')
|
||||
return
|
||||
}
|
||||
onDownloadStart()
|
||||
const result = await window.api.downloadAppUpdate()
|
||||
if (result && !result.success) onNotice(result.state.message || '更新下载失败')
|
||||
}
|
||||
|
||||
return (
|
||||
<AlertDialog open={shouldPrompt}>
|
||||
<AlertDialogContent aria-label="发现新版本">
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>发现新版本 v{update.version}</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{update.isSimulation
|
||||
? '开发模拟更新已准备好。'
|
||||
: update.delivery === 'release-page'
|
||||
? '有新版本可用,前往 GitHub 下载最新版本。'
|
||||
: '是否立即下载?'}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel asChild>
|
||||
<Button variant="outline" onClick={dismiss}>
|
||||
取消
|
||||
</Button>
|
||||
</AlertDialogCancel>
|
||||
<AlertDialogAction asChild>
|
||||
<Button onClick={() => void continueUpdate()}>
|
||||
{update.delivery === 'release-page' ? '前往下载' : '立即下载'}
|
||||
</Button>
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import type { AppUpdateState } from '../../../../shared/app-update'
|
||||
|
||||
const INITIAL_UPDATE_STATE: AppUpdateState = {
|
||||
status: 'idle',
|
||||
currentVersion: '读取中...',
|
||||
delivery: 'automatic'
|
||||
}
|
||||
|
||||
export function useAppUpdateState(): AppUpdateState {
|
||||
const [state, setState] = useState<AppUpdateState>(INITIAL_UPDATE_STATE)
|
||||
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
void window.api.getAppUpdateState().then((nextState) => {
|
||||
if (active) setState(nextState)
|
||||
})
|
||||
const unsubscribe = window.api.onAppUpdateState((nextState) => {
|
||||
if (active) setState(nextState)
|
||||
})
|
||||
return () => {
|
||||
active = false
|
||||
unsubscribe()
|
||||
}
|
||||
}, [])
|
||||
|
||||
return state
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import type { AppUpdateState } from '../../../../../shared/app-update'
|
||||
import { Button } from '../../../components/ui'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Button, Progress } from '../../../components/ui'
|
||||
import { useAppUpdateState } from '../../app-update/useAppUpdateState'
|
||||
|
||||
const REPOSITORY_URL = 'https://github.com/Wxw-Gu/WechatExplorer'
|
||||
const REPOSITORY_URL = 'https://github.com/Wxw-Gu/TraceMemo'
|
||||
const RELEASES_URL = `${REPOSITORY_URL}/releases`
|
||||
|
||||
function formatBytes(value?: number): string {
|
||||
if (!value) return ''
|
||||
if (value < 1024 * 1024) return `${Math.round(value / 1024)} KB/s`
|
||||
return `${(value / 1024 / 1024).toFixed(1)} MB/s`
|
||||
function formatDataSize(value?: number): string {
|
||||
if (!value) return '0 B'
|
||||
if (value < 1024) return `${value} B`
|
||||
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`
|
||||
return `${(value / 1024 / 1024).toFixed(1)} MB`
|
||||
}
|
||||
|
||||
export function AboutPage({
|
||||
@@ -16,47 +17,49 @@ export function AboutPage({
|
||||
}: {
|
||||
onNotice: (message: string) => void
|
||||
}): React.ReactElement {
|
||||
const [update, setUpdate] = useState<AppUpdateState>({
|
||||
status: 'idle',
|
||||
currentVersion: '读取中...'
|
||||
})
|
||||
const update = useAppUpdateState()
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
void window.api.getAppUpdateState().then((state) => active && setUpdate(state))
|
||||
const unsubscribe = window.api.onAppUpdateState((state) => {
|
||||
if (active) setUpdate(state)
|
||||
})
|
||||
return () => {
|
||||
active = false
|
||||
unsubscribe()
|
||||
}
|
||||
}, [])
|
||||
|
||||
const action = useMemo(() => {
|
||||
if (update.status === 'downloaded') return '重启并安装'
|
||||
if (update.status === 'available') return '下载更新'
|
||||
if (update.status === 'checking' || update.status === 'downloading') return '处理中...'
|
||||
if (update.status === 'available') {
|
||||
return update.delivery === 'release-page' ? '前往下载' : '下载更新'
|
||||
}
|
||||
if (update.status === 'checking') return '正在检查...'
|
||||
if (update.status === 'downloading') return '正在下载...'
|
||||
if (update.status === 'installing') return '正在安装...'
|
||||
if (update.status === 'error') return '重试'
|
||||
return '检查更新'
|
||||
}, [update.status])
|
||||
}, [update.delivery, update.status])
|
||||
|
||||
const runUpdate = async (): Promise<void> => {
|
||||
setBusy(true)
|
||||
try {
|
||||
if (update.status === 'downloaded') {
|
||||
const result = await window.api.installAppUpdate()
|
||||
if (!result.success) onNotice(result.error || '更新安装失败')
|
||||
} else if (update.status === 'available') {
|
||||
await window.api.downloadAppUpdate()
|
||||
} else {
|
||||
await window.api.checkAppUpdate()
|
||||
const result =
|
||||
update.status === 'available' && update.delivery === 'release-page'
|
||||
? await window.api.openAppUpdateDownloadPage()
|
||||
: update.status === 'available'
|
||||
? await window.api.downloadAppUpdate()
|
||||
: await window.api.checkAppUpdate()
|
||||
if (result && !result.success) {
|
||||
const message = 'state' in result ? result.state.message : result.error
|
||||
onNotice(message || '更新操作失败')
|
||||
}
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const installUpdate = async (): Promise<void> => {
|
||||
const result = await window.api.installAppUpdate()
|
||||
if (!result.success) {
|
||||
onNotice(result.error || '更新安装失败')
|
||||
return
|
||||
}
|
||||
if (result.message) onNotice(result.message)
|
||||
}
|
||||
|
||||
const percent = Math.max(0, Math.min(100, Math.round(update.percent || 0)))
|
||||
|
||||
return (
|
||||
<div className="settings-page">
|
||||
<header className="settings-page-header">
|
||||
@@ -82,29 +85,72 @@ export function AboutPage({
|
||||
<section className={`settings-card update-card status-${update.status}`}>
|
||||
<div className="update-card-copy">
|
||||
<strong>
|
||||
{update.status === 'available' || update.status === 'downloaded'
|
||||
? `发现 v${update.version}`
|
||||
: update.message || '检查 GitHub Releases 获取最新版本'}
|
||||
</strong>
|
||||
<span>
|
||||
{update.status === 'downloading'
|
||||
? `正在下载 ${Math.round(update.percent || 0)}% · ${formatBytes(update.bytesPerSecond)}`
|
||||
: '会根据当前系统和 CPU 自动选择对应安装包,安装前会等待你的确认。'}
|
||||
</span>
|
||||
{update.status === 'downloading' && (
|
||||
<div className="update-progress">
|
||||
<i style={{ width: `${update.percent || 0}%` }} />
|
||||
? `正在下载 v${update.version}`
|
||||
: update.status === 'downloaded'
|
||||
? `v${update.version} 已准备完成`
|
||||
: update.status === 'available'
|
||||
? `发现新版本 v${update.version}`
|
||||
: update.message || '检查 GitHub Releases 获取最新版本'}
|
||||
</strong>
|
||||
{update.status === 'downloading' ? (
|
||||
<div className="grid gap-2 pt-1">
|
||||
<span className="text-sm font-semibold text-foreground">{percent}%</span>
|
||||
<Progress value={percent} aria-label={`更新下载进度 ${percent}%`} />
|
||||
<div className="flex justify-between gap-4 text-xs">
|
||||
<span>
|
||||
{formatDataSize(update.transferred)} / {formatDataSize(update.total)}
|
||||
</span>
|
||||
<span>{formatDataSize(update.bytesPerSecond)}/s</span>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<span>
|
||||
{update.status === 'downloaded' ? (
|
||||
`更新将在重启 TraceMemo 后生效。${update.isSimulation ? ' 当前为开发模拟模式。' : ''}`
|
||||
) : update.status === 'available' && update.delivery === 'release-page' ? (
|
||||
<span className="grid gap-1">
|
||||
<span>当前版本:v{update.currentVersion}</span>
|
||||
<span>最新版本:v{update.version}</span>
|
||||
</span>
|
||||
) : (
|
||||
'会根据当前系统和 CPU 自动选择对应安装包,安装前会等待你的确认。'
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-wrap justify-end gap-2">
|
||||
{update.status === 'downloaded' ? (
|
||||
<>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => onNotice('更新已保留,稍后可在此安装')}
|
||||
>
|
||||
稍后
|
||||
</Button>
|
||||
<Button size="sm" onClick={() => void installUpdate()}>
|
||||
立即重启更新
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={
|
||||
busy ||
|
||||
update.status === 'checking' ||
|
||||
update.status === 'downloading' ||
|
||||
update.status === 'installing'
|
||||
}
|
||||
aria-busy={
|
||||
busy || update.status === 'checking' || update.status === 'downloading'
|
||||
}
|
||||
onClick={() => void runUpdate()}
|
||||
>
|
||||
{action}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
disabled={busy || update.status === 'checking' || update.status === 'downloading'}
|
||||
aria-busy={busy || update.status === 'checking' || update.status === 'downloading'}
|
||||
onClick={() => void runUpdate()}
|
||||
>
|
||||
{action}
|
||||
</Button>
|
||||
</section>
|
||||
|
||||
<h2 className="settings-section-heading">支持</h2>
|
||||
|
||||
@@ -629,21 +629,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
.update-progress {
|
||||
height: 6px;
|
||||
overflow: hidden;
|
||||
border-radius: 999px;
|
||||
background: var(--wxex-border);
|
||||
|
||||
i {
|
||||
display: block;
|
||||
height: 100%;
|
||||
border-radius: inherit;
|
||||
background: var(--wxex-brand);
|
||||
transition: width 0.2s ease;
|
||||
}
|
||||
}
|
||||
|
||||
.about-links-card {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
|
||||
@@ -1,25 +1,47 @@
|
||||
export const APP_UPDATE_RELEASES_URL = 'https://github.com/Wxw-Gu/TraceMemo/releases/latest'
|
||||
|
||||
export type AppUpdateStatus =
|
||||
| 'idle'
|
||||
| 'checking'
|
||||
| 'available'
|
||||
| 'not-available'
|
||||
| 'up-to-date'
|
||||
| 'downloading'
|
||||
| 'downloaded'
|
||||
| 'installing'
|
||||
| 'error'
|
||||
| 'unsupported'
|
||||
|
||||
export type AppUpdateCheckSource = 'startup' | 'manual'
|
||||
export type AppUpdateDelivery = 'automatic' | 'release-page'
|
||||
|
||||
export interface AppUpdateState {
|
||||
status: AppUpdateStatus
|
||||
currentVersion: string
|
||||
delivery: AppUpdateDelivery
|
||||
version?: string
|
||||
percent?: number
|
||||
transferred?: number
|
||||
total?: number
|
||||
bytesPerSecond?: number
|
||||
message?: string
|
||||
error?: string
|
||||
source?: AppUpdateCheckSource
|
||||
isSimulation?: boolean
|
||||
}
|
||||
|
||||
export interface AppUpdateCheckResult {
|
||||
success: boolean
|
||||
state: AppUpdateState
|
||||
}
|
||||
|
||||
export interface AppUpdateInstallResult {
|
||||
success: boolean
|
||||
simulated?: boolean
|
||||
message?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
export interface AppUpdateOpenDownloadPageResult {
|
||||
success: boolean
|
||||
error?: string
|
||||
}
|
||||
Reference in new issue
Block a user