refactor 2
This commit is contained in:
@@ -2,25 +2,6 @@ import { defineStore } from 'pinia'
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { listen } from '@tauri-apps/api/event'
|
||||
|
||||
const sortByName = (a: any, b: any) => a.name.localeCompare(b.name, 'zh-CN', { sensitivity: 'accent' });
|
||||
|
||||
// 版本比对工具函数
|
||||
const compareVersions = (v1: string | null | undefined, v2: string | null | undefined) => {
|
||||
if (!v1 || !v2) return 0;
|
||||
if (v1 === v2) return 0;
|
||||
const cleanV = (v: string) => v.replace(/^v/i, '').split(/[-+]/)[0].split('.');
|
||||
const p1 = cleanV(v1);
|
||||
const p2 = cleanV(v2);
|
||||
const len = Math.max(p1.length, p2.length);
|
||||
for (let i = 0; i < len; i++) {
|
||||
const n1 = parseInt(p1[i] || '0', 10);
|
||||
const n2 = parseInt(p2[i] || '0', 10);
|
||||
if (n1 < n2) return -1;
|
||||
if (n1 > n2) return 1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
export interface LogEntry {
|
||||
id: string; // 日志唯一标识
|
||||
timestamp: string;
|
||||
@@ -34,6 +15,18 @@ interface SyncEssentialsResult {
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface DashboardSnapshot {
|
||||
essentials_version: string;
|
||||
essentials: any[];
|
||||
updates: any[];
|
||||
installed_software: any[];
|
||||
}
|
||||
|
||||
interface EssentialsStatusResponse extends Array<any> {
|
||||
0: string;
|
||||
1: any[];
|
||||
}
|
||||
|
||||
export const useSoftwareStore = defineStore('software', {
|
||||
state: () => ({
|
||||
essentials: [] as any[],
|
||||
@@ -58,46 +51,14 @@ export const useSoftwareStore = defineStore('software', {
|
||||
getters: {
|
||||
mergedEssentials: (state) => {
|
||||
return state.essentials.map(item => {
|
||||
const installedInfo = state.allSoftware.find(s => s.id.toLowerCase() === item.id.toLowerCase());
|
||||
const wingetUpdate = state.updates.find(s => s.id.toLowerCase() === item.id.toLowerCase());
|
||||
|
||||
const task = state.activeTasks[item.id];
|
||||
const isInstalled = !!installedInfo;
|
||||
const currentVersion = installedInfo?.version;
|
||||
const recommendedVersion = item.version; // 清单里的推荐版本
|
||||
const availableVersion = wingetUpdate?.available_version; // Winget 查到的最新版
|
||||
|
||||
let displayStatus = task ? task.status : 'idle';
|
||||
let actionLabel = '安装';
|
||||
let targetVersion = recommendedVersion || availableVersion;
|
||||
|
||||
if (isInstalled) {
|
||||
const comp = compareVersions(currentVersion, recommendedVersion);
|
||||
if (comp >= 0) {
|
||||
displayStatus = task ? task.status : 'installed';
|
||||
actionLabel = '已安装';
|
||||
targetVersion = undefined;
|
||||
} else {
|
||||
actionLabel = '更新';
|
||||
targetVersion = recommendedVersion;
|
||||
}
|
||||
} else {
|
||||
actionLabel = '安装';
|
||||
targetVersion = recommendedVersion || availableVersion;
|
||||
}
|
||||
|
||||
// 获取偏好,默认开启
|
||||
const enablePostInstall = state.postInstallPrefs[item.id] !== false;
|
||||
const baseStatus = item.actionLabel === '已安装' ? 'installed' : 'idle';
|
||||
|
||||
return {
|
||||
...item,
|
||||
version: currentVersion,
|
||||
recommended_version: recommendedVersion,
|
||||
available_version: availableVersion,
|
||||
status: displayStatus,
|
||||
status: task ? task.status : baseStatus,
|
||||
progress: task ? task.progress : 0,
|
||||
actionLabel,
|
||||
targetVersion,
|
||||
enablePostInstall
|
||||
};
|
||||
});
|
||||
@@ -110,11 +71,9 @@ export const useSoftwareStore = defineStore('software', {
|
||||
...item,
|
||||
status: task ? task.status : 'idle',
|
||||
progress: task ? task.progress : 0,
|
||||
actionLabel: '更新',
|
||||
targetVersion: item.available_version,
|
||||
enablePostInstall
|
||||
};
|
||||
}).sort(sortByName);
|
||||
}).sort((a, b) => a.name.localeCompare(b.name, 'zh-CN', { sensitivity: 'accent' }));
|
||||
},
|
||||
isBusy: (state) => {
|
||||
return state.loading || Object.values(state.activeTasks).some(task =>
|
||||
@@ -183,18 +142,18 @@ export const useSoftwareStore = defineStore('software', {
|
||||
},
|
||||
|
||||
async fetchEssentials() {
|
||||
let repo = await invoke('get_essentials') as any;
|
||||
if (!repo) {
|
||||
let response = await invoke('get_essentials_status') as EssentialsStatusResponse;
|
||||
if ((!response || !response[1]) && !(await invoke('get_essentials') as any)) {
|
||||
try {
|
||||
await invoke('sync_essentials');
|
||||
repo = await invoke('get_essentials') as any;
|
||||
response = await invoke('get_essentials_status') as EssentialsStatusResponse;
|
||||
} catch (err) {
|
||||
console.error('Initial sync failed:', err);
|
||||
}
|
||||
}
|
||||
if (repo) {
|
||||
this.essentials = repo.essentials;
|
||||
this.essentialsVersion = repo.version;
|
||||
if (response && Array.isArray(response[1])) {
|
||||
this.essentialsVersion = response[0] || '';
|
||||
this.essentials = response[1];
|
||||
} else {
|
||||
this.essentials = [];
|
||||
this.essentialsVersion = '';
|
||||
@@ -204,7 +163,7 @@ export const useSoftwareStore = defineStore('software', {
|
||||
if (this.isBusy) return;
|
||||
this.loading = true
|
||||
try {
|
||||
const res = await invoke('get_updates')
|
||||
const res = await invoke('get_update_candidates')
|
||||
this.updates = res as any[]
|
||||
await this.loadIconsForUpdates()
|
||||
if (this.selectedUpdateIds.length === 0) this.selectAll('update');
|
||||
@@ -225,13 +184,11 @@ export const useSoftwareStore = defineStore('software', {
|
||||
async fetchAllData() {
|
||||
this.loading = true;
|
||||
try {
|
||||
await this.fetchEssentials();
|
||||
const [all, updates] = await Promise.all([
|
||||
invoke('get_installed_software'),
|
||||
invoke('get_updates')
|
||||
]);
|
||||
this.allSoftware = all as any[];
|
||||
this.updates = updates as any[];
|
||||
const snapshot = await invoke('get_dashboard_snapshot') as DashboardSnapshot;
|
||||
this.essentialsVersion = snapshot.essentials_version;
|
||||
this.essentials = snapshot.essentials;
|
||||
this.allSoftware = snapshot.installed_software;
|
||||
this.updates = snapshot.updates;
|
||||
await this.loadIconsForUpdates()
|
||||
this.lastFetched = Date.now();
|
||||
if (this.selectedEssentialIds.length === 0) this.selectAll('essential');
|
||||
|
||||
Reference in New Issue
Block a user