Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve number of requests #183

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 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,18 +14,12 @@ 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>
<a
href={`https://faucet-testnet.fuel.network/?address=${address}`}
href={`https://faucet-testnet.fuel.network/?address=${address}&autoClose`}
target="_blank"
className={`btn ${
isSigning
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
Loading