Stalwart's CalendarEvent/query ignores Task-type objects and does not support the 'types' filter (returns unsupportedFilter error). This caused tasks both locally created and from external clients like Thunderbird to disappear on reload. Root causes: - CalendarEvent/query only returns @type:'Event' objects on Stalwart, so tasks were invisible to the query endpoint. - CALENDAR_EVENT_PROPERTIES lacked Task-specific fields (due, progress, progressUpdated, priority), causing garbled data when tasks were fetched with Event properties (e.g. utcStart:'32548-12-04T15:30:07Z'). Changes: - Add CALENDAR_TASK_PROPERTIES with Task-specific fields (due, progress, progressUpdated, priority). - Rewrite getCalendarTasks() to first try CalendarEvent/query with types:['Task'] filter, then fall back to CalendarEvent/get ids:null which returns all calendar objects regardless of @type per JMAP spec. - Rewrite createCalendarTask() to fetch back created tasks using CALENDAR_TASK_PROPERTIES instead of piggybacking on createCalendarEvent. - Add comprehensive debug logging throughout the task fetch/create flow (TaskStore, JMAP client) visible when Debug Mode is enabled. - Add 'types' field to CalendarEventFilter interface.
96 lines
3.5 KiB
TypeScript
96 lines
3.5 KiB
TypeScript
import { create } from 'zustand';
|
|
import type { CalendarTask } from '@/lib/jmap/types';
|
|
import type { IJMAPClient } from '@/lib/jmap/client-interface';
|
|
import { debug } from '@/lib/debug';
|
|
|
|
export type TaskViewFilter = 'all' | 'pending' | 'completed' | 'overdue';
|
|
|
|
interface TaskStore {
|
|
tasks: CalendarTask[];
|
|
selectedTaskId: string | null;
|
|
filter: TaskViewFilter;
|
|
showCompleted: boolean;
|
|
isLoading: boolean;
|
|
error: string | null;
|
|
setTasks: (tasks: CalendarTask[]) => void;
|
|
setSelectedTaskId: (id: string | null) => void;
|
|
setFilter: (filter: TaskViewFilter) => void;
|
|
setShowCompleted: (show: boolean) => void;
|
|
fetchTasks: (client: IJMAPClient, calendarIds?: string[]) => Promise<void>;
|
|
createTask: (client: IJMAPClient, task: Partial<CalendarTask>) => Promise<CalendarTask>;
|
|
updateTask: (client: IJMAPClient, id: string, updates: Partial<CalendarTask>) => Promise<void>;
|
|
deleteTask: (client: IJMAPClient, id: string) => Promise<void>;
|
|
toggleTaskComplete: (client: IJMAPClient, task: CalendarTask) => Promise<void>;
|
|
clearTasks: () => void;
|
|
}
|
|
|
|
export const useTaskStore = create<TaskStore>((set, get) => ({
|
|
tasks: [],
|
|
selectedTaskId: null,
|
|
filter: 'all',
|
|
showCompleted: false,
|
|
isLoading: false,
|
|
error: null,
|
|
setTasks: (tasks) => set({ tasks }),
|
|
setSelectedTaskId: (id) => set({ selectedTaskId: id }),
|
|
setFilter: (filter) => set({ filter }),
|
|
setShowCompleted: (show) => set({ showCompleted: show }),
|
|
|
|
fetchTasks: async (client, calendarIds) => {
|
|
debug.log('TaskStore/fetchTasks start', { calendarIds: calendarIds || 'all' });
|
|
set({ isLoading: true, error: null });
|
|
try {
|
|
const tasks = await client.getCalendarTasks(calendarIds);
|
|
debug.log('TaskStore/fetchTasks received', tasks.length, 'tasks');
|
|
tasks.forEach((t, i) => {
|
|
debug.log(`TaskStore/fetchTasks [${i}]`, {
|
|
id: t.id, uid: t.uid, '@type': t['@type'],
|
|
title: t.title, due: t.due, progress: t.progress,
|
|
showWithoutTime: t.showWithoutTime, calendarIds: t.calendarIds,
|
|
});
|
|
});
|
|
set({ tasks, isLoading: false });
|
|
} catch (error) {
|
|
debug.error('TaskStore/fetchTasks failed', error);
|
|
set({ isLoading: false, error: 'Failed to fetch tasks' });
|
|
}
|
|
},
|
|
|
|
createTask: async (client, task) => {
|
|
debug.log('TaskStore/createTask', task);
|
|
const created = await client.createCalendarTask(task);
|
|
debug.log('TaskStore/createTask result', { id: created.id, uid: created.uid, title: created.title });
|
|
set({ tasks: [...get().tasks, created] });
|
|
return created;
|
|
},
|
|
|
|
updateTask: async (client, id, updates) => {
|
|
await client.updateCalendarTask(id, updates);
|
|
set({
|
|
tasks: get().tasks.map(t => t.id === id ? { ...t, ...updates, updated: new Date().toISOString() } : t),
|
|
});
|
|
},
|
|
|
|
deleteTask: async (client, id) => {
|
|
await client.deleteCalendarTask(id);
|
|
set({
|
|
tasks: get().tasks.filter(t => t.id !== id),
|
|
selectedTaskId: get().selectedTaskId === id ? null : get().selectedTaskId,
|
|
});
|
|
},
|
|
|
|
toggleTaskComplete: async (client, task) => {
|
|
const newProgress = task.progress === 'completed' ? 'needs-action' : 'completed';
|
|
const updates: Partial<CalendarTask> = {
|
|
progress: newProgress,
|
|
progressUpdated: new Date().toISOString(),
|
|
};
|
|
await client.updateCalendarTask(task.id, updates);
|
|
set({
|
|
tasks: get().tasks.map(t => t.id === task.id ? { ...t, ...updates, updated: new Date().toISOString() } : t),
|
|
});
|
|
},
|
|
|
|
clearTasks: () => set({ tasks: [], selectedTaskId: null, error: null }),
|
|
}));
|