From fbc9fc119bc5a48ace8dfea2982a83f7c22f49fe Mon Sep 17 00:00:00 2001 From: Pawan Kumar Date: Tue, 25 Feb 2025 20:38:11 +0530 Subject: [PATCH] memoize hotkeys for global search --- .../GlobalSearch/GlobalSearchHotKeys.tsx | 80 ++++++++++--------- .../pages/Editor/JSEditor/JSObjectHotKeys.tsx | 1 + 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/app/client/src/components/editorComponents/GlobalSearch/GlobalSearchHotKeys.tsx b/app/client/src/components/editorComponents/GlobalSearch/GlobalSearchHotKeys.tsx index c037938b431..6d6a4fa901c 100644 --- a/app/client/src/components/editorComponents/GlobalSearch/GlobalSearchHotKeys.tsx +++ b/app/client/src/components/editorComponents/GlobalSearch/GlobalSearchHotKeys.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useMemo } from "react"; import { useHotkeys, type HotkeyConfig } from "@blueprintjs/core"; import type { SearchItem, SelectEvent } from "./utils"; @@ -22,44 +22,48 @@ const GlobalSearchHotKeys: React.FC = ({ handleUpKey, modalOpen, }) => { - const hotkeys: HotkeyConfig[] = [ - { - combo: "up", - onKeyDown: handleUpKey, - hideWhenModalClosed: true, - allowInInput: true, - group: "Omnibar", - label: "Move up the list", - global: false, - }, - { - combo: "down", - onKeyDown: handleDownKey, - hideWhenModalClosed: true, - allowInInput: true, - group: "Omnibar", - label: "Move down the list", - global: false, - }, - { - combo: "return", - onKeyDown: (event: KeyboardEvent) => { - // TODO: Fix this the next time the file is edited - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const activeElement = document.activeElement as any; + const hotkeys: HotkeyConfig[] = useMemo( + () => + [ + { + combo: "up", + onKeyDown: handleUpKey, + hideWhenModalClosed: true, + allowInInput: true, + group: "Omnibar", + label: "Move up the list", + global: false, + }, + { + combo: "down", + onKeyDown: handleDownKey, + hideWhenModalClosed: true, + allowInInput: true, + group: "Omnibar", + label: "Move down the list", + global: false, + }, + { + combo: "return", + onKeyDown: (event: KeyboardEvent) => { + // TODO: Fix this the next time the file is edited + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const activeElement = document.activeElement as any; - activeElement?.blur(); // scroll into view doesn't work with the search input focused - handleItemLinkClick(event, null, "ENTER_KEY"); - }, - hideWhenModalClosed: true, - allowInInput: true, - group: "Omnibar", - label: "Navigate", - global: false, - }, - ].filter( - ({ hideWhenModalClosed }) => - !hideWhenModalClosed || (hideWhenModalClosed && modalOpen), + activeElement?.blur(); // scroll into view doesn't work with the search input focused + handleItemLinkClick(event, null, "ENTER_KEY"); + }, + hideWhenModalClosed: true, + allowInInput: true, + group: "Omnibar", + label: "Navigate", + global: false, + }, + ].filter( + ({ hideWhenModalClosed }) => + !hideWhenModalClosed || (hideWhenModalClosed && modalOpen), + ), + [handleUpKey, handleDownKey, handleItemLinkClick, modalOpen], ); const { handleKeyDown, handleKeyUp } = useHotkeys(hotkeys); diff --git a/app/client/src/pages/Editor/JSEditor/JSObjectHotKeys.tsx b/app/client/src/pages/Editor/JSEditor/JSObjectHotKeys.tsx index c26669cc05d..953412f770a 100644 --- a/app/client/src/pages/Editor/JSEditor/JSObjectHotKeys.tsx +++ b/app/client/src/pages/Editor/JSEditor/JSObjectHotKeys.tsx @@ -15,6 +15,7 @@ function JSObjectHotKeys({ children, runActiveJSFunction }: Props) { global: true, label: "Run Js Function", onKeyDown: runActiveJSFunction, + allowInInput: true, }, ], [runActiveJSFunction],