| 1 | import { create } from 'zustand' |
| 2 | import { persist } |
| 3 | |
| 4 | interface TimerContext { |
| 5 | orgSlug: string |
| 6 | projectId: string |
| 7 | projectName: string |
| 8 | projectSlug: string |
| 9 | } |
| 10 | |
| 11 | interface TimerState { |
| 12 | accumulatedMs: number |
| 13 | context: TimerContext |
| 14 | pause: () |
| 15 | resume: () |
| 16 | start: (context |
| 17 | startedAt: number |
| 18 | stop: () |
| 19 | } |
| 20 | |
| 21 | export const useTimerStore = |
| 22 | persist( |
| 23 | (set, get) |
| 24 | startedAt: null, |
| 25 | accumulatedMs: 0, |
| 26 | context: null, |
| 27 | start: (context) |
| 28 | set({ startedAt: |
| 29 | pause: () => |
| 30 | const { startedAt, |
| 31 | if (startedAt === |
| 32 | return |
| 33 | } |
| 34 | set({ |
| 35 | startedAt: null, |
| 36 | accumulatedMs: accumulatedMs + |
| 37 | }) |
| 38 | }, |
| 39 | resume: () => |
| 40 | if (get().startedAt |
| 41 | return |
| 42 | } |
| 43 | set({ startedAt: |
| 44 | }, |
| 45 | stop: () => |
| 46 | }), |
| 47 | { name: 'saturn-timer' |
| 48 | ) |
| 49 | ) |
| 50 | |
| 51 | export function getElapsedMs( |
| 52 | startedAt: number |
| 53 | accumulatedMs: number, |
| 54 | now: number |
| 55 | ): number { |
| 56 | return accumulatedMs + |
| 57 | } |
| 58 | |
| 59 | export function elapsedMinutesFromMs(ms |
| 60 | return Math.max |
| 61 | } |
| 62 | |
| 63 | export function formatElapsedMs(ms |
| 64 | const totalSeconds = |
| 65 | const h = |
| 66 | const m = |
| 67 | const s = |
| 68 | const pad = |
| 69 | return h > |
| 70 | } |