59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
|
|
export interface UpdateStatus {
|
|
status: 'idle' | 'checking' | 'available' | 'not-available' | 'downloading' | 'downloaded' | 'error';
|
|
currentVersion?: string;
|
|
error?: string;
|
|
progress?: number;
|
|
}
|
|
|
|
export const useAppStore = defineStore('app', {
|
|
state: () => ({
|
|
updateStatus: {
|
|
status: 'idle' as const,
|
|
} as UpdateStatus,
|
|
updateDialogVisible: false,
|
|
downloadProgress: 0,
|
|
}),
|
|
|
|
getters: {
|
|
isUpdateAvailable: state => state.updateStatus.status === 'available',
|
|
isCheckingForUpdates: state => state.updateStatus.status === 'checking',
|
|
hasUpdateError: state => state.updateStatus.status === 'error',
|
|
},
|
|
|
|
actions: {
|
|
setUpdateStatus(status: UpdateStatus['status'], currentVersion?: string, error?: string) {
|
|
this.updateStatus = { status, currentVersion, error };
|
|
},
|
|
|
|
showUpdateDialog() {
|
|
this.updateDialogVisible = true;
|
|
},
|
|
|
|
hideUpdateDialog() {
|
|
this.updateDialogVisible = false;
|
|
},
|
|
|
|
setDownloadProgress(progress: number) {
|
|
this.downloadProgress = progress;
|
|
},
|
|
|
|
// 启动更新下载
|
|
async startUpdateDownload() {
|
|
try {
|
|
this.setUpdateStatus('downloading');
|
|
const result = await window.mainApi.invoke('msgDownloadUpdate');
|
|
|
|
if (result.success) {
|
|
this.setUpdateStatus('downloaded');
|
|
} else {
|
|
this.setUpdateStatus('error', undefined, result.error);
|
|
}
|
|
} catch (error) {
|
|
this.setUpdateStatus('error', undefined, error instanceof Error ? error.message : String(error));
|
|
}
|
|
},
|
|
},
|
|
});
|