refactor(todo): share the to-do filters and the task form between desktop and phone

The to-do filters, counts, sort order and categories move from the phone
listsModel into todoListModel. useTaskForm holds the name, the fields,
the load and the save of a task for the desktop detail and new task
panes and for MTaskSheet. Each side keeps its own guard and error
wording.
This commit is contained in:
Maurice
2026-10-08 20:52:37 +00:00
parent 5a0e03929d
commit 6dc8699bd1
11 changed files with 791 additions and 307 deletions
+37
View File
@@ -0,0 +1,37 @@
import { useCallback, useState } from 'react';
import type { TodoItem } from '../../types';
import { blankTaskFields, taskFieldsOf, type TaskFieldValues } from './todoListModel';
/**
* The task form behind the desktop detail pane, the desktop new-task pane and the
* phone task sheet: the name and fields being edited, and whether a save is out.
* It starts on `item`, or on a blank task in `category`. Each surface keeps its
* own guard, the payload of a new task and the wording of a failure.
*/
export function useTaskForm(item: TodoItem | null, category?: string | null) {
const [name, setName] = useState(() => item?.name ?? '');
const [fields, setFields] = useState<TaskFieldValues>(() => (item ? taskFieldsOf(item) : blankTaskFields(category)));
const [saving, setSaving] = useState(false);
const patchFields = useCallback((patch: Partial<TaskFieldValues>) => setFields((f) => ({ ...f, ...patch })), []);
/** Puts the form back on a task, or on a blank one in `nextCategory`. */
const load = useCallback((next: TodoItem | null, nextCategory?: string | null) => {
setName(next ? next.name : '');
setFields(next ? taskFieldsOf(next) : blankTaskFields(nextCategory));
}, []);
/** Runs one write with `saving` raised, and hands a failure to `onError`. */
const saveWith = async (write: () => Promise<unknown>, onError: (err: unknown) => void) => {
setSaving(true);
try {
await write();
} catch (err) {
onError(err);
} finally {
setSaving(false);
}
};
return { name, setName, fields, patchFields, saving, load, saveWith };
}