Skip to content

Commit

Permalink
Fix value type assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Oct 2, 2023
1 parent 034d4f1 commit 1e232fe
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions utils/gear-hooks/src/context/Api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ type Props = ProviderProps & {
initialArgs: ProviderArgs;
};

const ApiContext = createContext({} as Value);
const initialValue = {
api: undefined,
isApiReady: false as const,
switchNetwork: () => Promise.resolve(),
};

const ApiContext = createContext<Value>(initialValue);
const { Provider } = ApiContext;

function ApiProvider({ initialArgs, children }: Props) {
const [api, setApi] = useState<GearApi>();
const isApiReady = !!api;

const providerRef = useRef<WsProvider | ScProvider>();

const switchNetwork = async (args: ProviderArgs) => {
Expand Down Expand Up @@ -82,9 +86,13 @@ function ApiProvider({ initialArgs, children }: Props) {
switchNetwork(initialArgs);
}, []);

const value = useMemo(() => ({ api, isApiReady, switchNetwork }), [api, isApiReady]);
const value = useMemo(
() =>
api ? { api, isApiReady: true as const, switchNetwork } : { api, isApiReady: false as const, switchNetwork },
[api],
);

return <Provider value={value as Value}>{children}</Provider>;
return <Provider value={value}>{children}</Provider>;
}

export { ApiContext, ApiProvider };

0 comments on commit 1e232fe

Please sign in to comment.