From d80149333ce55f2a6713b97257f2a7b105097b9f Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Mon, 16 Mar 2020 15:17:51 +0800 Subject: [PATCH] Cleanup network provider (#3) --- .../src/components/networkStatusButton.tsx | 14 +---- explorer/src/providers/network.tsx | 59 +++++++------------ explorer/src/utils.ts | 12 ++++ 3 files changed, 37 insertions(+), 48 deletions(-) create mode 100644 explorer/src/utils.ts diff --git a/explorer/src/components/networkStatusButton.tsx b/explorer/src/components/networkStatusButton.tsx index 231ed1dfff4f59..3181b1ba0eb6fe 100644 --- a/explorer/src/components/networkStatusButton.tsx +++ b/explorer/src/components/networkStatusButton.tsx @@ -6,15 +6,11 @@ function NetworkStatusButton() { switch (status) { case NetworkStatus.Connected: - return ( - - {url} - - ); + return {url}; case NetworkStatus.Connecting: return ( - + {"Connecting "} - Disconnected - - ); + return Disconnected; } } diff --git a/explorer/src/providers/network.tsx b/explorer/src/providers/network.tsx index 483e0dc887f992..c6dd14b2925f12 100644 --- a/explorer/src/providers/network.tsx +++ b/explorer/src/providers/network.tsx @@ -1,12 +1,20 @@ import React from "react"; import { testnetChannelEndpoint, Connection } from "@solana/web3.js"; +import { findGetParameter } from "../utils"; -enum NetworkStatus { +export const DEFAULT_URL = testnetChannelEndpoint("stable"); + +export enum NetworkStatus { Connected, Connecting, Failure } +interface State { + url: string; + status: NetworkStatus; +} + interface Connecting { status: NetworkStatus.Connecting; url: string; @@ -23,14 +31,6 @@ interface Failure { type Action = Connected | Connecting | Failure; type Dispatch = (action: Action) => void; -interface State { - url: string; - status: NetworkStatus; -} - -const StateContext = React.createContext(undefined); -const DispatchContext = React.createContext(undefined); - function networkReducer(state: State, action: Action): State { switch (action.status) { case NetworkStatus.Connected: @@ -43,20 +43,7 @@ function networkReducer(state: State, action: Action): State { } } -function findGetParameter(parameterName: string): string | null { - let result = null, - tmp = []; - window.location.search - .substr(1) - .split("&") - .forEach(function(item) { - tmp = item.split("="); - if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); - }); - return result; -} - -function init(url: string): State { +function initState(url: string): State { const networkUrlParam = findGetParameter("networkUrl"); return { url: networkUrlParam || url, @@ -64,10 +51,16 @@ function init(url: string): State { }; } -const DEFAULT_URL = testnetChannelEndpoint("stable"); +const StateContext = React.createContext(undefined); +const DispatchContext = React.createContext(undefined); + type NetworkProviderProps = { children: React.ReactNode }; -function NetworkProvider({ children }: NetworkProviderProps) { - const [state, dispatch] = React.useReducer(networkReducer, DEFAULT_URL, init); +export function NetworkProvider({ children }: NetworkProviderProps) { + const [state, dispatch] = React.useReducer( + networkReducer, + DEFAULT_URL, + initState + ); React.useEffect(() => { // Connect to network immediately @@ -83,7 +76,7 @@ function NetworkProvider({ children }: NetworkProviderProps) { ); } -async function updateNetwork(dispatch: Dispatch, newUrl: string) { +export async function updateNetwork(dispatch: Dispatch, newUrl: string) { dispatch({ status: NetworkStatus.Connecting, url: newUrl @@ -99,7 +92,7 @@ async function updateNetwork(dispatch: Dispatch, newUrl: string) { } } -function useNetwork() { +export function useNetwork() { const context = React.useContext(StateContext); if (!context) { throw new Error(`useNetwork must be used within a NetworkProvider`); @@ -107,18 +100,10 @@ function useNetwork() { return context; } -function useNetworkDispatch() { +export function useNetworkDispatch() { const context = React.useContext(DispatchContext); if (!context) { throw new Error(`useNetworkDispatch must be used within a NetworkProvider`); } return context; } - -export { - NetworkProvider, - useNetwork, - useNetworkDispatch, - updateNetwork, - NetworkStatus -}; diff --git a/explorer/src/utils.ts b/explorer/src/utils.ts new file mode 100644 index 00000000000000..fc96ca2a2b6d0c --- /dev/null +++ b/explorer/src/utils.ts @@ -0,0 +1,12 @@ +export function findGetParameter(parameterName: string): string | null { + let result = null, + tmp = []; + window.location.search + .substr(1) + .split("&") + .forEach(function(item) { + tmp = item.split("="); + if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); + }); + return result; +}