Skip to content

Commit 6797dc3

Browse files
committed
feat: character service hooks
1 parent b82bd29 commit 6797dc3

File tree

2 files changed

+99
-40
lines changed

2 files changed

+99
-40
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { sortBy } from 'lodash-es';
2+
import { useCallback, useEffect, useState } from 'react';
3+
import { runInBackground } from '../lib/async/run-in-background.js';
4+
import type { Character } from '../types/game.types.js';
5+
import { usePubSub, useSubscribe } from './pubsub.jsx';
6+
7+
/**
8+
* Returns a list of characters.
9+
* Automatically refreshes the list when an character is saved or removed.
10+
*/
11+
export const useListCharacters = (): Array<Character> => {
12+
const [characters, setCharacters] = useState<Array<Character>>([]);
13+
14+
const loadCharacters = useCallback(async () => {
15+
const allCharacters = await window.api.listCharacters();
16+
const sortedCharacters = sortBy(allCharacters, 'characterName');
17+
setCharacters(sortedCharacters);
18+
}, []);
19+
20+
// Reload when told to.
21+
useSubscribe('characters:reload', async () => {
22+
await loadCharacters();
23+
});
24+
25+
// Reload on first render.
26+
useEffect(() => {
27+
runInBackground(async () => {
28+
await loadCharacters();
29+
});
30+
}, [loadCharacters]);
31+
32+
return characters;
33+
};
34+
35+
type SaveCharacterFn = (options: {
36+
accountName: string;
37+
characterName: string;
38+
gameCode: string;
39+
}) => Promise<void>;
40+
41+
/**
42+
* Provides a function that when called saves an character.
43+
*/
44+
export const useSaveCharacter = (): SaveCharacterFn => {
45+
const { publish } = usePubSub();
46+
47+
const fn = useCallback<SaveCharacterFn>(
48+
async (options): Promise<void> => {
49+
const { accountName, characterName, gameCode } = options;
50+
await window.api.saveCharacter({
51+
accountName,
52+
characterName,
53+
gameCode,
54+
});
55+
publish('character:saved', {
56+
accountName,
57+
characterName,
58+
gameCode,
59+
});
60+
publish('characters:reload');
61+
},
62+
[publish]
63+
);
64+
65+
return fn;
66+
};
67+
68+
type RemoveCharacterFn = (options: {
69+
accountName: string;
70+
characterName: string;
71+
gameCode: string;
72+
}) => Promise<void>;
73+
74+
/**
75+
* Provides a function that when called removes an character.
76+
*/
77+
export const useRemoveCharacter = (): RemoveCharacterFn => {
78+
const { publish } = usePubSub();
79+
80+
const fn = useCallback<RemoveCharacterFn>(
81+
async (options): Promise<void> => {
82+
const { accountName, characterName, gameCode } = options;
83+
await window.api.removeCharacter({
84+
accountName,
85+
characterName,
86+
gameCode,
87+
});
88+
publish('character:removed', {
89+
accountName,
90+
characterName,
91+
gameCode,
92+
});
93+
publish('characters:reload');
94+
},
95+
[publish]
96+
);
97+
98+
return fn;
99+
};

electron/renderer/hooks/list-characters.tsx

-40
This file was deleted.

0 commit comments

Comments
 (0)