From 96f640da674e89824881070c4081b7aee212e619 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 14 Apr 2023 21:47:03 -0300 Subject: Make Context Menu API support hooks (#902) Co-authored-by: V --- src/api/ContextMenu.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/api') diff --git a/src/api/ContextMenu.ts b/src/api/ContextMenu.ts index 4e52131..4d1d577 100644 --- a/src/api/ContextMenu.ts +++ b/src/api/ContextMenu.ts @@ -19,17 +19,20 @@ import Logger from "@utils/Logger"; import type { ReactElement } from "react"; +type ContextMenuPatchCallbackReturn = (() => void) | void; /** * @param children The rendered context menu elements * @param args Any arguments passed into making the context menu, like the guild, channel, user or message for example + * @returns A callback which is only ran once used to modify the context menu elements (Use to avoid duplicates) */ -export type NavContextMenuPatchCallback = (children: Array, ...args: Array) => void; +export type NavContextMenuPatchCallback = (children: Array, ...args: Array) => ContextMenuPatchCallbackReturn; /** - * @param The navId of the context menu being patched + * @param navId The navId of the context menu being patched * @param children The rendered context menu elements * @param args Any arguments passed into making the context menu, like the guild, channel, user or message for example + * @returns A callback which is only ran once used to modify the context menu elements (Use to avoid duplicates) */ -export type GlobalContextMenuPatchCallback = (navId: string, children: Array, ...args: Array) => void; +export type GlobalContextMenuPatchCallback = (navId: string, children: Array, ...args: Array) => ContextMenuPatchCallbackReturn; const ContextMenuLogger = new Logger("ContextMenu"); @@ -78,6 +81,7 @@ export function removeContextMenuPatch>(navId: /** * Remove a global context menu patch + * @param patch The patch to be removed * @returns Wheter the patch was sucessfully removed */ export function removeGlobalContextMenuPatch(patch: GlobalContextMenuPatchCallback): boolean { @@ -87,12 +91,13 @@ export function removeGlobalContextMenuPatch(patch: GlobalContextMenuPatchCallba /** * A helper function for finding the children array of a group nested inside a context menu based on the id of one of its childs * @param id The id of the child + * @param children The context menu children */ -export function findGroupChildrenByChildId(id: string, children: Array, itemsArray?: Array): Array | null { +export function findGroupChildrenByChildId(id: string, children: Array, _itemsArray?: Array): Array | null { for (const child of children) { if (child == null) continue; - if (child.props?.id === id) return itemsArray ?? null; + if (child.props?.id === id) return _itemsArray ?? null; let nextChildren = child.props?.children; if (nextChildren) { @@ -121,9 +126,6 @@ interface ContextMenuProps { const patchedMenus = new WeakSet(); export function _patchContextMenu(props: ContextMenuProps) { - if (patchedMenus.has(props)) return; - patchedMenus.add(props); - props.contextMenuApiArguments ??= []; const contextMenuPatches = navPatches.get(props.navId); @@ -132,7 +134,8 @@ export function _patchContextMenu(props: ContextMenuProps) { if (contextMenuPatches) { for (const patch of contextMenuPatches) { try { - patch(props.children, ...props.contextMenuApiArguments); + const callback = patch(props.children, ...props.contextMenuApiArguments); + if (!patchedMenus.has(props)) callback?.(); } catch (err) { ContextMenuLogger.error(`Patch for ${props.navId} errored,`, err); } @@ -141,9 +144,12 @@ export function _patchContextMenu(props: ContextMenuProps) { for (const patch of globalPatches) { try { - patch(props.navId, props.children, ...props.contextMenuApiArguments); + const callback = patch(props.navId, props.children, ...props.contextMenuApiArguments); + if (!patchedMenus.has(props)) callback?.(); } catch (err) { ContextMenuLogger.error("Global patch errored,", err); } } + + patchedMenus.add(props); } -- cgit