59 lines
1.3 KiB
TypeScript
59 lines
1.3 KiB
TypeScript
// filepath: src/stores/logs.ts
|
|
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { listen } from '@tauri-apps/api/event'
|
|
|
|
export interface LogEntry {
|
|
id: string
|
|
taskId: string
|
|
message: string
|
|
level: 'info' | 'error'
|
|
timestamp: number
|
|
}
|
|
|
|
interface LogEvent {
|
|
id: string
|
|
message: string
|
|
level: string
|
|
}
|
|
|
|
export const useLogsStore = defineStore('logs', () => {
|
|
const logs = ref<LogEntry[]>([])
|
|
const isListening = ref(false)
|
|
|
|
function addLog(taskId: string, message: string, level: 'info' | 'error') {
|
|
logs.value.push({
|
|
id: crypto.randomUUID(),
|
|
taskId,
|
|
message,
|
|
level,
|
|
timestamp: Date.now()
|
|
})
|
|
|
|
// Optional: Limit log size to avoid memory issues
|
|
if (logs.value.length > 5000) {
|
|
logs.value = logs.value.slice(-5000)
|
|
}
|
|
}
|
|
|
|
async function initListener() {
|
|
if (isListening.value) return
|
|
isListening.value = true
|
|
|
|
await listen<LogEvent>('download-log', (event) => {
|
|
const { id, message, level } = event.payload
|
|
addLog(id, message, level as 'info' | 'error')
|
|
})
|
|
}
|
|
|
|
function clearLogs() {
|
|
logs.value = []
|
|
}
|
|
|
|
// UI State Persistence
|
|
const autoScroll = ref(true)
|
|
const scrollTop = ref(0)
|
|
|
|
return { logs, addLog, initListener, clearLogs, autoScroll, scrollTop }
|
|
})
|