From 9ce85cdf2131d6507a569fcd288d11ef1dbac579 Mon Sep 17 00:00:00 2001 From: Dakshesh Jain <65905942+dakshesh14@users.noreply.github.com> Date: Thu, 10 Aug 2023 13:12:17 +0530 Subject: [PATCH] refactor: if/else conditions (#1822) * refactor: shortcuts if/else * fix: merge conflict --- .../command-palette/command-pallette.tsx | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/apps/app/components/command-palette/command-pallette.tsx b/apps/app/components/command-palette/command-pallette.tsx index e96acbebc..be54c8f75 100644 --- a/apps/app/components/command-palette/command-pallette.tsx +++ b/apps/app/components/command-palette/command-pallette.tsx @@ -78,38 +78,39 @@ export const CommandPalette: React.FC = () => { const handleKeyDown = useCallback( (e: KeyboardEvent) => { - const singleShortcutKeys = ["p", "v", "d", "h", "q", "m"]; - const { key, ctrlKey, metaKey, altKey, shiftKey } = e; - if (!key) return; - const keyPressed = key.toLowerCase(); + // if on input, textarea or editor, don't do anything if ( - !(e.target instanceof HTMLTextAreaElement) && - !(e.target instanceof HTMLInputElement) && - !(e.target as Element).classList?.contains("remirror-editor") - ) { - if ((ctrlKey || metaKey) && keyPressed === "k") { + e.target instanceof HTMLTextAreaElement || + e.target instanceof HTMLInputElement || + (e.target as Element).classList?.contains("remirror-editor") + ) + return; + + const { key, ctrlKey, metaKey, altKey, shiftKey } = e; + + if (!key) return; + + const keyPressed = key.toLowerCase(); + + const cmdClicked = ctrlKey || metaKey; + + if (cmdClicked) { + if (keyPressed === "k") { e.preventDefault(); setIsPaletteOpen(true); - } else if ((ctrlKey || metaKey) && keyPressed === "c") { - if (altKey) { - e.preventDefault(); - copyIssueUrlToClipboard(); - } - } else if (keyPressed === "c") { + } else if (keyPressed === "c" && altKey) { e.preventDefault(); - setIsIssueModalOpen(true); - } else if ((ctrlKey || metaKey) && keyPressed === "b") { + copyIssueUrlToClipboard(); + } else if (keyPressed === "b") { e.preventDefault(); - // toggleCollapsed(); - store.theme.setSidebarCollapsed(!store?.theme?.sidebarCollapsed); - } else if (key === "Delete") { + toggleCollapsed(); + } else if (keyPressed === "backspace") { e.preventDefault(); setIsBulkDeleteIssuesModalOpen(true); - } else if ( - singleShortcutKeys.includes(keyPressed) && - (ctrlKey || metaKey || altKey || shiftKey) - ) { - e.preventDefault(); + } + } else { + if (keyPressed === "c") { + setIsIssueModalOpen(true); } else if (keyPressed === "p") { setIsProjectModalOpen(true); } else if (keyPressed === "v") { @@ -125,7 +126,7 @@ export const CommandPalette: React.FC = () => { } } }, - [copyIssueUrlToClipboard] + [copyIssueUrlToClipboard, toggleCollapsed] ); useEffect(() => {