Skip to content

Commit

Permalink
feat: use Fuel SDK on react (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio authored Oct 13, 2023
1 parent 154d5e4 commit d6b847d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-seas-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuel-wallet/react': patch
---

feat: use Fuel SDK instead of window.fuel
30 changes: 19 additions & 11 deletions packages/react/src/components/FuelProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Fuel } from '@fuel-wallet/sdk';
import { Fuel } from '@fuel-wallet/sdk';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { ReactNode } from 'react';
import { createContext, useContext, useEffect } from 'react';

import { useWindowFuel } from '../hooks';
import { QUERY_KEYS } from '../utils';

export const fuelQueryClient = new QueryClient({
Expand Down Expand Up @@ -38,7 +37,14 @@ export const useFuel = () => {
};

export const FuelProvider = ({ children }: FuelProviderProps) => {
const fuel = useWindowFuel();
const fuel = new Fuel();

function onFuelLoaded() {
const connectorName = localStorage.getItem('connector');
if (connectorName) {
fuel.selectConnector(connectorName);
}
}

function onCurrentAccountChange() {
fuelQueryClient.invalidateQueries([QUERY_KEYS.account]);
Expand Down Expand Up @@ -67,16 +73,18 @@ export const FuelProvider = ({ children }: FuelProviderProps) => {
}

useEffect(() => {
fuel?.on(fuel.events.currentAccount, onCurrentAccountChange);
fuel?.on(fuel.events.connection, onConnectionChange);
fuel?.on(fuel.events.accounts, onAccountsChange);
fuel?.on(fuel.events.network, onNetworkChange);
fuel.on(fuel.events.currentAccount, onCurrentAccountChange);
fuel.on(fuel.events.connection, onConnectionChange);
fuel.on(fuel.events.accounts, onAccountsChange);
fuel.on(fuel.events.network, onNetworkChange);
fuel.on(fuel.events.load, onFuelLoaded);

return () => {
fuel?.off(fuel.events.currentAccount, onCurrentAccountChange);
fuel?.off(fuel.events.connection, onConnectionChange);
fuel?.off(fuel.events.accounts, onAccountsChange);
fuel?.off(fuel.events.network, onNetworkChange);
fuel.off(fuel.events.currentAccount, onCurrentAccountChange);
fuel.off(fuel.events.connection, onConnectionChange);
fuel.off(fuel.events.accounts, onAccountsChange);
fuel.off(fuel.events.network, onNetworkChange);
fuel.off(fuel.events.load, onFuelLoaded);
};
}, [fuel]);

Expand Down
1 change: 0 additions & 1 deletion packages/react/src/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ export * from './useProvider';
export * from './useTransaction';
export * from './useTransactionReceipts';
export * from './useWallet';
export * from './useWindowFuel';
5 changes: 2 additions & 3 deletions packages/react/src/hooks/useConnectors.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useQuery } from '@tanstack/react-query';

import { useFuel } from '../components';
import { QUERY_KEYS } from '../utils';

import { useWindowFuel } from './useWindowFuel';

export const useConnectors = () => {
const fuel = useWindowFuel();
const { fuel } = useFuel();

const query = useQuery(
[QUERY_KEYS.chain, fuel],
Expand Down
28 changes: 0 additions & 28 deletions packages/react/src/hooks/useWindowFuel.tsx

This file was deleted.

0 comments on commit d6b847d

Please sign in to comment.