Files
outline/app/hooks/useCommandBarActions.ts
T
Tom Moor 935088123a fix: Prevent update depth errors when many linked documents load (#14701)
Stabilize the editor refs so they are not detached and attached on every
render, and defer command bar action registration so bursts of store
updates do not each re-register actions.
2026-11-25 07:38:52 -05:00

37 lines
1.2 KiB
TypeScript

import { useRegisterActions } from "kbar";
import { flattenDeep } from "es-toolkit/compat";
import { useDeferredValue } from "react";
import { actionToKBar } from "~/actions";
import type { ActionVariant } from "~/types";
import useActionContext from "./useActionContext";
import { useLocationSelector } from "./useLocationSelector";
import useWebMCPActions from "./useWebMCPActions";
/**
* Hook to add actions to the command bar while the hook is inside a mounted
* component. The same actions are also exposed as WebMCP tools in supported
* browsers.
*
* @param actions actions to make available
*/
export default function useCommandBarActions(
actions: ActionVariant[],
additionalDeps: React.DependencyList = []
) {
const pathname = useLocationSelector((location) => location.pathname);
const context = useActionContext({
isCommandBar: true,
});
useWebMCPActions(actions, additionalDeps);
const registerable = flattenDeep(
actions.map((action) => actionToKBar(action, context))
);
// Deferred so that a burst of store updates does not re-register on every commit.
const ids = useDeferredValue(registerable.map((r) => r.id).join(""));
useRegisterActions(registerable, [ids, pathname, ...additionalDeps]);
}