mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +03:00
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.
37 lines
1.2 KiB
TypeScript
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]);
|
|
}
|