refactor 3
This commit is contained in:
130
src/store/catalog.ts
Normal file
130
src/store/catalog.ts
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { invoke } from '@tauri-apps/api/core'
|
||||||
|
|
||||||
|
import type { AppSettings, DashboardSnapshot, SoftwareListItem, SyncEssentialsResult, UpdateCandidate } from './types'
|
||||||
|
|
||||||
|
type EssentialsStatusResponse = [string, SoftwareListItem[]]
|
||||||
|
|
||||||
|
export const useCatalogStore = defineStore('catalog', {
|
||||||
|
state: () => ({
|
||||||
|
essentials: [] as SoftwareListItem[],
|
||||||
|
essentialsVersion: '',
|
||||||
|
updates: [] as UpdateCandidate[],
|
||||||
|
allSoftware: [] as SoftwareListItem[],
|
||||||
|
settings: {
|
||||||
|
repo_url: 'https://karlblue.github.io/winget-repo'
|
||||||
|
} as AppSettings,
|
||||||
|
loading: false,
|
||||||
|
isInitialized: false,
|
||||||
|
initStatus: '正在检查系统环境...',
|
||||||
|
lastFetched: 0
|
||||||
|
}),
|
||||||
|
actions: {
|
||||||
|
async initializeApp() {
|
||||||
|
if (this.isInitialized) return
|
||||||
|
this.initStatus = '正在加载应用配置...'
|
||||||
|
try {
|
||||||
|
this.settings = await invoke('get_settings')
|
||||||
|
this.initStatus = '正在同步 Winget 模块...'
|
||||||
|
await invoke('initialize_app')
|
||||||
|
this.isInitialized = true
|
||||||
|
} catch {
|
||||||
|
this.initStatus = '环境配置失败,请检查运行日志'
|
||||||
|
setTimeout(() => { this.isInitialized = true }, 2000)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async saveSettings(newSettings: AppSettings) {
|
||||||
|
await invoke('save_settings', { settings: newSettings })
|
||||||
|
this.settings = newSettings
|
||||||
|
},
|
||||||
|
|
||||||
|
async syncEssentials() {
|
||||||
|
this.loading = true
|
||||||
|
try {
|
||||||
|
const result = await invoke('sync_essentials') as SyncEssentialsResult
|
||||||
|
await this.fetchEssentials()
|
||||||
|
return result
|
||||||
|
} finally {
|
||||||
|
this.loading = false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async fetchEssentials() {
|
||||||
|
let response = await invoke('get_essentials_status') as EssentialsStatusResponse
|
||||||
|
if ((!response || !response[1]) && !(await invoke('get_essentials') as unknown)) {
|
||||||
|
try {
|
||||||
|
await invoke('sync_essentials')
|
||||||
|
response = await invoke('get_essentials_status') as EssentialsStatusResponse
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Initial sync failed:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (response && Array.isArray(response[1])) {
|
||||||
|
this.essentialsVersion = response[0] || ''
|
||||||
|
this.essentials = response[1]
|
||||||
|
} else {
|
||||||
|
this.essentials = []
|
||||||
|
this.essentialsVersion = ''
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async fetchUpdates() {
|
||||||
|
this.loading = true
|
||||||
|
try {
|
||||||
|
const res = await invoke('get_update_candidates')
|
||||||
|
this.updates = res as UpdateCandidate[]
|
||||||
|
await this.loadIconsForUpdates()
|
||||||
|
} finally {
|
||||||
|
this.loading = false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async syncDataIfNeeded(force = false) {
|
||||||
|
const now = Date.now()
|
||||||
|
const cacheTimeout = 5 * 60 * 1000
|
||||||
|
if (!force && this.allSoftware.length > 0 && (now - this.lastFetched < cacheTimeout)) {
|
||||||
|
if (this.essentials.length === 0) await this.fetchEssentials()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await this.fetchAllData()
|
||||||
|
},
|
||||||
|
|
||||||
|
async fetchAllData() {
|
||||||
|
this.loading = true
|
||||||
|
try {
|
||||||
|
const snapshot = await invoke('get_dashboard_snapshot') as DashboardSnapshot
|
||||||
|
this.applyDashboardSnapshot(snapshot)
|
||||||
|
await this.loadIconsForUpdates()
|
||||||
|
this.lastFetched = Date.now()
|
||||||
|
} finally {
|
||||||
|
this.loading = false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
applyDashboardSnapshot(snapshot: DashboardSnapshot) {
|
||||||
|
this.essentialsVersion = snapshot.essentials_version
|
||||||
|
this.essentials = snapshot.essentials
|
||||||
|
this.updates = snapshot.updates
|
||||||
|
this.allSoftware = snapshot.installed_software
|
||||||
|
},
|
||||||
|
|
||||||
|
async loadIconsForUpdates() {
|
||||||
|
const targets = this.updates.filter(item => !item.icon_url && item.id && item.name)
|
||||||
|
await Promise.allSettled(targets.map(async (item) => {
|
||||||
|
const iconUrl = await invoke('get_software_icon', { id: item.id, name: item.name }) as string | null
|
||||||
|
if (!iconUrl) return
|
||||||
|
const target = this.updates.find(update => update.id === item.id)
|
||||||
|
if (target) {
|
||||||
|
target.icon_url = iconUrl
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -1,322 +1,160 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore, storeToRefs } from 'pinia'
|
||||||
import { invoke } from '@tauri-apps/api/core'
|
import { computed } from 'vue'
|
||||||
import { listen } from '@tauri-apps/api/event'
|
|
||||||
|
|
||||||
export interface LogEntry {
|
import { useCatalogStore } from './catalog'
|
||||||
id: string; // 日志唯一标识
|
import { useTaskRuntimeStore } from './taskRuntime'
|
||||||
timestamp: string;
|
|
||||||
command: string;
|
|
||||||
output: string;
|
|
||||||
status: 'info' | 'success' | 'error';
|
|
||||||
}
|
|
||||||
|
|
||||||
interface SyncEssentialsResult {
|
export const useSoftwareStore = defineStore('software', () => {
|
||||||
status: 'updated' | 'cache_used';
|
const catalog = useCatalogStore()
|
||||||
message: string;
|
const taskRuntime = useTaskRuntimeStore()
|
||||||
}
|
|
||||||
|
|
||||||
interface DashboardSnapshot {
|
const {
|
||||||
essentials_version: string;
|
essentials,
|
||||||
essentials: any[];
|
essentialsVersion,
|
||||||
updates: any[];
|
updates,
|
||||||
installed_software: any[];
|
allSoftware,
|
||||||
}
|
settings,
|
||||||
|
loading,
|
||||||
|
isInitialized,
|
||||||
|
initStatus
|
||||||
|
} = storeToRefs(catalog)
|
||||||
|
|
||||||
interface EssentialsStatusResponse extends Array<any> {
|
const {
|
||||||
0: string;
|
activeTasks,
|
||||||
1: any[];
|
selectedEssentialIds,
|
||||||
}
|
selectedUpdateIds,
|
||||||
|
logs,
|
||||||
|
postInstallPrefs
|
||||||
|
} = storeToRefs(taskRuntime)
|
||||||
|
|
||||||
export const useSoftwareStore = defineStore('software', {
|
const mergedEssentials = computed(() => essentials.value.map(item => {
|
||||||
state: () => ({
|
const task = activeTasks.value[item.id]
|
||||||
essentials: [] as any[],
|
const enablePostInstall = postInstallPrefs.value[item.id] !== false
|
||||||
essentialsVersion: '',
|
const baseStatus = item.action_label === '已安装' ? 'installed' : 'idle'
|
||||||
updates: [] as any[],
|
|
||||||
allSoftware: [] as any[],
|
|
||||||
selectedEssentialIds: [] as string[],
|
|
||||||
selectedUpdateIds: [] as string[],
|
|
||||||
logs: [] as LogEntry[],
|
|
||||||
settings: {
|
|
||||||
repo_url: 'https://karlblue.github.io/winget-repo'
|
|
||||||
},
|
|
||||||
activeTasks: {} as Record<string, { status: string, progress: number, targetVersion?: string }>,
|
|
||||||
loading: false,
|
|
||||||
isInitialized: false,
|
|
||||||
initStatus: '正在检查系统环境...',
|
|
||||||
lastFetched: 0,
|
|
||||||
refreshTimer: null as any,
|
|
||||||
batchQueue: [] as string[],
|
|
||||||
postInstallPrefs: {} as Record<string, boolean> // 记录用户对每个软件后安装配置的偏好
|
|
||||||
}),
|
|
||||||
getters: {
|
|
||||||
mergedEssentials: (state) => {
|
|
||||||
return state.essentials.map(item => {
|
|
||||||
const task = state.activeTasks[item.id];
|
|
||||||
const enablePostInstall = state.postInstallPrefs[item.id] !== false;
|
|
||||||
const baseStatus = item.actionLabel === '已安装' ? 'installed' : 'idle';
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
status: task ? task.status : baseStatus,
|
version: item.version ?? undefined,
|
||||||
progress: task ? task.progress : 0,
|
recommended_version: item.recommended_version ?? undefined,
|
||||||
enablePostInstall
|
available_version: item.available_version ?? undefined,
|
||||||
};
|
icon_url: item.icon_url ?? undefined,
|
||||||
});
|
manifest_url: item.manifest_url ?? undefined,
|
||||||
},
|
post_install_url: item.post_install_url ?? undefined,
|
||||||
sortedUpdates: (state) => {
|
actionLabel: item.action_label,
|
||||||
return [...state.updates].map(item => {
|
targetVersion: item.target_version ?? undefined,
|
||||||
const task = state.activeTasks[item.id];
|
status: task ? task.status : baseStatus,
|
||||||
const enablePostInstall = state.postInstallPrefs[item.id] !== false;
|
progress: task ? task.progress : 0,
|
||||||
return {
|
enablePostInstall
|
||||||
...item,
|
|
||||||
status: task ? task.status : 'idle',
|
|
||||||
progress: task ? task.progress : 0,
|
|
||||||
enablePostInstall
|
|
||||||
};
|
|
||||||
}).sort((a, b) => a.name.localeCompare(b.name, 'zh-CN', { sensitivity: 'accent' }));
|
|
||||||
},
|
|
||||||
isBusy: (state) => {
|
|
||||||
return state.loading || Object.values(state.activeTasks).some(task =>
|
|
||||||
task.status === 'pending' || task.status === 'installing'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
}))
|
||||||
actions: {
|
|
||||||
async initializeApp() {
|
|
||||||
if (this.isInitialized) return;
|
|
||||||
this.initStatus = '正在加载应用配置...';
|
|
||||||
try {
|
|
||||||
this.settings = await invoke('get_settings');
|
|
||||||
this.initStatus = '正在同步 Winget 模块...';
|
|
||||||
await invoke('initialize_app');
|
|
||||||
this.isInitialized = true;
|
|
||||||
} catch (err) {
|
|
||||||
this.initStatus = '环境配置失败,请检查运行日志';
|
|
||||||
setTimeout(() => { this.isInitialized = true; }, 2000);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async saveSettings(newSettings: any) {
|
const sortedUpdates = computed(() => [...updates.value].map(item => {
|
||||||
await invoke('save_settings', { settings: newSettings });
|
const task = activeTasks.value[item.id]
|
||||||
this.settings = newSettings;
|
const enablePostInstall = postInstallPrefs.value[item.id] !== false
|
||||||
},
|
|
||||||
|
|
||||||
async syncEssentials() {
|
return {
|
||||||
this.loading = true;
|
...item,
|
||||||
try {
|
version: item.version ?? undefined,
|
||||||
const result = await invoke('sync_essentials') as SyncEssentialsResult;
|
recommended_version: item.recommended_version ?? undefined,
|
||||||
await this.fetchEssentials();
|
available_version: item.available_version ?? undefined,
|
||||||
return result;
|
icon_url: item.icon_url ?? undefined,
|
||||||
} finally {
|
manifest_url: item.manifest_url ?? undefined,
|
||||||
this.loading = false;
|
post_install_url: item.post_install_url ?? undefined,
|
||||||
}
|
actionLabel: item.action_label,
|
||||||
},
|
targetVersion: item.target_version ?? undefined,
|
||||||
|
status: task ? task.status : 'idle',
|
||||||
|
progress: task ? task.progress : 0,
|
||||||
|
enablePostInstall
|
||||||
|
}
|
||||||
|
}).sort((a, b) => a.name.localeCompare(b.name, 'zh-CN', { sensitivity: 'accent' })))
|
||||||
|
|
||||||
toggleSelection(id: string, type: 'essential' | 'update') {
|
const isBusy = computed(() => loading.value || taskRuntime.isTaskBusy)
|
||||||
if (this.isBusy) return;
|
|
||||||
const list = type === 'essential' ? this.selectedEssentialIds : this.selectedUpdateIds;
|
|
||||||
const index = list.indexOf(id);
|
|
||||||
if (index === -1) list.push(id);
|
|
||||||
else list.splice(index, 1);
|
|
||||||
},
|
|
||||||
selectAll(type: 'essential' | 'update') {
|
|
||||||
if (type === 'essential') {
|
|
||||||
const selectable = this.mergedEssentials.filter(s => s.actionLabel !== '已安装');
|
|
||||||
this.selectedEssentialIds = selectable.map(s => s.id);
|
|
||||||
} else {
|
|
||||||
this.selectedUpdateIds = this.updates.map(s => s.id);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
deselectAll(type: 'essential' | 'update') {
|
|
||||||
if (type === 'essential') this.selectedEssentialIds = [];
|
|
||||||
else this.selectedUpdateIds = [];
|
|
||||||
},
|
|
||||||
invertSelection(type: 'essential' | 'update') {
|
|
||||||
if (type === 'essential') {
|
|
||||||
const selectable = this.mergedEssentials.filter(s => s.actionLabel !== '已安装').map(s => s.id);
|
|
||||||
this.selectedEssentialIds = selectable.filter(id => !this.selectedEssentialIds.includes(id));
|
|
||||||
} else {
|
|
||||||
const selectable = this.updates.map(s => s.id);
|
|
||||||
this.selectedUpdateIds = selectable.filter(id => !this.selectedUpdateIds.includes(id));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async fetchEssentials() {
|
const toggleSelection = (id: string, type: 'essential' | 'update') => {
|
||||||
let response = await invoke('get_essentials_status') as EssentialsStatusResponse;
|
if (isBusy.value) return
|
||||||
if ((!response || !response[1]) && !(await invoke('get_essentials') as any)) {
|
taskRuntime.toggleSelection(id, type)
|
||||||
try {
|
}
|
||||||
await invoke('sync_essentials');
|
|
||||||
response = await invoke('get_essentials_status') as EssentialsStatusResponse;
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Initial sync failed:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (response && Array.isArray(response[1])) {
|
|
||||||
this.essentialsVersion = response[0] || '';
|
|
||||||
this.essentials = response[1];
|
|
||||||
} else {
|
|
||||||
this.essentials = [];
|
|
||||||
this.essentialsVersion = '';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async fetchUpdates() {
|
|
||||||
if (this.isBusy) return;
|
|
||||||
this.loading = true
|
|
||||||
try {
|
|
||||||
const res = await invoke('get_update_candidates')
|
|
||||||
this.updates = res as any[]
|
|
||||||
await this.loadIconsForUpdates()
|
|
||||||
if (this.selectedUpdateIds.length === 0) this.selectAll('update');
|
|
||||||
} finally {
|
|
||||||
this.loading = false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async syncDataIfNeeded(force = false) {
|
|
||||||
if (this.isBusy) return;
|
|
||||||
const now = Date.now();
|
|
||||||
const CACHE_TIMEOUT = 5 * 60 * 1000;
|
|
||||||
if (!force && this.allSoftware.length > 0 && (now - this.lastFetched < CACHE_TIMEOUT)) {
|
|
||||||
if (this.essentials.length === 0) await this.fetchEssentials();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await this.fetchAllData();
|
|
||||||
},
|
|
||||||
async fetchAllData() {
|
|
||||||
this.loading = true;
|
|
||||||
try {
|
|
||||||
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');
|
|
||||||
} finally {
|
|
||||||
this.loading = false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async install(id: string, targetVersion?: string) {
|
|
||||||
const software = this.findSoftware(id)
|
|
||||||
if (software) {
|
|
||||||
// 根据偏好决定是否开启后安装配置
|
|
||||||
const enablePostInstall = this.postInstallPrefs[id] !== false;
|
|
||||||
|
|
||||||
this.activeTasks[id] = { status: 'pending', progress: 0, targetVersion };
|
const selectAll = (type: 'essential' | 'update') => {
|
||||||
try {
|
if (type === 'essential') {
|
||||||
await invoke('install_software', {
|
const selectable = mergedEssentials.value.filter(item => item.actionLabel !== '已安装')
|
||||||
task: {
|
taskRuntime.setSelection('essential', selectable.map(item => item.id))
|
||||||
id,
|
} else {
|
||||||
version: targetVersion,
|
taskRuntime.setSelection('update', updates.value.map(item => item.id))
|
||||||
use_manifest: software.use_manifest || false,
|
|
||||||
manifest_url: software.manifest_url || null,
|
|
||||||
enable_post_install: enablePostInstall
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Invoke install failed:', err);
|
|
||||||
this.activeTasks[id] = { status: 'error', progress: 0 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
togglePostInstallPref(id: string) {
|
|
||||||
const current = this.postInstallPrefs[id] !== false;
|
|
||||||
this.postInstallPrefs[id] = !current;
|
|
||||||
},
|
|
||||||
|
|
||||||
startBatch(ids: string[]) {
|
|
||||||
this.batchQueue = [...ids];
|
|
||||||
},
|
|
||||||
|
|
||||||
scheduleDataRefresh() {
|
|
||||||
if (this.refreshTimer) clearTimeout(this.refreshTimer);
|
|
||||||
|
|
||||||
this.refreshTimer = setTimeout(async () => {
|
|
||||||
await this.fetchAllData();
|
|
||||||
Object.keys(this.activeTasks).forEach(id => {
|
|
||||||
const status = this.activeTasks[id].status;
|
|
||||||
if (status === 'success' || status === 'error') {
|
|
||||||
delete this.activeTasks[id];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.refreshTimer = null;
|
|
||||||
}, 2000);
|
|
||||||
},
|
|
||||||
|
|
||||||
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)
|
|
||||||
},
|
|
||||||
async loadIconsForUpdates() {
|
|
||||||
const targets = this.updates.filter(item => !item.icon_url && item.id && item.name);
|
|
||||||
await Promise.allSettled(targets.map(async (item) => {
|
|
||||||
const iconUrl = await invoke('get_software_icon', { id: item.id, name: item.name }) as string | null;
|
|
||||||
if (!iconUrl) return;
|
|
||||||
const target = this.updates.find(update => update.id === item.id);
|
|
||||||
if (target) {
|
|
||||||
target.icon_url = iconUrl;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
},
|
|
||||||
initListener() {
|
|
||||||
if ((window as any).__tauri_listener_init) return;
|
|
||||||
(window as any).__tauri_listener_init = true;
|
|
||||||
|
|
||||||
listen('install-status', async (event: any) => {
|
|
||||||
const { id, status, progress } = event.payload
|
|
||||||
const task = this.activeTasks[id];
|
|
||||||
|
|
||||||
this.activeTasks[id] = { status, progress, targetVersion: task?.targetVersion };
|
|
||||||
|
|
||||||
if (status === 'success' || status === 'error') {
|
|
||||||
if (status === 'success') {
|
|
||||||
try {
|
|
||||||
const latestInfo = await invoke('get_software_info', { id }) as any;
|
|
||||||
if (latestInfo) {
|
|
||||||
const index = this.allSoftware.findIndex(s => s.id.toLowerCase() === id.toLowerCase());
|
|
||||||
if (index !== -1) {
|
|
||||||
this.allSoftware[index] = { ...this.allSoftware[index], ...latestInfo };
|
|
||||||
} else {
|
|
||||||
this.allSoftware.push(latestInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Partial refresh failed:', err);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.selectedEssentialIds = this.selectedEssentialIds.filter(i => i !== id);
|
|
||||||
this.selectedUpdateIds = this.selectedUpdateIds.filter(i => i !== id);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
if (this.activeTasks[id]?.status === 'success') {
|
|
||||||
delete this.activeTasks[id];
|
|
||||||
}
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
const index = this.batchQueue.indexOf(id);
|
|
||||||
if (index !== -1) {
|
|
||||||
this.batchQueue.splice(index, 1);
|
|
||||||
if (this.batchQueue.length === 0) {
|
|
||||||
this.scheduleDataRefresh();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
listen('log-event', (event: any) => {
|
|
||||||
const payload = event.payload as LogEntry;
|
|
||||||
const existingLog = this.logs.find(l => l.id === payload.id);
|
|
||||||
if (existingLog) {
|
|
||||||
if (payload.output) existingLog.output += '\n' + payload.output;
|
|
||||||
if (payload.status !== 'info') existingLog.status = payload.status;
|
|
||||||
} else {
|
|
||||||
this.logs.unshift(payload);
|
|
||||||
if (this.logs.length > 100) this.logs.pop();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deselectAll = (type: 'essential' | 'update') => {
|
||||||
|
taskRuntime.setSelection(type, [])
|
||||||
|
}
|
||||||
|
|
||||||
|
const invertSelection = (type: 'essential' | 'update') => {
|
||||||
|
if (type === 'essential') {
|
||||||
|
const selectable = mergedEssentials.value
|
||||||
|
.filter(item => item.actionLabel !== '已安装')
|
||||||
|
.map(item => item.id)
|
||||||
|
taskRuntime.setSelection(
|
||||||
|
'essential',
|
||||||
|
selectable.filter(id => !selectedEssentialIds.value.includes(id))
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
const selectable = updates.value.map(item => item.id)
|
||||||
|
taskRuntime.setSelection(
|
||||||
|
'update',
|
||||||
|
selectable.filter(id => !selectedUpdateIds.value.includes(id))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchUpdates = async () => {
|
||||||
|
if (isBusy.value) return
|
||||||
|
await catalog.fetchUpdates()
|
||||||
|
if (selectedUpdateIds.value.length === 0) selectAll('update')
|
||||||
|
}
|
||||||
|
|
||||||
|
const syncDataIfNeeded = async (force = false) => {
|
||||||
|
if (isBusy.value) return
|
||||||
|
await catalog.syncDataIfNeeded(force)
|
||||||
|
if (selectedEssentialIds.value.length === 0) selectAll('essential')
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchAllData = async () => {
|
||||||
|
await catalog.fetchAllData()
|
||||||
|
if (selectedEssentialIds.value.length === 0) selectAll('essential')
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
essentials,
|
||||||
|
essentialsVersion,
|
||||||
|
updates,
|
||||||
|
allSoftware,
|
||||||
|
selectedEssentialIds,
|
||||||
|
selectedUpdateIds,
|
||||||
|
logs,
|
||||||
|
settings,
|
||||||
|
activeTasks,
|
||||||
|
loading,
|
||||||
|
isInitialized,
|
||||||
|
initStatus,
|
||||||
|
mergedEssentials,
|
||||||
|
sortedUpdates,
|
||||||
|
isBusy,
|
||||||
|
initializeApp: catalog.initializeApp,
|
||||||
|
saveSettings: catalog.saveSettings,
|
||||||
|
syncEssentials: catalog.syncEssentials,
|
||||||
|
fetchEssentials: catalog.fetchEssentials,
|
||||||
|
fetchUpdates,
|
||||||
|
syncDataIfNeeded,
|
||||||
|
fetchAllData,
|
||||||
|
install: taskRuntime.install,
|
||||||
|
togglePostInstallPref: taskRuntime.togglePostInstallPref,
|
||||||
|
startBatch: taskRuntime.startBatch,
|
||||||
|
scheduleDataRefresh: taskRuntime.scheduleDataRefresh,
|
||||||
|
findSoftware: catalog.findSoftware,
|
||||||
|
initListener: taskRuntime.initListener,
|
||||||
|
toggleSelection,
|
||||||
|
selectAll,
|
||||||
|
deselectAll,
|
||||||
|
invertSelection
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
144
src/store/taskRuntime.ts
Normal file
144
src/store/taskRuntime.ts
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { invoke } from '@tauri-apps/api/core'
|
||||||
|
import { listen } from '@tauri-apps/api/event'
|
||||||
|
|
||||||
|
import { useCatalogStore } from './catalog'
|
||||||
|
import type { ActiveTaskState, LogEntry } from './types'
|
||||||
|
|
||||||
|
export const useTaskRuntimeStore = defineStore('task-runtime', {
|
||||||
|
state: () => ({
|
||||||
|
activeTasks: {} as Record<string, ActiveTaskState>,
|
||||||
|
selectedEssentialIds: [] as string[],
|
||||||
|
selectedUpdateIds: [] as string[],
|
||||||
|
logs: [] as LogEntry[],
|
||||||
|
refreshTimer: null as ReturnType<typeof setTimeout> | null,
|
||||||
|
batchQueue: [] as string[],
|
||||||
|
postInstallPrefs: {} as Record<string, boolean>
|
||||||
|
}),
|
||||||
|
getters: {
|
||||||
|
isTaskBusy: (state) => Object.values(state.activeTasks).some(task =>
|
||||||
|
task.status === 'pending' || task.status === 'installing'
|
||||||
|
)
|
||||||
|
},
|
||||||
|
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)
|
||||||
|
},
|
||||||
|
|
||||||
|
setSelection(type: 'essential' | 'update', ids: string[]) {
|
||||||
|
if (type === 'essential') this.selectedEssentialIds = ids
|
||||||
|
else this.selectedUpdateIds = ids
|
||||||
|
},
|
||||||
|
|
||||||
|
togglePostInstallPref(id: string) {
|
||||||
|
const current = this.postInstallPrefs[id] !== false
|
||||||
|
this.postInstallPrefs[id] = !current
|
||||||
|
},
|
||||||
|
|
||||||
|
startBatch(ids: string[]) {
|
||||||
|
this.batchQueue = [...ids]
|
||||||
|
},
|
||||||
|
|
||||||
|
async install(id: string, targetVersion?: string) {
|
||||||
|
const catalog = useCatalogStore()
|
||||||
|
const software = catalog.findSoftware(id)
|
||||||
|
if (!software) return
|
||||||
|
|
||||||
|
const enablePostInstall = this.postInstallPrefs[id] !== false
|
||||||
|
this.activeTasks[id] = { status: 'pending', progress: 0, targetVersion }
|
||||||
|
try {
|
||||||
|
await invoke('install_software', {
|
||||||
|
task: {
|
||||||
|
id,
|
||||||
|
version: targetVersion,
|
||||||
|
use_manifest: software.use_manifest || false,
|
||||||
|
manifest_url: software.manifest_url || null,
|
||||||
|
enable_post_install: enablePostInstall
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Invoke install failed:', err)
|
||||||
|
this.activeTasks[id] = { status: 'error', progress: 0 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
scheduleDataRefresh() {
|
||||||
|
const catalog = useCatalogStore()
|
||||||
|
if (this.refreshTimer) clearTimeout(this.refreshTimer)
|
||||||
|
|
||||||
|
this.refreshTimer = setTimeout(async () => {
|
||||||
|
await catalog.fetchAllData()
|
||||||
|
Object.keys(this.activeTasks).forEach(id => {
|
||||||
|
const status = this.activeTasks[id].status
|
||||||
|
if (status === 'success' || status === 'error') {
|
||||||
|
delete this.activeTasks[id]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.refreshTimer = null
|
||||||
|
}, 2000)
|
||||||
|
},
|
||||||
|
|
||||||
|
initListener() {
|
||||||
|
if ((window as Window & { __tauri_listener_init?: boolean }).__tauri_listener_init) return
|
||||||
|
;(window as Window & { __tauri_listener_init?: boolean }).__tauri_listener_init = true
|
||||||
|
|
||||||
|
listen('install-status', async (event: { payload: { id: string, status: string, progress: number } }) => {
|
||||||
|
const catalog = useCatalogStore()
|
||||||
|
const { id, status, progress } = event.payload
|
||||||
|
const task = this.activeTasks[id]
|
||||||
|
|
||||||
|
this.activeTasks[id] = { status, progress, targetVersion: task?.targetVersion }
|
||||||
|
|
||||||
|
if (status === 'success' || status === 'error') {
|
||||||
|
if (status === 'success') {
|
||||||
|
try {
|
||||||
|
const latestInfo = await invoke('get_software_info', { id }) as Record<string, unknown> | null
|
||||||
|
if (latestInfo) {
|
||||||
|
const index = catalog.allSoftware.findIndex(s => s.id.toLowerCase() === id.toLowerCase())
|
||||||
|
if (index !== -1) {
|
||||||
|
catalog.allSoftware[index] = { ...catalog.allSoftware[index], ...latestInfo }
|
||||||
|
} else {
|
||||||
|
catalog.allSoftware.push(latestInfo as never)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Partial refresh failed:', err)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.selectedEssentialIds = this.selectedEssentialIds.filter(item => item !== id)
|
||||||
|
this.selectedUpdateIds = this.selectedUpdateIds.filter(item => item !== id)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
if (this.activeTasks[id]?.status === 'success') {
|
||||||
|
delete this.activeTasks[id]
|
||||||
|
}
|
||||||
|
}, 3000)
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = this.batchQueue.indexOf(id)
|
||||||
|
if (index !== -1) {
|
||||||
|
this.batchQueue.splice(index, 1)
|
||||||
|
if (this.batchQueue.length === 0) {
|
||||||
|
this.scheduleDataRefresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
listen('log-event', (event: { payload: LogEntry }) => {
|
||||||
|
const payload = event.payload
|
||||||
|
const existingLog = this.logs.find(item => item.id === payload.id)
|
||||||
|
if (existingLog) {
|
||||||
|
if (payload.output) existingLog.output += '\n' + payload.output
|
||||||
|
if (payload.status !== 'info') existingLog.status = payload.status
|
||||||
|
} else {
|
||||||
|
this.logs.unshift(payload)
|
||||||
|
if (this.logs.length > 100) this.logs.pop()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
52
src/store/types.ts
Normal file
52
src/store/types.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
export interface LogEntry {
|
||||||
|
id: string
|
||||||
|
timestamp: string
|
||||||
|
command: string
|
||||||
|
output: string
|
||||||
|
status: 'info' | 'success' | 'error'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SyncEssentialsResult {
|
||||||
|
status: 'updated' | 'cache_used'
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DashboardSnapshot {
|
||||||
|
essentials_version: string
|
||||||
|
essentials: SoftwareListItem[]
|
||||||
|
updates: UpdateCandidate[]
|
||||||
|
installed_software: SoftwareListItem[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SoftwareListItem {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
description?: string
|
||||||
|
version?: string | null
|
||||||
|
recommended_version?: string | null
|
||||||
|
available_version?: string | null
|
||||||
|
icon_url?: string | null
|
||||||
|
use_manifest?: boolean
|
||||||
|
manifest_url?: string | null
|
||||||
|
post_install?: unknown
|
||||||
|
post_install_url?: string | null
|
||||||
|
actionLabel?: string
|
||||||
|
action_label?: string
|
||||||
|
targetVersion?: string | null
|
||||||
|
target_version?: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateCandidate extends SoftwareListItem {
|
||||||
|
action_label: string
|
||||||
|
target_version?: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ActiveTaskState {
|
||||||
|
status: string
|
||||||
|
progress: number
|
||||||
|
targetVersion?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AppSettings {
|
||||||
|
repo_url: string
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user