|
| 1 | +import $axios from '@/plugins/axios'; |
| 2 | +import store from '@/store'; |
| 3 | +import { requestCache } from '@/utils/requestCache'; |
| 4 | +import { requestDeduplicator } from '@/utils/requestDeduplicator'; |
| 5 | +import { patchUserSettings } from '@/actions/tmgr/user'; |
| 6 | + |
| 7 | +export type PomodoroPhase = 'focus' | 'short' | 'long'; |
| 8 | + |
| 9 | +export interface PomodoroState { |
| 10 | + id: number; |
| 11 | + task_id: number; |
| 12 | + user_id: number; |
| 13 | + phase: PomodoroPhase; |
| 14 | + running: boolean; |
| 15 | + phase_started_at: string | null; |
| 16 | + remaining_ms: number; |
| 17 | + completed_pomodoros: number; |
| 18 | + distractions_count: number; |
| 19 | + updated_at: string; |
| 20 | +} |
| 21 | + |
| 22 | +export interface PomodoroSettings { |
| 23 | + focusMin: number; |
| 24 | + shortMin: number; |
| 25 | + longMin: number; |
| 26 | + longEvery: number; |
| 27 | + autoStart: boolean; |
| 28 | + pauseTrackingOnBreak: boolean; |
| 29 | + notify: 'silent' | 'system' | 'both'; |
| 30 | + sound: string; |
| 31 | + volume: number; |
| 32 | +} |
| 33 | + |
| 34 | +export const DEFAULT_POMODORO_SETTINGS: PomodoroSettings = { |
| 35 | + focusMin: 25, |
| 36 | + shortMin: 5, |
| 37 | + longMin: 15, |
| 38 | + longEvery: 4, |
| 39 | + autoStart: false, |
| 40 | + pauseTrackingOnBreak: false, |
| 41 | + notify: 'system', |
| 42 | + sound: 'bell', |
| 43 | + volume: 60, |
| 44 | +}; |
| 45 | + |
| 46 | +const stateKey = (taskId: number) => `task-pomodoro-${taskId}`; |
| 47 | + |
| 48 | +export const getPomodoroState = async ( |
| 49 | + taskId: number, |
| 50 | +): Promise<PomodoroState | null> => { |
| 51 | + const key = stateKey(taskId); |
| 52 | + const cached = requestCache.get<PomodoroState | null>(key); |
| 53 | + if (cached !== undefined) return cached; |
| 54 | + |
| 55 | + return requestDeduplicator.deduplicate(key, async () => { |
| 56 | + const { |
| 57 | + data: { data }, |
| 58 | + } = await $axios.get(`tasks/${taskId}/pomodoro`); |
| 59 | + requestCache.set(key, data ?? null, 30_000); |
| 60 | + return data ?? null; |
| 61 | + }); |
| 62 | +}; |
| 63 | + |
| 64 | +export const enablePomodoro = async ( |
| 65 | + taskId: number, |
| 66 | +): Promise<PomodoroState> => { |
| 67 | + const { |
| 68 | + data: { data }, |
| 69 | + } = await $axios.post(`tasks/${taskId}/pomodoro`); |
| 70 | + requestCache.set(stateKey(taskId), data, 30_000); |
| 71 | + return data; |
| 72 | +}; |
| 73 | + |
| 74 | +export const disablePomodoro = async (taskId: number): Promise<void> => { |
| 75 | + await $axios.delete(`tasks/${taskId}/pomodoro`); |
| 76 | + requestCache.invalidate(stateKey(taskId)); |
| 77 | +}; |
| 78 | + |
| 79 | +export const updatePomodoroState = async ( |
| 80 | + taskId: number, |
| 81 | + patch: Partial< |
| 82 | + Pick< |
| 83 | + PomodoroState, |
| 84 | + | 'phase' |
| 85 | + | 'running' |
| 86 | + | 'phase_started_at' |
| 87 | + | 'remaining_ms' |
| 88 | + | 'completed_pomodoros' |
| 89 | + | 'distractions_count' |
| 90 | + > |
| 91 | + >, |
| 92 | +): Promise<PomodoroState> => { |
| 93 | + const { |
| 94 | + data: { data }, |
| 95 | + } = await $axios.put(`tasks/${taskId}/pomodoro`, patch); |
| 96 | + requestCache.set(stateKey(taskId), data, 30_000); |
| 97 | + return data; |
| 98 | +}; |
| 99 | + |
| 100 | +export const distractPomodoro = async ( |
| 101 | + taskId: number, |
| 102 | +): Promise<PomodoroState> => { |
| 103 | + const { |
| 104 | + data: { data }, |
| 105 | + } = await $axios.post(`tasks/${taskId}/pomodoro/distract`); |
| 106 | + requestCache.set(stateKey(taskId), data, 30_000); |
| 107 | + return data; |
| 108 | +}; |
| 109 | + |
| 110 | +export const getPomodoroSettings = (): PomodoroSettings => { |
| 111 | + const raw = (store.state as any)?.userSettings?.pomodoro; |
| 112 | + return { ...DEFAULT_POMODORO_SETTINGS, ...(raw ?? {}) }; |
| 113 | +}; |
| 114 | + |
| 115 | +export const savePomodoroSettings = async ( |
| 116 | + settings: PomodoroSettings, |
| 117 | +): Promise<PomodoroSettings> => { |
| 118 | + await patchUserSettings({ pomodoro: settings }); |
| 119 | + const current = (store.state as any)?.userSettings ?? {}; |
| 120 | + store.commit('setUserSettings', { ...current, pomodoro: settings }); |
| 121 | + return settings; |
| 122 | +}; |
0 commit comments