Skip to content

Commit

Permalink
fix: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-eren committed Jan 14, 2024
1 parent 552eece commit dc504f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/contexts/BlockNumberContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const useBlockNumber = (): number => {
const context = useContext(BlockNumberContext);

if (context === null) {
throw new Error('You must wrap your app content in EthooksProvider component');
console.error('You must wrap your app content in EthooksProvider component');
return 0;
}

return context;
Expand Down
6 changes: 4 additions & 2 deletions src/contexts/EthersContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export const EthersContext = createContext<EthersProvider | null>(null);
export const useProvider = () => {
const context = useContext(EthersContext);

if (!context)
throw new Error('You must pass provider prop with a provider to EthooksProvider component');
if (!context) {
console.error('You must pass provider prop with a provider to EthooksProvider component');
return null;
}

return context;
};
18 changes: 11 additions & 7 deletions src/contexts/RootContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ export interface RootContextType {
ensName?: string;
chains: Chain[];
}
export type RootContextDispatch = React.Dispatch<React.SetStateAction<RootContextType>>;

export interface RootContextProviderProps {
initialState: RootContextType;
children: React.ReactNode;
}

export const RootContext = createContext<
[RootContextType, React.Dispatch<React.SetStateAction<RootContextType>>] | null
>(null);
export const RootContext = createContext<[RootContextType, RootContextDispatch] | null>(null);

export const RootContextProvider: React.FC<RootContextProviderProps> = (props) => {
const {children, initialState} = props;
Expand All @@ -29,23 +28,28 @@ export const RootContextProvider: React.FC<RootContextProviderProps> = (props) =
export const useRootContext = (): RootContextType => {
const context = useContext(RootContext);

if (!context) throw new Error('You must wrap your app content in EthooksProvider component');
if (!context) {
console.error('You must wrap your app content in EthooksProvider component');
return {chains: []};
}

return context[0];
};

export const useSetRootContext = () => {
const context = useContext(RootContext);

if (!context) throw new Error('You must wrap your app content in EthooksProvider component');
if (!context) {
console.error('You must wrap your app content in EthooksProvider component');
}

const setState = context[1];
const setState = context?.[1];

return useCallback(
(
action: Partial<RootContextType> | ((previous: RootContextType) => Partial<RootContextType>),
): void => {
setState((prev) => ({...prev, ...(typeof action === 'function' ? action(prev) : action)}));
setState?.((prev) => ({...prev, ...(typeof action === 'function' ? action(prev) : action)}));
},
[setState],
);
Expand Down

0 comments on commit dc504f6

Please sign in to comment.