import { defineStore } from 'pinia'; import api from '../api'; export function formatDuration(totalSeconds) { const s = Math.max(0, Math.floor(totalSeconds || 0)); const h = String(Math.floor(s / 3600)).padStart(2, '0'); const m = String(Math.floor((s % 3600) / 60)).padStart(2, '0'); const sec = String(s % 60).padStart(2, '0'); return `${h}:${m}:${sec}`; } export const useTimeStore = defineStore('time', { state: () => ({ entries: [], open: null, // running timer entry (ended_at == null) workspaceId: null, }), getters: { elapsed: (s) => { if (!s.open) return 0; return (Date.now() - new Date(s.open.started_at).getTime()) / 1000; }, }, actions: { async fetch(workspaceId) { this.workspaceId = workspaceId; const { data } = await api.get(`/time/by-workspace/${workspaceId}`); this.entries = data.entries; this.open = data.open; }, async start(description = '', book_id = null) { const { data } = await api.post('/time/start', { workspace_id: this.workspaceId, description, book_id, }); this.open = data.entry; this.entries.unshift(data.entry); return data.entry; }, async stop() { const { data } = await api.post('/time/stop', { workspace_id: this.workspaceId }); this.open = null; const idx = this.entries.findIndex((e) => e.id === data.entry.id); if (idx >= 0) this.entries[idx] = { ...data.entry, user_name: this.entries[idx].user_name }; else this.entries.unshift(data.entry); return data.entry; }, async remove(id) { await api.delete(`/time/${id}`); this.entries = this.entries.filter((e) => e.id !== id); }, }, });