Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/renderer/__helpers__/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { type InitialEntry, MemoryRouter } from 'react-router-dom';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

import { useShortcutRegistrationStore } from '../hooks/useShortcutRegistration';
import { type FiltersStore, useAccountsStore, useFiltersStore, useSettingsStore } from '../stores';
import {
type FiltersStore,
useAccountsStore,
useFiltersStore,
useSettingsStore,
useShortcutRegistrationStore,
} from '../stores';

import type { Account, SettingsState } from '../types';

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/__helpers__/visual.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ beforeEach(async () => {
const { resetHookMocks } = await import('./hook-mocks');
resetHookMocks();

const { useShortcutRegistrationStore } = await import('../hooks/useShortcutRegistration');
const { useShortcutRegistrationStore } = await import('../stores');
useShortcutRegistrationStore.getState().reset();
});

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/__helpers__/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ beforeEach(async () => {
const { resetHookMocks } = await import('./hook-mocks');
resetHookMocks();

const { useShortcutRegistrationStore } = await import('../hooks/useShortcutRegistration');
const { useShortcutRegistrationStore } = await import('../stores');
useShortcutRegistrationStore.getState().reset();
});

Expand Down
3 changes: 1 addition & 2 deletions src/renderer/components/GlobalEffects.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { act } from '@testing-library/react';
import { renderWithProviders } from '../__helpers__/test-utils';
import { mockGitHubCloudAccount } from '../__mocks__/account-mocks';

import { useShortcutRegistrationStore } from '../hooks/useShortcutRegistration';
import { useAccountsStore, useSettingsStore } from '../stores';
import { useAccountsStore, useSettingsStore, useShortcutRegistrationStore } from '../stores';

import { Theme } from '../types';

Expand Down
8 changes: 5 additions & 3 deletions src/renderer/components/GlobalEffects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { useNotificationsHost } from '../hooks/notifications/useNotificationsHos
import { useAccounts } from '../hooks/useAccounts';
import { useAppearance } from '../hooks/useAppearance';
import { useOnlineStatus } from '../hooks/useOnlineStatus';
import { useShortcutRegistration } from '../hooks/useShortcutRegistration';
import {
useShortcutRegistration,
useAccountsStore,
useFiltersStore,
useSettingsStore,
useShortcutRegistrationStore,
} from '../hooks/useShortcutRegistration';
import { useAccountsStore, useFiltersStore, useSettingsStore } from '../stores';
} from '../stores';

import { setTrayIconColorAndTitle } from '../utils/system/tray';

Expand Down
7 changes: 5 additions & 2 deletions src/renderer/components/settings/SystemSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { Banner, Button, ButtonGroup, IconButton, Stack, Text } from '@primer/re

import { APPLICATION } from '../../../shared/constants';

import { useShortcutRegistrationStore } from '../../hooks/useShortcutRegistration';
import { DEFAULT_SETTINGS_STATE, useSettingsStore } from '../../stores';
import {
DEFAULT_SETTINGS_STATE,
useSettingsStore,
useShortcutRegistrationStore,
} from '../../stores';

import { Checkbox } from '../fields/Checkbox';
import { RadioGroup } from '../fields/RadioGroup';
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/hooks/useShortcutRegistration.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { renderHook, waitFor } from '@testing-library/react';

import { DEFAULT_SETTINGS_STATE, useSettingsStore } from '../stores';
import { DEFAULT_SETTINGS_STATE, useSettingsStore, useShortcutRegistrationStore } from '../stores';

import type { KeyboardAcceleratorShortcut } from '../types';

import { useShortcutRegistration, useShortcutRegistrationStore } from './useShortcutRegistration';
import { useShortcutRegistration } from './useShortcutRegistration';

describe('renderer/hooks/useShortcutRegistration.ts', () => {
it('registers the configured shortcut and records it as last applied', async () => {
Expand Down
42 changes: 1 addition & 41 deletions src/renderer/hooks/useShortcutRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
import { useEffect } from 'react';

import { create } from 'zustand';

import { DEFAULT_SETTINGS_STATE, useSettingsStore } from '../stores';

import type { KeyboardAcceleratorShortcut } from '../types';
import { useSettingsStore, useShortcutRegistrationStore } from '../stores';

import { applyKeyboardShortcut } from '../utils/system/comms';

interface ShortcutRegistrationStore {
/**
* User-facing error when the configured global shortcut could not be
* registered (e.g. already in use by another application).
*/
shortcutRegistrationError: string | null;

/**
* The last accelerator that registered successfully; used to revert the
* setting when a newly chosen accelerator fails to register.
*/
lastAppliedShortcut: KeyboardAcceleratorShortcut;

setShortcutRegistrationError: (error: string | null) => void;
setLastAppliedShortcut: (accelerator: KeyboardAcceleratorShortcut) => void;
clearShortcutRegistrationError: () => void;
reset: () => void;
}

/**
* Transient (non-persisted) state for global keyboard shortcut registration.
*/
export const useShortcutRegistrationStore = create<ShortcutRegistrationStore>()((set) => ({
shortcutRegistrationError: null,
lastAppliedShortcut: DEFAULT_SETTINGS_STATE.openGitifyShortcut,

setShortcutRegistrationError: (error) => set({ shortcutRegistrationError: error }),
setLastAppliedShortcut: (accelerator) => set({ lastAppliedShortcut: accelerator }),
clearShortcutRegistrationError: () => set({ shortcutRegistrationError: null }),
reset: () =>
set({
shortcutRegistrationError: null,
lastAppliedShortcut: DEFAULT_SETTINGS_STATE.openGitifyShortcut,
}),
}));

/**
* Registers the global open/close shortcut whenever the setting changes,
* reverting to the last working accelerator when registration fails.
Expand Down
1 change: 1 addition & 0 deletions src/renderer/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export {
getNotificationFailureKey,
} from './useNotificationActionFailuresStore';
export { default as useSettingsStore } from './useSettingsStore';
export { default as useShortcutRegistrationStore } from './useShortcutRegistrationStore';
27 changes: 27 additions & 0 deletions src/renderer/stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Forge,
GitifyError,
Hostname,
KeyboardAcceleratorShortcut,
ReasonFilterValue,
ReviewRequestTypeFilterValue,
SearchToken,
Expand Down Expand Up @@ -273,3 +274,29 @@ export interface NotificationActionFailuresActions {
*/
export type NotificationActionFailuresStore = NotificationActionFailuresState &
NotificationActionFailuresActions;

// ============================================================================
// Shortcut Registration Store Types
// ============================================================================

/**
* Transient (non-persisted) state for global keyboard shortcut registration.
*/
export interface ShortcutRegistrationStore {
/**
* User-facing error when the configured global shortcut could not be
* registered (e.g. already in use by another application).
*/
shortcutRegistrationError: string | null;

/**
* The last accelerator that registered successfully; used to revert the
* setting when a newly chosen accelerator fails to register.
*/
lastAppliedShortcut: KeyboardAcceleratorShortcut;

setShortcutRegistrationError: (error: string | null) => void;
setLastAppliedShortcut: (accelerator: KeyboardAcceleratorShortcut) => void;
clearShortcutRegistrationError: () => void;
reset: () => void;
}
27 changes: 27 additions & 0 deletions src/renderer/stores/useShortcutRegistrationStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { create } from 'zustand';

import type { ShortcutRegistrationStore } from './types';

import { DEFAULT_SETTINGS_STATE } from './defaults';

/**
* Transient (non-persisted) state for global keyboard shortcut registration.
*
* Lives in the stores layer alongside the other client state; the
* `useShortcutRegistration` effect hook reads it from the stores barrel.
*/
const useShortcutRegistrationStore = create<ShortcutRegistrationStore>()((set) => ({
shortcutRegistrationError: null,
lastAppliedShortcut: DEFAULT_SETTINGS_STATE.openGitifyShortcut,

setShortcutRegistrationError: (error) => set({ shortcutRegistrationError: error }),
setLastAppliedShortcut: (accelerator) => set({ lastAppliedShortcut: accelerator }),
clearShortcutRegistrationError: () => set({ shortcutRegistrationError: null }),
reset: () =>
set({
shortcutRegistrationError: null,
lastAppliedShortcut: DEFAULT_SETTINGS_STATE.openGitifyShortcut,
}),
}));

export default useShortcutRegistrationStore;
Loading