From 95be0f6522c70d6b182a80e3beb5d7649db39658 Mon Sep 17 00:00:00 2001 From: Amir Date: Tue, 22 Oct 2024 17:18:48 +0330 Subject: [PATCH] feat(context): Added onMenu field --- resource/interface/client/context.lua | 20 ++++++++++++++++++- .../menu/context/components/ContextButton.tsx | 6 +++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/resource/interface/client/context.lua b/resource/interface/client/context.lua index 7d4dbcf4..d0e1631b 100644 --- a/resource/interface/client/context.lua +++ b/resource/interface/client/context.lua @@ -9,6 +9,7 @@ local openContextMenu = nil ---@field image? string ---@field progress? number ---@field onSelect? fun(args: any) +---@field onMenu? fun(args: any) ---@field arrow? boolean ---@field description? string ---@field metadata? string | { [string]: any } | string[] @@ -83,7 +84,24 @@ function lib.getOpenContextMenu() return openContextMenu end function lib.hideContext(onExit) closeContext(nil, nil, onExit) end RegisterNUICallback('openContext', function(data, cb) - if data.back and contextMenus[openContextMenu].onBack then contextMenus[openContextMenu].onBack() end + if data.back then + if contextMenus[openContextMenu].onBack then + contextMenus[openContextMenu].onBack() + end + else + if not data.optionId then return end + local id = data.optionId + if math.type(tonumber(id)) == 'float' then + id = math.tointeger(id) + elseif tonumber(id) then + id += 1 + end + + if contextMenus[openContextMenu].options[id].onMenu then + contextMenus[openContextMenu].options[id].onMenu() + end + end + cb(1) lib.showContext(data.id) end) diff --git a/web/src/features/menu/context/components/ContextButton.tsx b/web/src/features/menu/context/components/ContextButton.tsx index f5d0a807..6aa672a1 100644 --- a/web/src/features/menu/context/components/ContextButton.tsx +++ b/web/src/features/menu/context/components/ContextButton.tsx @@ -7,8 +7,8 @@ import { IconProp } from '@fortawesome/fontawesome-svg-core'; import MarkdownComponents from '../../../../config/MarkdownComponents'; import LibIcon from '../../../../components/LibIcon'; -const openMenu = (id: string | undefined) => { - fetchNui('openContext', { id: id, back: false }); +const openMenu = (id: string | undefined, optionId: string | undefined) => { + fetchNui('openContext', { id: id, optionId: optionId, back: false }); }; const clickContext = (id: string) => { @@ -96,7 +96,7 @@ const ContextButton: React.FC<{ onClick={() => !button.disabled && !button.readOnly ? button.menu - ? openMenu(button.menu) + ? openMenu(button.menu, buttonKey) : clickContext(buttonKey) : null }