All files / src/lib/stores todos.svelte.ts

96.87% Statements 62/64
88.57% Branches 31/35
100% Functions 28/28
96.42% Lines 54/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124    6x 6x     7x 7x 7x             7x       106x       40x 7x   33x         6x 6x     86x 224x 25x     32x 1x   31x       2x 2x       2x       3x 3x       3x 1x 2x 2x       2x 2x       3x 2x 2x 2x 2x 2x       6x 6x       8x 8x       27x 24x       3x   5x 1x 1x   4x   2x       32x 32x 32x 32x       1x 1x         6x  
import type { Task, TimerMode } from '../types'
 
const STORAGE_KEY = 'pomandoro-tasks'
const ACTIVE_TASK_KEY = 'pomandoro-active-task-id'
 
function loadFromStorage(): Task[] {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    return raw ? (JSON.parse(raw) as Task[]) : []
  } catch {
    return []
  }
}
 
function loadActiveTaskId(): string | null {
  return localStorage.getItem(ACTIVE_TASK_KEY)
}
 
function save(tasks: Task[]) {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(tasks))
}
 
function saveActiveTaskId(id: string | null) {
  if (id) {
    localStorage.setItem(ACTIVE_TASK_KEY, id)
  } else {
    localStorage.removeItem(ACTIVE_TASK_KEY)
  }
}
 
function createTodosStore() {
  let tasks = $state<Task[]>(loadFromStorage())
  let activeTaskId = $state<string | null>(loadActiveTaskId())
 
  return {
    get tasks() { return tasks },
    get activeTaskId() { return activeTaskId },
    get activeTask() { return tasks.find(t => t.id === activeTaskId) ?? null },
 
    addTask(title: string, priority: 'low' | 'medium' | 'high' = 'medium') {
      const trimmed = title.trim()
      if (!trimmed) return
      tasks = [...tasks, { id: crypto.randomUUID(), title: trimmed, pomodoros: 0, done: false, priority }]
      save(tasks)
    },
 
    removeTask(id: string) {
      tasks = tasks.filter(t => t.id !== id)
      if (IactiveTaskId === id) {
        activeTaskId = null
        saveActiveTaskId(null)
      }
      save(tasks)
    },
 
    toggleDone(id: string) {
      tasks = tasks.map(t => t.id === id ? { ...t, done: !t.done } : t)
      save(tasks)
    },
 
    editTask(id: string, newTitle: string) {
      const trimmed = newTitle.trim()
      if (!trimmed) return
      tasks = tasks.map(t => t.id === id ? { ...t, title: trimmed } : t)
      save(tasks)
    },
 
    setPriority(id: string, priority: 'low' | 'medium' | 'high') {
      tasks = tasks.map(t => t.id === id ? { ...t, priority } : t)
      save(tasks)
    },
 
    reorderTasks(oldIndex: number, newIndex: number) {
      if (oldIndex < 0 || oldIndex >= tasks.length || newIndex < 0 || newIndex >= tasks.length) return
      const newTasks = [...tasks]
      const [moved] = newTasks.splice(oldIndex, 1)
      newTasks.splice(newIndex, 0, moved)
      tasks = newTasks
      save(tasks)
    },
 
    incrementPomodoro(id: string) {
      tasks = tasks.map(t => t.id === id ? { ...t, pomodoros: t.pomodoros + 1 } : t)
      save(tasks)
    },
 
    selectTask(id: string | null) {
      activeTaskId = id
      saveActiveTaskId(id)
    },
 
    updateTimerState(id: string, mode: TimerMode, remaining: number) {
      tasks = tasks.map(t => t.id === id ? { ...t, timerMode: mode, timerRemaining: remaining } : t)
      save(tasks)
    },
 
    updateAllTasksProportionally(oldTotal: number, newTotal: number, mode: TimerMode) {
      if (oldTotal <= 0) return
      tasks = tasks.map(t => {
        if (t.timerMode === mode && t.timerRemaining !== undefined) {
          const ratio = t.timerRemaining / oldTotal
          return { ...t, timerRemaining: Math.round(ratio * newTotal) }
        }
        return t
      })
      save(tasks)
    },
 
    clear() {
      tasks = []
      activeTaskId = null
      save(tasks)
      saveActiveTaskId(null)
    },
 
    load() {
      tasks = loadFromStorage()
      activeTaskId = loadActiveTaskId()
    }
  }
}
 
export const todos = createTodosStore()