Skip to content

Commit

Permalink
Cleanup network provider (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Mar 16, 2020
1 parent f045563 commit 0df6675
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
14 changes: 3 additions & 11 deletions src/components/networkStatusButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ function NetworkStatusButton() {

switch (status) {
case NetworkStatus.Connected:
return (
<a href="#networkModal" className="btn btn-primary lift">
{url}
</a>
);
return <a className="btn btn-primary lift">{url}</a>;

case NetworkStatus.Connecting:
return (
<a href="#networkModal" className="btn btn-warning lift">
<a className="btn btn-warning lift">
{"Connecting "}
<span
className="spinner-grow spinner-grow-sm text-dark"
Expand All @@ -25,11 +21,7 @@ function NetworkStatusButton() {
);

case NetworkStatus.Failure:
return (
<a href="#networkModal" className="btn btn-danger lift">
Disconnected
</a>
);
return <a className="btn btn-danger lift">Disconnected</a>;
}
}

Expand Down
59 changes: 22 additions & 37 deletions src/providers/network.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<State | undefined>(undefined);
const DispatchContext = React.createContext<Dispatch | undefined>(undefined);

function networkReducer(state: State, action: Action): State {
switch (action.status) {
case NetworkStatus.Connected:
Expand All @@ -43,31 +43,24 @@ 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,
status: NetworkStatus.Connecting
};
}

const DEFAULT_URL = testnetChannelEndpoint("stable");
const StateContext = React.createContext<State | undefined>(undefined);
const DispatchContext = React.createContext<Dispatch | undefined>(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
Expand All @@ -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
Expand All @@ -99,26 +92,18 @@ 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`);
}
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
};
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 0df6675

Please sign in to comment.