diff options
Diffstat (limited to 'desktop/src/hooks/useTasks.ts')
| -rw-r--r-- | desktop/src/hooks/useTasks.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/desktop/src/hooks/useTasks.ts b/desktop/src/hooks/useTasks.ts new file mode 100644 index 0000000..3b361e5 --- /dev/null +++ b/desktop/src/hooks/useTasks.ts @@ -0,0 +1,59 @@ +import { useState, useEffect, useCallback } from "react"; +import { commands, type Task } from "../lib/commands"; + +export function useTasks() { + const [tasks, setTasks] = useState<Task[]>([]); + const [activeTaskTitle, setActiveTaskTitle] = useState<string | null>(null); + + const refresh = useCallback(async () => { + const snap = await commands.getState(); + setTasks(snap.tasks); + setActiveTaskTitle(snap.active_task_title); + }, []); + + useEffect(() => { + refresh(); + }, [refresh]); + + const addTask = useCallback(async (title: string) => { + await commands.addTask(title); + await refresh(); + }, [refresh]); + + const editTask = useCallback(async (index: number, title: string) => { + await commands.editTask(index, title); + await refresh(); + }, [refresh]); + + const deleteTask = useCallback(async (index: number) => { + await commands.deleteTask(index); + await refresh(); + }, [refresh]); + + const toggleTask = useCallback(async (index: number) => { + await commands.toggleTask(index); + await refresh(); + }, [refresh]); + + const reorderTask = useCallback(async (index: number, direction: "up" | "down") => { + await commands.reorderTask(index, direction); + await refresh(); + }, [refresh]); + + const moveTask = useCallback(async (from: number, to: number) => { + await commands.moveTask(from, to); + await refresh(); + }, [refresh]); + + return { + tasks, + activeTaskTitle, + addTask, + editTask, + deleteTask, + toggleTask, + reorderTask, + moveTask, + refresh, + }; +} |