Skip to content

Commit

Permalink
feat: improve number of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio committed Aug 8, 2024
1 parent 0234329 commit 697b147
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
9 changes: 1 addition & 8 deletions examples/react-app/src/components/balance.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bn } from 'fuels';
import { useEffect } from 'react';
import { useWallet } from '../hooks/useWallet';
import Feature from './feature';

Expand All @@ -15,13 +14,7 @@ const BalanceSkeleton = () => (
);

export default function Balance({ isSigning }: Props) {
const { refetchWallet, balance, address } = useWallet();

useEffect(() => {
const interval = setInterval(() => refetchWallet(), 5000);
return () => clearInterval(interval);
}, [refetchWallet]);

const { balance, address } = useWallet();
return (
<Feature title="Balance">
<code>{balance ? `${balance?.format()} ETH` : <BalanceSkeleton />}</code>
Expand Down
2 changes: 1 addition & 1 deletion examples/react-app/src/components/transfer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Provider } from 'fuels';
import { Address } from 'fuels';
import { useState } from 'react';
import { useWallet } from '../hooks/useWallet';
import type { CustomError } from '../utils/customError';
Expand Down
16 changes: 8 additions & 8 deletions examples/react-app/src/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useWallet = () => {
isConnecting,
isLoading: isLoadingConnectors,
} = useConnectUI();
const { isConnected, refetch: refetchConnected } = useIsConnected();
const { isConnected } = useIsConnected();
const {
account,
isLoading: isLoadingAccount,
Expand All @@ -40,15 +40,16 @@ export const useWallet = () => {
balance,
isLoading: isLoadingBalance,
isFetching: isFetchingBalance,
} = useBalance({ address });

} = useBalance(
{ address },
{
refetchOnWindowFocus: true,
refetchInterval: 10000,
},
);
const [currentConnector, setCurrentConnector] =
useState<ICurrentConnector>(DEFAULT_CONNECTOR);

useEffect(() => {
refetchConnected();
}, [refetchConnected]);

useEffect(() => {
if (!isConnected) {
setCurrentConnector(DEFAULT_CONNECTOR);
Expand Down Expand Up @@ -83,7 +84,6 @@ export const useWallet = () => {
isLoadingConnectors,
wallet,
connect,
refetchConnected,
refetchWallet,
};
};
5 changes: 5 additions & 0 deletions packages/react/src/core/useNamedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ export function useNamedQuery<

return proxy;
}

export type ServiceOptions<TQueryData, TError, TData> = Omit<
UseQueryOptions<TQueryData, TError, TData>,
'queryFn' | 'queryKey' | 'initialData' | 'data'
>;
36 changes: 12 additions & 24 deletions packages/react/src/hooks/useBalance.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type { BytesLike } from 'fuels';
import type { BN, BytesLike } from 'fuels';
import { Address } from 'fuels';
import { useEffect } from 'react';

import { useNamedQuery } from '../core';
import { type ServiceOptions, useNamedQuery } from '../core';
import { QUERY_KEYS } from '../utils';

import { useProvider } from './useProvider';

export const useBalance = ({
address,
assetId,
}: {
type UseBalanceParams = {
address?: string;
assetId?: BytesLike;
}) => {
const { provider } = useProvider();
};

const query = useNamedQuery('balance', {
export const useBalance = (
{ address, assetId }: UseBalanceParams,
options?: ServiceOptions<unknown, Error, BN | null>,
) => {
const { provider } = useProvider();
return useNamedQuery('balance', {
...options,
queryKey: QUERY_KEYS.balance(address, assetId),
queryFn: async () => {
try {
Expand All @@ -32,21 +33,8 @@ export const useBalance = ({
return null;
}
},
refetchOnWindowFocus: true,
initialData: null,
enabled: !!provider,
});

useEffect(() => {
const listenerAccountFetcher = () => {
query.refetch();
};

window.addEventListener('focus', listenerAccountFetcher);

return () => {
window.removeEventListener('focus', listenerAccountFetcher);
};
}, [query]);

return query;
};
14 changes: 11 additions & 3 deletions packages/react/src/providers/FuelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ type FuelProviderProps = {
fuelConfig?: FuelConfig;
} & FuelUIProviderProps;

function FuelUI({ theme, children, fuelConfig }: FuelProviderProps) {
return (
<FuelUIProvider theme={theme} fuelConfig={fuelConfig}>
<Connect />
{children}
</FuelUIProvider>
);
}

export function FuelProvider({
theme,
children,
Expand All @@ -22,10 +31,9 @@ export function FuelProvider({
if (ui) {
return (
<FuelHooksProvider fuelConfig={fuelConfig}>
<FuelUIProvider theme={theme} fuelConfig={fuelConfig}>
<Connect />
<FuelUI fuelConfig={fuelConfig} theme={theme}>
{children}
</FuelUIProvider>
</FuelUI>
</FuelHooksProvider>
);
}
Expand Down

0 comments on commit 697b147

Please sign in to comment.