Skip to content

Commit

Permalink
Fix bug causing eager fetching of rich list
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored and mvines committed Jun 12, 2020
1 parent 0170300 commit aec7f02
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
13 changes: 10 additions & 3 deletions explorer/src/components/SupplyCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { useSupply, useFetchSupply } from "providers/supply";
import { useSupply, useFetchSupply, Status } from "providers/supply";
import LoadingCard from "./common/LoadingCard";
import ErrorCard from "./common/ErrorCard";
import { lamportsToSolString } from "utils";
Expand All @@ -9,11 +9,18 @@ export default function SupplyCard() {
const supply = useSupply();
const fetchSupply = useFetchSupply();

if (typeof supply === "boolean") {
if (supply) return <LoadingCard />;
// Fetch supply on load
React.useEffect(() => {
fetchSupply();
}, []); // eslint-disable-line react-hooks/exhaustive-deps

if (supply === Status.Disconnected) {
return <ErrorCard text="Not connected to the cluster" />;
}

if (supply === Status.Idle || supply === Status.Connecting)
return <LoadingCard />;

if (typeof supply === "string") {
return <ErrorCard text={supply} retry={fetchSupply} />;
}
Expand Down
13 changes: 10 additions & 3 deletions explorer/src/components/TopAccountsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { Link } from "react-router-dom";
import { AccountBalancePair } from "@solana/web3.js";
import Copyable from "./Copyable";
import { useRichList, useFetchRichList } from "providers/richList";
import { useRichList, useFetchRichList, Status } from "providers/richList";
import LoadingCard from "./common/LoadingCard";
import ErrorCard from "./common/ErrorCard";
import { lamportsToSolString } from "utils";
Expand All @@ -11,11 +11,18 @@ export default function TopAccountsCard() {
const richList = useRichList();
const fetchRichList = useFetchRichList();

if (typeof richList === "boolean") {
if (richList) return <LoadingCard />;
// Fetch on load
React.useEffect(() => {
fetchRichList();
}, []); // eslint-disable-line react-hooks/exhaustive-deps

if (richList === Status.Disconnected) {
return <ErrorCard text="Not connected to the cluster" />;
}

if (richList === Status.Idle || richList === Status.Connecting)
return <LoadingCard />;

if (typeof richList === "string") {
return <ErrorCard text={richList} retry={fetchRichList} />;
}
Expand Down
28 changes: 19 additions & 9 deletions explorer/src/providers/richList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@ import React from "react";
import { AccountBalancePair, Connection } from "@solana/web3.js";
import { useCluster, ClusterStatus } from "./cluster";

export enum Status {
Idle,
Disconnected,
Connecting
}

type RichList = {
accounts: AccountBalancePair[];
totalSupply: number;
circulatingSupply: number;
};

type State = RichList | boolean | string;
type State = RichList | Status | string;

type Dispatch = React.Dispatch<React.SetStateAction<State>>;
const StateContext = React.createContext<State | undefined>(undefined);
const DispatchContext = React.createContext<Dispatch | undefined>(undefined);

type Props = { children: React.ReactNode };
export function RichListProvider({ children }: Props) {
const [state, setState] = React.useState<State>(false);
const { status, url } = useCluster();
const [state, setState] = React.useState<State>(Status.Idle);
const { status: clusterStatus, url } = useCluster();

React.useEffect(() => {
if (status === ClusterStatus.Connecting) setState(false);
if (status === ClusterStatus.Connected) fetch(setState, url);
}, [status, url]);
if (state !== Status.Idle) {
if (clusterStatus === ClusterStatus.Connecting)
setState(Status.Disconnected);
if (clusterStatus === ClusterStatus.Connected) fetch(setState, url);
}
}, [clusterStatus, url]); // eslint-disable-line react-hooks/exhaustive-deps

return (
<StateContext.Provider value={state}>
Expand All @@ -35,17 +44,18 @@ export function RichListProvider({ children }: Props) {
}

async function fetch(dispatch: Dispatch, url: string) {
dispatch(true);
dispatch(Status.Connecting);

try {
const connection = new Connection(url, "max");
const supply = (await connection.getSupply()).value;
const accounts = (
await connection.getLargestAccounts({ filter: "circulating" })
).value;

// Update state if selected cluster hasn't changed
// Update state if still connecting
dispatch(state => {
if (!state) return state;
if (state !== Status.Connecting) return state;
return {
accounts,
totalSupply: supply.total,
Expand Down
28 changes: 19 additions & 9 deletions explorer/src/providers/supply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ import React from "react";
import { Supply, Connection } from "@solana/web3.js";
import { useCluster, ClusterStatus } from "./cluster";

type State = Supply | boolean | string;
export enum Status {
Idle,
Disconnected,
Connecting
}

type State = Supply | Status | string;

type Dispatch = React.Dispatch<React.SetStateAction<State>>;
const StateContext = React.createContext<State | undefined>(undefined);
const DispatchContext = React.createContext<Dispatch | undefined>(undefined);

type Props = { children: React.ReactNode };
export function SupplyProvider({ children }: Props) {
const [state, setState] = React.useState<State>(false);
const { status, url } = useCluster();
const [state, setState] = React.useState<State>(Status.Idle);
const { status: clusterStatus, url } = useCluster();

React.useEffect(() => {
if (status === ClusterStatus.Connecting) setState(false);
if (status === ClusterStatus.Connected) fetch(setState, url);
}, [status, url]);
if (state !== Status.Idle) {
if (clusterStatus === ClusterStatus.Connecting)
setState(Status.Disconnected);
if (clusterStatus === ClusterStatus.Connected) fetch(setState, url);
}
}, [clusterStatus, url]); // eslint-disable-line react-hooks/exhaustive-deps

return (
<StateContext.Provider value={state}>
Expand All @@ -29,14 +38,15 @@ export function SupplyProvider({ children }: Props) {
}

async function fetch(dispatch: Dispatch, url: string) {
dispatch(true);
dispatch(Status.Connecting);

try {
const connection = new Connection(url, "max");
const supply = (await connection.getSupply()).value;

// Update state if selected cluster hasn't changed
// Update state if still connecting
dispatch(state => {
if (!state) return state;
if (state !== Status.Connecting) return state;
return supply;
});
} catch (err) {
Expand Down

0 comments on commit aec7f02

Please sign in to comment.