|
1 | 1 | import { useEffect } from 'react'; |
2 | 2 |
|
3 | | -import { create } from 'zustand'; |
4 | | - |
5 | | -import { DEFAULT_SETTINGS_STATE, useSettingsStore } from '../stores'; |
6 | | - |
7 | | -import type { KeyboardAcceleratorShortcut } from '../types'; |
| 3 | +import { useSettingsStore, useShortcutRegistrationStore } from '../stores'; |
8 | 4 |
|
9 | 5 | import { applyKeyboardShortcut } from '../utils/system/comms'; |
10 | 6 |
|
11 | | -interface ShortcutRegistrationStore { |
12 | | - /** |
13 | | - * User-facing error when the configured global shortcut could not be |
14 | | - * registered (e.g. already in use by another application). |
15 | | - */ |
16 | | - shortcutRegistrationError: string | null; |
17 | | - |
18 | | - /** |
19 | | - * The last accelerator that registered successfully; used to revert the |
20 | | - * setting when a newly chosen accelerator fails to register. |
21 | | - */ |
22 | | - lastAppliedShortcut: KeyboardAcceleratorShortcut; |
23 | | - |
24 | | - setShortcutRegistrationError: (error: string | null) => void; |
25 | | - setLastAppliedShortcut: (accelerator: KeyboardAcceleratorShortcut) => void; |
26 | | - clearShortcutRegistrationError: () => void; |
27 | | - reset: () => void; |
28 | | -} |
29 | | - |
30 | | -/** |
31 | | - * Transient (non-persisted) state for global keyboard shortcut registration. |
32 | | - */ |
33 | | -export const useShortcutRegistrationStore = create<ShortcutRegistrationStore>()((set) => ({ |
34 | | - shortcutRegistrationError: null, |
35 | | - lastAppliedShortcut: DEFAULT_SETTINGS_STATE.openGitifyShortcut, |
36 | | - |
37 | | - setShortcutRegistrationError: (error) => set({ shortcutRegistrationError: error }), |
38 | | - setLastAppliedShortcut: (accelerator) => set({ lastAppliedShortcut: accelerator }), |
39 | | - clearShortcutRegistrationError: () => set({ shortcutRegistrationError: null }), |
40 | | - reset: () => |
41 | | - set({ |
42 | | - shortcutRegistrationError: null, |
43 | | - lastAppliedShortcut: DEFAULT_SETTINGS_STATE.openGitifyShortcut, |
44 | | - }), |
45 | | -})); |
46 | | - |
47 | 7 | /** |
48 | 8 | * Registers the global open/close shortcut whenever the setting changes, |
49 | 9 | * reverting to the last working accelerator when registration fails. |
|
0 commit comments