-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { sortBy } from 'lodash-es'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { runInBackground } from '../lib/async/run-in-background.js'; | ||
import type { Account } from '../types/game.types.js'; | ||
import { usePubSub, useSubscribe } from './pubsub.jsx'; | ||
|
||
/** | ||
* Returns a list of accounts. | ||
* Automatically refreshes the list when an account is saved or removed. | ||
*/ | ||
export const useListAccounts = (): Array<Account> => { | ||
const [accounts, setAccounts] = useState<Array<Account>>([]); | ||
|
||
const loadAccounts = useCallback(async () => { | ||
const allAccounts = await window.api.listAccounts(); | ||
const sortedAccounts = sortBy(allAccounts, 'accountName'); | ||
setAccounts(sortedAccounts); | ||
}, []); | ||
|
||
// Reload when told to. | ||
useSubscribe('accounts:reload', async () => { | ||
await loadAccounts(); | ||
}); | ||
|
||
// Reload on first render. | ||
useEffect(() => { | ||
runInBackground(async () => { | ||
await loadAccounts(); | ||
}); | ||
}, [loadAccounts]); | ||
|
||
return accounts; | ||
}; | ||
|
||
type SaveAccountFn = (options: { | ||
accountName: string; | ||
accountPassword: string; | ||
}) => Promise<void>; | ||
|
||
/** | ||
* Provides a function that when called saves an account. | ||
*/ | ||
export const useSaveAccount = (): SaveAccountFn => { | ||
const { publish } = usePubSub(); | ||
|
||
const fn = useCallback<SaveAccountFn>( | ||
async (options): Promise<void> => { | ||
const { accountName, accountPassword } = options; | ||
await window.api.saveAccount({ accountName, accountPassword }); | ||
publish('account:saved', { accountName }); | ||
publish('accounts:reload'); | ||
}, | ||
[publish] | ||
); | ||
|
||
return fn; | ||
}; | ||
|
||
type RemoveAccountFn = (options: { accountName: string }) => Promise<void>; | ||
|
||
/** | ||
* Provides a function that when called removes an account. | ||
*/ | ||
export const useRemoveAccount = (): RemoveAccountFn => { | ||
const { publish } = usePubSub(); | ||
|
||
const fn = useCallback<RemoveAccountFn>( | ||
async (options): Promise<void> => { | ||
const { accountName } = options; | ||
await window.api.removeAccount({ accountName }); | ||
publish('account:removed', { accountName }); | ||
publish('accounts:reload'); | ||
}, | ||
[publish] | ||
); | ||
|
||
return fn; | ||
}; |
This file was deleted.
Oops, something went wrong.