Skip to content

Commit 5b659cc

Browse files
committed
fix: The command callback is not refreshed
1 parent accfedf commit 5b659cc

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/context/KeyBindContext.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
KeyBindProviderPropsI,
1212
ShortcutType,
1313
} from '../types';
14-
import { findFirstPlatformMatch, isDuplicate } from '../utils';
14+
import { findFirstPlatformMatch, isDuplicate, logMsg } from '../utils';
1515
import { useShortcuts } from '../hooks';
1616

1717
export const KeyBindContext = createContext({} as KeyBindContextState);
@@ -23,12 +23,19 @@ const KeyBindProvider: FC<KeyBindProviderPropsI> = ({
2323
const [storeShortcuts, setStoreCommands] = useState(shortcuts);
2424

2525
const registerShortcut = useCallback(
26-
(command: ShortcutType) => {
27-
if (isDuplicate(storeShortcuts, command)) {
28-
console.warn('Command already registered', { storeShortcuts, command });
29-
return;
30-
}
31-
setStoreCommands(prev => [...prev, command]);
26+
(shortcut: ShortcutType) => {
27+
setStoreCommands(prev => {
28+
return [
29+
...(prev?.filter(s => {
30+
const isDuplicated = isDuplicate(prev, s);
31+
if (isDuplicated) {
32+
console.warn(logMsg(s, shortcut));
33+
}
34+
return !isDuplicated;
35+
}) ?? []),
36+
shortcut,
37+
];
38+
});
3239
},
3340
[storeShortcuts]
3441
);

src/hooks/useRegisterShortcut.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export const useRegisterShortcut = (shortcut: ShortcutType) => {
77

88
useEffect(() => {
99
registerShortcut(shortcut);
10-
}, []);
10+
}, [shortcut]);
1111
};

src/utils/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,10 @@ export const isDuplicate = (
7979
) => {
8080
return !!findShortcut(shortcuts, getShortcutKeys(shortcut));
8181
};
82+
83+
export const logMsg = (short: ShortcutType, short2: ShortcutType) =>
84+
`Shortcut is duplicated: label: "${short.label}", keys: "${JSON.stringify(
85+
short.keys
86+
)}" ,replacing for: label: "${short2.label}", keys: "${JSON.stringify(
87+
short2.keys
88+
)}"...`;

0 commit comments

Comments
 (0)