add logger
This commit is contained in:
@@ -4,6 +4,13 @@ import { listen } from '@tauri-apps/api/event'
|
||||
|
||||
const sortByName = (a: any, b: any) => a.name.localeCompare(b.name, 'zh-CN', { sensitivity: 'accent' });
|
||||
|
||||
export interface LogEntry {
|
||||
timestamp: string;
|
||||
command: string;
|
||||
output: string;
|
||||
status: 'info' | 'success' | 'error';
|
||||
}
|
||||
|
||||
export const useSoftwareStore = defineStore('software', {
|
||||
state: () => ({
|
||||
essentials: [] as any[],
|
||||
@@ -11,6 +18,7 @@ export const useSoftwareStore = defineStore('software', {
|
||||
allSoftware: [] as any[],
|
||||
selectedEssentialIds: [] as string[],
|
||||
selectedUpdateIds: [] as string[],
|
||||
logs: [] as LogEntry[],
|
||||
loading: false,
|
||||
lastFetched: 0
|
||||
}),
|
||||
@@ -39,15 +47,11 @@ export const useSoftwareStore = defineStore('software', {
|
||||
sortedAllSoftware: (state) => [...state.allSoftware].sort(sortByName)
|
||||
},
|
||||
actions: {
|
||||
// 通用的勾选切换逻辑
|
||||
toggleSelection(id: string, type: 'essential' | 'update') {
|
||||
const list = type === 'essential' ? this.selectedEssentialIds : this.selectedUpdateIds;
|
||||
const index = list.indexOf(id);
|
||||
if (index === -1) {
|
||||
list.push(id);
|
||||
} else {
|
||||
list.splice(index, 1);
|
||||
}
|
||||
if (index === -1) list.push(id);
|
||||
else list.splice(index, 1);
|
||||
},
|
||||
selectAll(type: 'essential' | 'update') {
|
||||
if (type === 'essential') {
|
||||
@@ -79,7 +83,6 @@ export const useSoftwareStore = defineStore('software', {
|
||||
try {
|
||||
const res = await invoke('get_updates')
|
||||
this.updates = res as any[]
|
||||
// 刷新数据后,如果没选过,默认全选
|
||||
if (this.selectedUpdateIds.length === 0) this.selectAll('update');
|
||||
} finally {
|
||||
this.loading = false
|
||||
@@ -115,7 +118,6 @@ export const useSoftwareStore = defineStore('software', {
|
||||
this.allSoftware = all as any[];
|
||||
this.updates = updates as any[];
|
||||
this.lastFetched = Date.now();
|
||||
// 如果没选过,默认全选
|
||||
if (this.selectedEssentialIds.length === 0) this.selectAll('essential');
|
||||
} finally {
|
||||
this.loading = false;
|
||||
@@ -144,11 +146,17 @@ export const useSoftwareStore = defineStore('software', {
|
||||
}
|
||||
if (status === 'success') {
|
||||
this.lastFetched = 0;
|
||||
// 安装成功后从选择列表中移除
|
||||
this.selectedEssentialIds = this.selectedEssentialIds.filter(i => i !== id);
|
||||
this.selectedUpdateIds = this.selectedUpdateIds.filter(i => i !== id);
|
||||
}
|
||||
})
|
||||
|
||||
// 监听日志事件
|
||||
listen('log-event', (event: any) => {
|
||||
this.logs.unshift(event.payload as LogEntry);
|
||||
// 限制日志条数,防止内存溢出
|
||||
if (this.logs.length > 200) this.logs.pop();
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user