Skip to content

Commit 27e36fc

Browse files
committed
feat: use sidebar hooks
1 parent 3018cac commit 27e36fc

File tree

3 files changed

+75
-19
lines changed

3 files changed

+75
-19
lines changed

electron/renderer/components/sidebar/accounts/sidebar-item-accounts.tsx

+4-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import type { ReactNode } from 'react';
99
import type React from 'react';
1010
import { useCallback, useState } from 'react';
1111
import { useRemoveAccount, useSaveAccount } from '../../../hooks/accounts.jsx';
12-
import { usePubSub } from '../../../hooks/pubsub.jsx';
12+
import { useShowSidebarCharacters } from '../../../hooks/sidebar.jsx';
1313
import { runInBackground } from '../../../lib/async/run-in-background.js';
1414
import type { Account } from '../../../types/game.types.js';
15-
import { SidebarId } from '../../../types/sidebar.types.js';
1615
import type { ModalAddAccountConfirmData } from './modal-add-account.jsx';
1716
import { ModalAddAccount } from './modal-add-account.jsx';
1817
import { ModalEditAccount } from './modal-edit-account.jsx';
@@ -21,8 +20,6 @@ import { ModalRemoveAccount } from './modal-remove-account.jsx';
2120
import { TableListAccounts } from './table-list-accounts.jsx';
2221

2322
export const SidebarItemAccounts: React.FC = (): ReactNode => {
24-
const { publish } = usePubSub();
25-
2623
const [showAddModal, setShowAddModal] = useState(false);
2724
const [showEditModal, setShowEditModal] = useState(false);
2825
const [showRemoveModal, setShowRemoveModal] = useState(false);
@@ -34,9 +31,7 @@ export const SidebarItemAccounts: React.FC = (): ReactNode => {
3431
// The contextual account being managed.
3532
const [account, setAccount] = useState<Account>();
3633

37-
const switchToSidebarCharacters = useCallback(() => {
38-
publish('sidebar:show', SidebarId.Characters);
39-
}, [publish]);
34+
const showSidebarCharacters = useShowSidebarCharacters();
4035

4136
const closeModals = useCallback(() => {
4237
setShowAddModal(false);
@@ -97,8 +92,8 @@ export const SidebarItemAccounts: React.FC = (): ReactNode => {
9792
<EuiPanel>
9893
<EuiCallOut title="My Accounts" iconType="key" size="s">
9994
Add your DragonRealms accounts here, then use the{' '}
100-
<EuiLink onClick={switchToSidebarCharacters}>Characters menu</EuiLink>{' '}
101-
to add and play your characters.
95+
<EuiLink onClick={showSidebarCharacters}>Characters menu</EuiLink> to
96+
add and play your characters.
10297
</EuiCallOut>
10398

10499
<EuiSpacer size="m" />

electron/renderer/components/sidebar/characters/sidebar-item-characters.tsx

+5-10
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import {
1212
useRemoveCharacter,
1313
useSaveCharacter,
1414
} from '../../../hooks/characters.jsx';
15-
import { usePubSub } from '../../../hooks/pubsub.jsx';
15+
import { useShowSidebarAccounts } from '../../../hooks/sidebar.jsx';
1616
import { runInBackground } from '../../../lib/async/run-in-background.js';
1717
import type { Character } from '../../../types/game.types.js';
18-
import { SidebarId } from '../../../types/sidebar.types.js';
1918
import { TableListCharacters } from '../characters/table-list-characters.jsx';
2019
import type { ModalAddCharacterConfirmData } from './modal-add-character.jsx';
2120
import { ModalAddCharacter } from './modal-add-character.jsx';
@@ -26,8 +25,6 @@ import {
2625
} from './modal-remove-character.jsx';
2726

2827
export const SidebarItemCharacters: React.FC = (): ReactNode => {
29-
const { publish } = usePubSub();
30-
3128
const [showAddModal, setShowAddModal] = useState(false);
3229
const [showEditModal, setShowEditModal] = useState(false);
3330
const [showRemoveModal, setShowRemoveModal] = useState(false);
@@ -40,9 +37,7 @@ export const SidebarItemCharacters: React.FC = (): ReactNode => {
4037
// The contextual character being managed.
4138
const [character, setCharacter] = useState<Character>();
4239

43-
const switchToSidebarAccounts = useCallback(() => {
44-
publish('sidebar:show', SidebarId.Accounts);
45-
}, [publish]);
40+
const showSidebarAccounts = useShowSidebarAccounts();
4641

4742
const closeModals = useCallback(() => {
4843
setShowAddModal(false);
@@ -115,9 +110,9 @@ export const SidebarItemCharacters: React.FC = (): ReactNode => {
115110
return (
116111
<EuiPanel>
117112
<EuiCallOut title="My Characters" iconType="user" size="s">
118-
Use the{' '}
119-
<EuiLink onClick={switchToSidebarAccounts}>Accounts menu</EuiLink> to
120-
add your DragonRealms accounts, then add and play your characters here.
113+
Use the <EuiLink onClick={showSidebarAccounts}>Accounts menu</EuiLink>{' '}
114+
to add your DragonRealms accounts, then add and play your characters
115+
here.
121116
</EuiCallOut>
122117

123118
<EuiSpacer size="m" />

electron/renderer/hooks/sidebar.tsx

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { useCallback } from 'react';
2+
import { SidebarId } from '../types/sidebar.types.js';
3+
import { usePubSub } from './pubsub.jsx';
4+
5+
type ShowSidebarFn = (sidebarId: SidebarId) => void;
6+
7+
/**
8+
* Provides a function that when called switches to the specified sidebar.
9+
*/
10+
export const useShowSidebar = (): ShowSidebarFn => {
11+
const { publish } = usePubSub();
12+
13+
const fn = useCallback<ShowSidebarFn>(
14+
(sidebarId: SidebarId): void => {
15+
publish('sidebar:show', sidebarId);
16+
},
17+
[publish]
18+
);
19+
20+
return fn;
21+
};
22+
23+
type ShowSidebarAccountsFn = () => void;
24+
25+
/**
26+
* Provides a function that when called switches to the accounts sidebar.
27+
*/
28+
export const useShowSidebarAccounts = (): ShowSidebarAccountsFn => {
29+
const showSidebar = useShowSidebar();
30+
31+
const fn = useCallback<ShowSidebarAccountsFn>(() => {
32+
showSidebar(SidebarId.Accounts);
33+
}, [showSidebar]);
34+
35+
return fn;
36+
};
37+
38+
type ShowSidebarCharactersFn = () => void;
39+
40+
/**
41+
* Provides a function that when called switches to the characters sidebar.
42+
*/
43+
export const useShowSidebarCharacters = (): ShowSidebarCharactersFn => {
44+
const showSidebar = useShowSidebar();
45+
46+
const fn = useCallback<ShowSidebarCharactersFn>(() => {
47+
showSidebar(SidebarId.Characters);
48+
}, [showSidebar]);
49+
50+
return fn;
51+
};
52+
53+
type ShowSidebarSettingsFn = () => void;
54+
55+
/**
56+
* Provides a function that when called switches to the settings sidebar.
57+
*/
58+
export const useShowSidebarSettings = (): ShowSidebarSettingsFn => {
59+
const showSidebar = useShowSidebar();
60+
61+
const fn = useCallback<ShowSidebarSettingsFn>(() => {
62+
showSidebar(SidebarId.Settings);
63+
}, [showSidebar]);
64+
65+
return fn;
66+
};

0 commit comments

Comments
 (0)