2023-04-27 05:02:32 +08:00
|
|
|
import getFromUserSettings from '/imports/ui/services/users-settings';
|
|
|
|
import { Meteor } from 'meteor/meteor'
|
2023-05-09 03:54:06 +08:00
|
|
|
import { useEffect, useState } from 'react';
|
2023-04-27 05:02:32 +08:00
|
|
|
|
|
|
|
interface ShortcutObject {
|
|
|
|
accesskey: string,
|
|
|
|
descId: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Accumulator {
|
|
|
|
[key: string]: string,
|
|
|
|
}
|
|
|
|
|
2023-05-09 03:54:06 +08:00
|
|
|
type UseShortcutHelp = Object | string
|
|
|
|
|
2023-04-27 05:02:32 +08:00
|
|
|
const BASE_SHORTCUTS: Array<ShortcutObject> = Meteor.settings.public.app.shortcuts;
|
|
|
|
|
2023-05-09 03:54:06 +08:00
|
|
|
function useShortcutHelp(param: string): UseShortcutHelp {
|
|
|
|
const [shortcut, setShortcut] = useState<UseShortcutHelp>("");
|
2023-05-05 04:42:03 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-04-27 05:02:32 +08:00
|
|
|
const ENABLED_SHORTCUTS = getFromUserSettings('bbb_shortcuts', null);
|
2023-05-09 03:54:06 +08:00
|
|
|
let shortcuts: ShortcutObject[] = Object.values(BASE_SHORTCUTS).map(
|
|
|
|
(el: ShortcutObject) => {
|
|
|
|
return {
|
|
|
|
...el,
|
|
|
|
descId: el.descId.toLowerCase(),
|
|
|
|
}
|
|
|
|
});
|
2023-05-05 04:42:03 +08:00
|
|
|
|
|
|
|
if (ENABLED_SHORTCUTS) {
|
2023-05-09 03:54:06 +08:00
|
|
|
shortcuts = Object.values(BASE_SHORTCUTS).filter((el: ShortcutObject) =>
|
|
|
|
ENABLED_SHORTCUTS.includes(el.descId));
|
2023-05-05 04:42:03 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 03:54:06 +08:00
|
|
|
let shortcutsString: Object = "";
|
|
|
|
if (!Array.isArray(param)) {
|
|
|
|
shortcutsString = shortcuts
|
|
|
|
.filter(el => {
|
|
|
|
return el.descId === param.toLowerCase()})
|
|
|
|
.map(el => {
|
|
|
|
return el.accesskey})
|
|
|
|
.pop() || "";
|
|
|
|
} else {
|
|
|
|
shortcutsString = shortcuts
|
|
|
|
.filter(el => param.map(p => p.toLowerCase()).includes(el.descId.toLowerCase()))
|
|
|
|
.reduce((acc: Accumulator, current: ShortcutObject) => {
|
|
|
|
acc[current.descId.toLowerCase()] = current.accesskey;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2023-04-27 05:02:32 +08:00
|
|
|
}
|
2023-05-09 03:54:06 +08:00
|
|
|
setShortcut(shortcutsString);
|
2023-05-05 04:42:03 +08:00
|
|
|
}, [])
|
2023-05-09 03:54:06 +08:00
|
|
|
return shortcut;
|
2023-04-27 05:02:32 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 03:54:06 +08:00
|
|
|
export { useShortcutHelp, UseShortcutHelp };
|