From 1e232fe19df621f8162e5fec2e57ff2911e8fcf0 Mon Sep 17 00:00:00 2001 From: Nikita Yutanov Date: Tue, 3 Oct 2023 01:05:50 +0300 Subject: [PATCH] Fix value type assertion --- utils/gear-hooks/src/context/Api.tsx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/utils/gear-hooks/src/context/Api.tsx b/utils/gear-hooks/src/context/Api.tsx index 1dd860fc8f..ae520ed9f3 100644 --- a/utils/gear-hooks/src/context/Api.tsx +++ b/utils/gear-hooks/src/context/Api.tsx @@ -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(initialValue); const { Provider } = ApiContext; function ApiProvider({ initialArgs, children }: Props) { const [api, setApi] = useState(); - const isApiReady = !!api; - const providerRef = useRef(); const switchNetwork = async (args: ProviderArgs) => { @@ -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 {children}; + return {children}; } export { ApiContext, ApiProvider };