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
|
import { create } from 'zustand';
import { invoke } from '@tauri-apps/api/core';
import { useTimerStore } from './timerStore';
export interface Task {
id: string;
name: string;
total_sessions: number;
remaining_sessions: number;
completed: boolean;
created_at: string;
}
interface TaskStore {
tasks: Task[];
loading: boolean;
fetchTasks: () => Promise<void>;
addTask: (name: string, totalSessions: number) => Promise<void>;
updateTask: (id: string, remainingSessions?: number, completed?: boolean) => Promise<void>;
deleteTask: (id: string) => Promise<void>;
setCurrentTask: (id: string | null) => Promise<void>;
}
export const useTaskStore = create<TaskStore>((set) => ({
tasks: [],
loading: false,
fetchTasks: async () => {
set({ loading: true });
try {
const tasks = await invoke<Task[]>('get_tasks');
set({ tasks, loading: false });
} catch (e) {
console.error('fetchTasks error:', e);
set({ loading: false });
}
},
addTask: async (name, totalSessions) => {
try {
const task = await invoke<Task>('add_task', {
name,
totalSessions,
});
set((state) => ({ tasks: [...state.tasks, task] }));
} catch (e) {
console.error('addTask error:', e);
throw e;
}
},
updateTask: async (id, remainingSessions, completed) => {
await invoke('update_task', {
id,
remainingSessions: remainingSessions ?? null,
completed: completed ?? null,
});
set((state) => ({
tasks: state.tasks.map((t) =>
t.id === id
? {
...t,
remaining_sessions: remainingSessions ?? t.remaining_sessions,
completed: completed ?? t.completed,
}
: t,
),
}));
},
deleteTask: async (id) => {
try {
await invoke('delete_task', { id });
set((state) => ({ tasks: state.tasks.filter((t) => t.id !== id) }));
} catch (e) {
console.error('deleteTask error:', e);
throw e;
}
},
setCurrentTask: async (id) => {
await invoke('set_current_task', { taskId: id });
useTimerStore.getState().setCurrentTaskId(id);
try {
await useTimerStore.getState().syncFromBackend();
} catch (e) {
console.error('syncFromBackend after set_current_task error:', e);
}
},
}));
|