Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

904 B
Raw Blame
import { useEffect } from "react";

type FocusStore = {
  getState: () => {
    focusedTaskId: string | null;
    focusedTaskRowId?: string | null;
  };
};

export function useRestoreTaskFocus(
  taskId: string,
  store: FocusStore,
  rowId?: string,
) {
  useEffect(() => {
    const state = store.getState();
    if (state.focusedTaskId !== taskId) return;
    if (rowId && state.focusedTaskRowId && state.focusedTaskRowId !== rowId)
      return;
    const active = document.activeElement;
    if (active && active !== document.body) return;
    const element = document.querySelector<HTMLElement>(
      rowId && state.focusedTaskRowId
        ? `[data-task-id="${taskId}"][data-task-row-id="${rowId}"]`
        : `[data-task-id="${taskId}"]`,
    );
    element?.focus({ preventScroll: true });
    element?.scrollIntoView?.({ block: "nearest", inline: "nearest" });
  }, [taskId, store, rowId]);
}