This commit is contained in:
Julian Freeman
2026-03-14 18:49:38 -04:00
parent 2955e1eada
commit 8d700770aa
3 changed files with 158 additions and 42 deletions

View File

@@ -2,6 +2,8 @@ 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' });
export const useSoftwareStore = defineStore('software', {
state: () => ({
essentials: [] as any[],
@@ -12,7 +14,7 @@ export const useSoftwareStore = defineStore('software', {
}),
getters: {
mergedEssentials: (state) => {
return state.essentials.map(item => {
const items = state.essentials.map(item => {
const isInstalled = state.allSoftware.some(s => s.id.toLowerCase() === item.id.toLowerCase());
const hasUpdate = state.updates.some(s => s.id.toLowerCase() === item.id.toLowerCase());
@@ -34,7 +36,10 @@ export const useSoftwareStore = defineStore('software', {
actionLabel
};
});
}
return items; // Essentials 通常保持自定义顺序
},
sortedUpdates: (state) => [...state.updates].sort(sortByName),
sortedAllSoftware: (state) => [...state.allSoftware].sort(sortByName)
},
actions: {
async fetchEssentials() {
@@ -42,13 +47,21 @@ export const useSoftwareStore = defineStore('software', {
},
async fetchUpdates() {
this.loading = true
this.updates = await invoke('get_updates')
this.loading = false
try {
const res = await invoke('get_updates')
this.updates = res as any[]
} finally {
this.loading = false
}
},
async fetchAll() {
this.loading = true
this.allSoftware = await invoke('get_all_software')
this.loading = false
try {
const res = await invoke('get_all_software')
this.allSoftware = res as any[]
} finally {
this.loading = false
}
},
// 智能同步:如果数据较新,则直接返回
async syncDataIfNeeded(force = false) {
@@ -56,7 +69,6 @@ export const useSoftwareStore = defineStore('software', {
const CACHE_TIMEOUT = 5 * 60 * 1000; // 5 分钟缓存
if (!force && this.allSoftware.length > 0 && (now - this.lastFetched < CACHE_TIMEOUT)) {
// 如果不是强制刷新,且数据在 5 分钟内,且已经有数据,则跳过
if (this.essentials.length === 0) await this.fetchEssentials();
return;
}
@@ -85,13 +97,11 @@ export const useSoftwareStore = defineStore('software', {
await invoke('install_software', { id })
},
findSoftware(id: string) {
// 这里的逻辑会从多个列表中查找对象
return this.essentials.find(s => s.id === id) ||
this.updates.find(s => s.id === id) ||
this.allSoftware.find(s => s.id === id)
},
initListener() {
// 避免重复监听
if ((window as any).__tauri_listener_init) return;
(window as any).__tauri_listener_init = true;
@@ -103,7 +113,6 @@ export const useSoftwareStore = defineStore('software', {
software.progress = progress
}
// 如果安装成功,标记数据过期,以便下次切换或刷新时同步最新状态
if (status === 'success') {
this.lastFetched = 0;
}