Frontend

The controller hook: one home for a feature's behaviour

The task detail view was one component of more than 4,000 lines. Splitting it into smaller components kept failing until the logic had a home of its own.

The problem

The task detail view was one component of more than 4,000 lines. It held roughly thirty data hooks and two dozen handlers: update a field, mark complete, add an assignee, attach a file. It worked, and all of it was welded to that one component.

The clue

Pulling the task header into its own component needed about 25 props. My decision record treats that as the clue, not as bad luck.

The header needs 25 props because the thing it depends on is not something it can simply ask for.

Extracting more components would only move the problem around. The logic needed a home of its own. Once it had one, the seams would narrow by themselves.

The decision

One hook per feature. useTaskDetail takes a task id and returns two things: the task with everything attached to it, and a plain function for each thing a person or an automation can do.

What the hook returnsIllustrative
interface TaskController {
  // Data, read straight from the cache
  task: Task | null;
  isLoading: boolean;
  error: Error | null;

  // Actions, one per thing a person can do
  updateField: (changes: Partial<Task>) => Promise<void>;
  addComment: (text: string) => Promise<void>;
  createSubtask: (title: string) => Promise<boolean>;
  reorderSubtasks: (order: string[]) => Promise<void>;
  deleteTask: () => Promise<void>;
}

function useTaskController(taskId: string): TaskController;

The hook reads straight from the data cache and keeps no second copy of server state. I rejected a global store for that reason: the server data already has a cache, and a second one is two sources of truth to keep in sync.

I also rejected many small hooks reassembled in each screen. The parts are reusable that way, but the wiring between them is copied, and two implementations of completing a task drift. And I kept the logic out of a React context, because then anything that wanted the behaviour would have to render the provider. A hook can be called on its own. A provider is an optional convenience on top.

The side sheet and the full-page view both call useTaskDetail. The hook reads data from the React Query cache and exposes action functions. An action patches the cache optimistically, sends the mutation, then swaps in the server row or rolls back. Tables and lists use the sibling useRowActions hook.
Screens call the hook. The hook owns the data and what each action does.

One action, the whole choreography

Each action carries its own optimistic update, cache refresh and error handling, so the caller never has to know the steps. Adding a subtask is a good example.

Add a subtask: show, swap, or roll backIllustrative
const createSubtask = async (title: string) => {
  const placeholder = { id: tempId(), title, pending: true };

  // 1. Show it immediately.
  cache.update(taskId, (task) => withSubtask(task, placeholder));
  try {
    const saved = await api.createSubtask(taskId, { title });
    // 2. Swap the placeholder for the server's row.
    cache.update(taskId, (task) => swapSubtask(task, placeholder.id, saved));
    return true;
  } catch (error) {
    // 3. Remove only this placeholder, keeping any concurrent edits.
    cache.update(taskId, (task) => withoutSubtask(task, placeholder.id));
    report(error);
    return false;
  }
};

The caller gets back true or false. It clears its input box or it does not. Everything else has already happened.

Two screens, one implementation

The side sheet and the full-page task view both moved onto the hook in June 2026. They behave the same because they run the same code, not because someone kept two copies in step.

Rows need a sibling

A table has no single open task. It renders many rows and needs to do something to whichever one was clicked. So I added a sibling hook that owns no data and takes the task as an argument.

It exposes updateTask, setStatus, completeTask and undoComplete. completeTask never rejects: failures are restored and reported inside, so a checkbox can fire and forget. Confirmation dialogs stay with each screen. The screen decides whether to ask, and the hook guarantees what happens once the answer is yes.

This is what a thin caller looks like. The task card on the home page completes a task without knowing anything about caches, toasts or undo.

A thin callerIllustrative
function DueTodayCard() {
  const { dueToday } = useMyTasks();
  const { completeTask, isCompleting } = useRowActions();

  return dueToday.map((task) => (
    <Checkbox
      key={task.id}
      disabled={isCompleting}
      label={`Complete ${task.title}`}
      onClick={() => completeTask(task)}
    />
  ));
}

What changed

It is the same idea as a shared write path or a repository, applied to the front end: give behaviour one named place, and make every surface a thin caller.

The cost I am watching: a controller can grow into its own oversized hook. The decision record says what to do then. Split it into smaller hooks by concern, and keep the controller as the single way in.