/** * Jotai atoms for task state management. * Manages task data, loading states, filters, and errors. */ import { atom } from 'jotai'; /** * Task status enum */ export type TaskStatus = 'pending' | 'in_progress' | 'completed'; /** * Task interface matching backend API response */ export interface Task { id: string; project_id: string; name: string; data: Record; status: TaskStatus; assigned_to: string | null; created_at: string; progress: number; } /** * Task filter interface */ export interface TaskFilter { status: TaskStatus | null; projectId: string | null; assignedTo: string | null; } /** * Atom storing the list of all tasks */ export const tasksAtom = atom([]); /** * Atom storing the currently selected task */ export const currentTaskAtom = atom(null); /** * Atom tracking task loading state */ export const taskLoadingAtom = atom(false); /** * Atom storing task-related errors */ export const taskErrorAtom = atom(null); /** * Atom storing task filter criteria */ export const taskFilterAtom = atom({ status: null, projectId: null, assignedTo: null, }); /** * Derived atom to get filtered tasks */ export const filteredTasksAtom = atom((get) => { const tasks = get(tasksAtom); const filter = get(taskFilterAtom); return tasks.filter((task) => { if (filter.status && task.status !== filter.status) { return false; } if (filter.projectId && task.project_id !== filter.projectId) { return false; } if (filter.assignedTo && task.assigned_to !== filter.assignedTo) { return false; } return true; }); }); /** * Derived atom to get task by ID */ export const getTaskByIdAtom = atom( (get) => (taskId: string) => { const tasks = get(tasksAtom); return tasks.find((t) => t.id === taskId) || null; } ); /** * Derived atom to get tasks by project ID */ export const getTasksByProjectIdAtom = atom( (get) => (projectId: string) => { const tasks = get(tasksAtom); return tasks.filter((t) => t.project_id === projectId); } ); /** * Derived atom to check if tasks are empty */ export const hasTasksAtom = atom((get) => { const tasks = get(tasksAtom); return tasks.length > 0; });