Skip to content

Commit 40113f2

Browse files
authored
refactor(db-react): remove db prefix from hooks and helpers (#538)
1 parent c923233 commit 40113f2

File tree

98 files changed

+383
-383
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+383
-383
lines changed

packages/db-react/src/db-loader.tsx

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/db-react/src/db-get-or-fetch.tsx renamed to packages/db-react/src/get-or-fetch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DataTag, DefaultError, QueryClient, QueryKey, UndefinedInitialDataOptions } from '@tanstack/react-query'
22

3-
export async function dbGetOrFetchQuery<
3+
export async function getOrFetchQuery<
44
TQueryFnData = unknown,
55
TError = DefaultError,
66
TData = TQueryFnData,

packages/db-react/src/db-account-options.tsx renamed to packages/db-react/src/options-account.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ import { accountSetActive } from '@workspace/db/account/account-set-active'
1010
import { accountUpdate } from '@workspace/db/account/account-update'
1111
import type { AccountUpdateInput } from '@workspace/db/account/account-update-input'
1212
import { db } from '@workspace/db/db'
13-
import { queryClient } from './db-query-client.tsx'
14-
import { dbSettingOptions } from './db-setting-options.tsx'
13+
import { optionsSetting } from './options-setting.tsx'
14+
import { queryClient } from './query-client.tsx'
1515

16-
export type DbAccountCreateMutateOptions = MutateOptions<string, Error, { input: AccountCreateInput }>
17-
export type DbAccountDeleteMutateOptions = MutateOptions<void, Error, { id: string }>
18-
export type DbAccountSetActiveMutateOptions = MutateOptions<void, Error, { id: string }>
19-
export type DbAccountUpdateMutateOptions = MutateOptions<number, Error, { input: AccountUpdateInput }>
16+
export type AccountCreateMutateOptions = MutateOptions<string, Error, { input: AccountCreateInput }>
17+
export type AccountDeleteMutateOptions = MutateOptions<void, Error, { id: string }>
18+
export type AccountSetActiveMutateOptions = MutateOptions<void, Error, { id: string }>
19+
export type AccountUpdateMutateOptions = MutateOptions<number, Error, { input: AccountUpdateInput }>
2020

21-
export const dbAccountOptions = {
22-
create: (props: DbAccountCreateMutateOptions = {}) =>
21+
export const optionsAccount = {
22+
create: (props: AccountCreateMutateOptions = {}) =>
2323
mutationOptions({
2424
mutationFn: ({ input }: { input: AccountCreateInput }) => accountCreate(db, input),
2525
onSuccess: () => {
26-
queryClient.invalidateQueries(dbSettingOptions.getAll())
27-
queryClient.invalidateQueries(dbSettingOptions.getValue('activeAccountId'))
26+
queryClient.invalidateQueries(optionsSetting.getAll())
27+
queryClient.invalidateQueries(optionsSetting.getValue('activeAccountId'))
2828
},
2929
...props,
3030
}),
31-
delete: (props: DbAccountDeleteMutateOptions = {}) =>
31+
delete: (props: AccountDeleteMutateOptions = {}) =>
3232
mutationOptions({
3333
mutationFn: ({ id }: { id: string }) => accountDelete(db, id),
3434
...props,
@@ -48,17 +48,17 @@ export const dbAccountOptions = {
4848
queryFn: () => accountFindUnique(db, id),
4949
queryKey: ['accountFindUnique', id],
5050
}),
51-
setActive: (props: DbAccountSetActiveMutateOptions = {}) =>
51+
setActive: (props: AccountSetActiveMutateOptions = {}) =>
5252
mutationOptions({
5353
mutationFn: ({ id }: { id: string }) => accountSetActive(db, id),
5454
onSuccess: () => {
55-
queryClient.invalidateQueries(dbSettingOptions.getAll())
56-
queryClient.invalidateQueries(dbSettingOptions.getValue('activeWalletId'))
57-
queryClient.invalidateQueries(dbSettingOptions.getValue('activeAccountId'))
55+
queryClient.invalidateQueries(optionsSetting.getAll())
56+
queryClient.invalidateQueries(optionsSetting.getValue('activeWalletId'))
57+
queryClient.invalidateQueries(optionsSetting.getValue('activeAccountId'))
5858
},
5959
...props,
6060
}),
61-
update: (props: DbAccountUpdateMutateOptions = {}) =>
61+
update: (props: AccountUpdateMutateOptions = {}) =>
6262
mutationOptions({
6363
mutationFn: ({ id, input }: { id: string; input: AccountUpdateInput }) => accountUpdate(db, id, input),
6464
...props,

packages/db-react/src/db-network-options.tsx renamed to packages/db-react/src/options-network.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ import type { NetworkUpdateInput } from '@workspace/db/network/network-update-in
1111
import { toastError } from '@workspace/ui/lib/toast-error'
1212
import { toastSuccess } from '@workspace/ui/lib/toast-success'
1313

14-
export type DbNetworkCreateMutateOptions = MutateOptions<string, Error, { input: NetworkCreateInput }>
15-
export type DbNetworkDeleteMutateOptions = MutateOptions<void, Error, { id: string }>
16-
export type DbNetworkUpdateMutateOptions = MutateOptions<number, Error, { input: NetworkUpdateInput }>
14+
export type NetworkCreateMutateOptions = MutateOptions<string, Error, { input: NetworkCreateInput }>
15+
export type NetworkDeleteMutateOptions = MutateOptions<void, Error, { id: string }>
16+
export type NetworkUpdateMutateOptions = MutateOptions<number, Error, { input: NetworkUpdateInput }>
1717

18-
export const dbNetworkOptions = {
19-
create: (props: DbNetworkCreateMutateOptions = {}) =>
18+
export const optionsNetwork = {
19+
create: (props: NetworkCreateMutateOptions = {}) =>
2020
mutationOptions({
2121
mutationFn: ({ input }: { input: NetworkCreateInput }) => networkCreate(db, input),
2222
onError: () => toastError('Error creating network'),
2323
onSuccess: () => toastSuccess('Network created'),
2424
...props,
2525
}),
26-
delete: (props: DbNetworkDeleteMutateOptions = {}) =>
26+
delete: (props: NetworkDeleteMutateOptions = {}) =>
2727
mutationOptions({
2828
mutationFn: ({ id }: { id: string }) => networkDelete(db, id),
2929
onError: () => toastError('Error deleting network'),
@@ -40,7 +40,7 @@ export const dbNetworkOptions = {
4040
queryFn: () => networkFindUnique(db, id),
4141
queryKey: ['networkFindUnique', id],
4242
}),
43-
update: (props: DbNetworkUpdateMutateOptions) =>
43+
update: (props: NetworkUpdateMutateOptions) =>
4444
mutationOptions({
4545
mutationFn: ({ id, input }: { id: string; input: NetworkUpdateInput }) => networkUpdate(db, id, input),
4646
onError: () => toastError('Error updating network'),

packages/db-react/src/db-setting-options.tsx renamed to packages/db-react/src/options-setting.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { settingGetValue } from '@workspace/db/setting/setting-get-value'
55
import type { SettingKey } from '@workspace/db/setting/setting-key'
66
import { settingSetValue } from '@workspace/db/setting/setting-set-value'
77
import { toastError } from '@workspace/ui/lib/toast-error'
8-
import { queryClient } from './db-query-client.tsx'
8+
import { queryClient } from './query-client.tsx'
99

10-
export type DbSettingSetValueMutateOptions = MutateOptions<void, Error, string>
10+
export type SettingSetValueMutateOptions = MutateOptions<void, Error, string>
1111

12-
export const dbSettingOptions = {
12+
export const optionsSetting = {
1313
getAll: () =>
1414
queryOptions({
1515
queryFn: () => settingGetAll(db),
@@ -20,13 +20,13 @@ export const dbSettingOptions = {
2020
queryFn: () => settingGetValue(db, key),
2121
queryKey: ['settingGetValue', key],
2222
}),
23-
setValue: (key: SettingKey, props: DbSettingSetValueMutateOptions = {}) =>
23+
setValue: (key: SettingKey, props: SettingSetValueMutateOptions = {}) =>
2424
mutationOptions({
2525
mutationFn: (value: string) => settingSetValue(db, key, value),
2626
onError: () => toastError('Error setting value'),
2727
onSuccess: () => {
28-
queryClient.invalidateQueries(dbSettingOptions.getValue(key))
29-
queryClient.invalidateQueries(dbSettingOptions.getAll())
28+
queryClient.invalidateQueries(optionsSetting.getValue(key))
29+
queryClient.invalidateQueries(optionsSetting.getAll())
3030
},
3131
...props,
3232
}),

packages/db-react/src/db-wallet-options.tsx renamed to packages/db-react/src/options-wallet.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import { walletFindUnique } from '@workspace/db/wallet/wallet-find-unique'
99
import { walletSetActive } from '@workspace/db/wallet/wallet-set-active'
1010
import { walletUpdate } from '@workspace/db/wallet/wallet-update'
1111
import type { WalletUpdateInput } from '@workspace/db/wallet/wallet-update-input'
12-
import { queryClient } from './db-query-client.tsx'
13-
import { dbSettingOptions } from './db-setting-options.tsx'
12+
import { optionsSetting } from './options-setting.tsx'
13+
import { queryClient } from './query-client.tsx'
1414

15-
export type DbWalletCreateMutateOptions = MutateOptions<string, Error, { input: WalletCreateInput }>
16-
export type DbWalletDeleteMutateOptions = MutateOptions<void, Error, { id: string }>
17-
export type DbWalletSetActiveMutateOptions = MutateOptions<void, Error, { id: string }>
18-
export type DbWalletUpdateMutateOptions = MutateOptions<number, Error, { input: WalletUpdateInput }>
15+
export type WalletCreateMutateOptions = MutateOptions<string, Error, { input: WalletCreateInput }>
16+
export type WalletDeleteMutateOptions = MutateOptions<void, Error, { id: string }>
17+
export type WalletSetActiveMutateOptions = MutateOptions<void, Error, { id: string }>
18+
export type WalletUpdateMutateOptions = MutateOptions<number, Error, { input: WalletUpdateInput }>
1919

20-
export const dbWalletOptions = {
21-
create: (props: DbWalletCreateMutateOptions = {}) =>
20+
export const optionsWallet = {
21+
create: (props: WalletCreateMutateOptions = {}) =>
2222
mutationOptions({
2323
mutationFn: ({ input }: { input: WalletCreateInput }) => walletCreate(db, input),
2424
onSuccess: () => {
25-
queryClient.invalidateQueries(dbSettingOptions.getAll())
26-
queryClient.invalidateQueries(dbSettingOptions.getValue('activeWalletId'))
25+
queryClient.invalidateQueries(optionsSetting.getAll())
26+
queryClient.invalidateQueries(optionsSetting.getValue('activeWalletId'))
2727
},
2828
...props,
2929
}),
30-
delete: (props: DbWalletDeleteMutateOptions = {}) =>
30+
delete: (props: WalletDeleteMutateOptions = {}) =>
3131
mutationOptions({
3232
mutationFn: ({ id }: { id: string }) => walletDelete(db, id),
3333
...props,
@@ -42,17 +42,17 @@ export const dbWalletOptions = {
4242
queryFn: () => walletFindUnique(db, id),
4343
queryKey: ['walletFindUnique', id],
4444
}),
45-
setActive: (props: DbWalletSetActiveMutateOptions = {}) =>
45+
setActive: (props: WalletSetActiveMutateOptions = {}) =>
4646
mutationOptions({
4747
mutationFn: ({ id }: { id: string }) => walletSetActive(db, id),
4848
onSuccess: () => {
49-
queryClient.invalidateQueries(dbSettingOptions.getAll())
50-
queryClient.invalidateQueries(dbSettingOptions.getValue('activeWalletId'))
51-
queryClient.invalidateQueries(dbSettingOptions.getValue('activeAccountId'))
49+
queryClient.invalidateQueries(optionsSetting.getAll())
50+
queryClient.invalidateQueries(optionsSetting.getValue('activeWalletId'))
51+
queryClient.invalidateQueries(optionsSetting.getValue('activeAccountId'))
5252
},
5353
...props,
5454
}),
55-
update: (props: DbWalletUpdateMutateOptions = {}) =>
55+
update: (props: WalletUpdateMutateOptions = {}) =>
5656
mutationOptions({
5757
mutationFn: ({ id, input }: { id: string; input: WalletUpdateInput }) => walletUpdate(db, id, input),
5858
...props,
File renamed without changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Account } from '@workspace/db/account/account'
2+
import type { Network } from '@workspace/db/network/network'
3+
import type { Setting } from '@workspace/db/setting/setting'
4+
import { getOrFetchQuery } from './get-or-fetch.tsx'
5+
import { optionsAccount } from './options-account.tsx'
6+
import { optionsNetwork } from './options-network.tsx'
7+
import { optionsSetting } from './options-setting.tsx'
8+
import { queryClient } from './query-client.tsx'
9+
10+
export interface RootLoaderData {
11+
accounts: Account[]
12+
networks: Network[]
13+
settings: Setting[]
14+
}
15+
16+
export async function rootLoader() {
17+
const [networks, settings] = await Promise.all([
18+
getOrFetchQuery(queryClient, optionsNetwork.findMany({})),
19+
getOrFetchQuery(queryClient, optionsSetting.getAll()),
20+
])
21+
22+
const activeWalletId = settings.find((s) => s.key === 'activeWalletId')?.value
23+
const accounts = activeWalletId
24+
? await getOrFetchQuery(queryClient, optionsAccount.findByWalletId(activeWalletId))
25+
: []
26+
27+
return {
28+
accounts,
29+
networks,
30+
settings,
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useMemo } from 'react'
2+
import { useAccountLive } from './use-account-live.tsx'
3+
import { useSetting } from './use-setting.tsx'
4+
5+
export function useAccountActive() {
6+
const [accountId] = useSetting('activeAccountId')
7+
const [walletId] = useSetting('activeWalletId')
8+
if (!walletId) {
9+
throw new Error('No active wallet set.')
10+
}
11+
const accountLive = useAccountLive({ walletId })
12+
const account = useMemo(() => accountLive.find((item) => item.id === accountId), [accountId, accountLive])
13+
if (!account) {
14+
throw new Error('No active account set.')
15+
}
16+
17+
return account
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { useMutation } from '@tanstack/react-query'
2+
import type { AccountCreateMutateOptions } from './options-account.tsx'
3+
import { optionsAccount } from './options-account.tsx'
4+
5+
export function useAccountCreate(props: AccountCreateMutateOptions = {}) {
6+
return useMutation(optionsAccount.create(props))
7+
}

0 commit comments

Comments
 (0)