feat: enhance keybinding management by integrating keybindingsEnabled state and updating related hooks

This commit is contained in:
Anwarul Islam
2025-07-18 08:05:02 +06:00
parent cdae10ca0a
commit 8e4661951d
2 changed files with 17 additions and 17 deletions
+5 -17
View File
@@ -1,21 +1,15 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { ActionWithOptionalArgs, invokeAction } from "../constants/actions"; import { invokeAction } from "../constants/actions";
import { useKeybindingsStore } from "@/stores/keybindings-store"; import { useKeybindingsStore } from "@/stores/keybindings-store";
/**
* This variable keeps track whether keybindings are being accepted
* true -> Keybindings are checked
* false -> Key presses are ignored (Keybindings are not checked)
*/
let keybindingsEnabled = true;
/** /**
* A composable that hooks to the caller component's * A composable that hooks to the caller component's
* lifecycle and hooks to the keyboard events to fire * lifecycle and hooks to the keyboard events to fire
* the appropriate actions based on keybindings * the appropriate actions based on keybindings
*/ */
export function useKeybindingsListener() { export function useKeybindingsListener() {
const { keybindings, getKeybindingString } = useKeybindingsStore(); const { keybindings, getKeybindingString, keybindingsEnabled } =
useKeybindingsStore();
useEffect(() => { useEffect(() => {
const handleKeyDown = (ev: KeyboardEvent) => { const handleKeyDown = (ev: KeyboardEvent) => {
@@ -51,20 +45,14 @@ export function useKeybindingsListener() {
return () => { return () => {
document.removeEventListener("keydown", handleKeyDown); document.removeEventListener("keydown", handleKeyDown);
}; };
}, [keybindings, getKeybindingString]); }, [keybindings, getKeybindingString, keybindingsEnabled]);
} }
/** /**
* This composable allows for the UI component to be disabled if the component in question is mounted * This composable allows for the UI component to be disabled if the component in question is mounted
*/ */
export function useKeybindingDisabler() { export function useKeybindingDisabler() {
const disableKeybindings = () => { const { disableKeybindings, enableKeybindings } = useKeybindingsStore();
keybindingsEnabled = false;
};
const enableKeybindings = () => {
keybindingsEnabled = true;
};
return { return {
disableKeybindings, disableKeybindings,
+12
View File
@@ -43,6 +43,7 @@ export interface KeybindingConflict {
interface KeybindingsState { interface KeybindingsState {
keybindings: KeybindingConfig; keybindings: KeybindingConfig;
isCustomized: boolean; isCustomized: boolean;
keybindingsEnabled: boolean;
// Actions // Actions
updateKeybinding: (key: string, action: ActionWithOptionalArgs) => void; updateKeybinding: (key: string, action: ActionWithOptionalArgs) => void;
@@ -50,6 +51,8 @@ interface KeybindingsState {
resetToDefaults: () => void; resetToDefaults: () => void;
importKeybindings: (config: KeybindingConfig) => void; importKeybindings: (config: KeybindingConfig) => void;
exportKeybindings: () => KeybindingConfig; exportKeybindings: () => KeybindingConfig;
enableKeybindings: () => void;
disableKeybindings: () => void;
// Validation // Validation
validateKeybinding: ( validateKeybinding: (
@@ -67,6 +70,7 @@ export const useKeybindingsStore = create<KeybindingsState>()(
(set, get) => ({ (set, get) => ({
keybindings: { ...defaultKeybindings }, keybindings: { ...defaultKeybindings },
isCustomized: false, isCustomized: false,
keybindingsEnabled: true,
updateKeybinding: (key: string, action: ActionWithOptionalArgs) => { updateKeybinding: (key: string, action: ActionWithOptionalArgs) => {
set((state) => { set((state) => {
@@ -99,6 +103,14 @@ export const useKeybindingsStore = create<KeybindingsState>()(
}); });
}, },
enableKeybindings: () => {
set({ keybindingsEnabled: true });
},
disableKeybindings: () => {
set({ keybindingsEnabled: false });
},
importKeybindings: (config: KeybindingConfig) => { importKeybindings: (config: KeybindingConfig) => {
set({ set({
keybindings: { ...config }, keybindings: { ...config },