Fix bug causing eager fetching of rich list
This commit is contained in:
parent
94531f0879
commit
e962d40815
|
@ -1,5 +1,5 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useSupply, useFetchSupply } from "providers/supply";
|
import { useSupply, useFetchSupply, Status } from "providers/supply";
|
||||||
import LoadingCard from "./common/LoadingCard";
|
import LoadingCard from "./common/LoadingCard";
|
||||||
import ErrorCard from "./common/ErrorCard";
|
import ErrorCard from "./common/ErrorCard";
|
||||||
import { lamportsToSolString } from "utils";
|
import { lamportsToSolString } from "utils";
|
||||||
|
@ -9,11 +9,18 @@ export default function SupplyCard() {
|
||||||
const supply = useSupply();
|
const supply = useSupply();
|
||||||
const fetchSupply = useFetchSupply();
|
const fetchSupply = useFetchSupply();
|
||||||
|
|
||||||
if (typeof supply === "boolean") {
|
// Fetch supply on load
|
||||||
if (supply) return <LoadingCard />;
|
React.useEffect(() => {
|
||||||
|
fetchSupply();
|
||||||
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
if (supply === Status.Disconnected) {
|
||||||
return <ErrorCard text="Not connected to the cluster" />;
|
return <ErrorCard text="Not connected to the cluster" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (supply === Status.Idle || supply === Status.Connecting)
|
||||||
|
return <LoadingCard />;
|
||||||
|
|
||||||
if (typeof supply === "string") {
|
if (typeof supply === "string") {
|
||||||
return <ErrorCard text={supply} retry={fetchSupply} />;
|
return <ErrorCard text={supply} retry={fetchSupply} />;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import React from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { AccountBalancePair } from "@solana/web3.js";
|
import { AccountBalancePair } from "@solana/web3.js";
|
||||||
import Copyable from "./Copyable";
|
import Copyable from "./Copyable";
|
||||||
import { useRichList, useFetchRichList } from "providers/richList";
|
import { useRichList, useFetchRichList, Status } from "providers/richList";
|
||||||
import LoadingCard from "./common/LoadingCard";
|
import LoadingCard from "./common/LoadingCard";
|
||||||
import ErrorCard from "./common/ErrorCard";
|
import ErrorCard from "./common/ErrorCard";
|
||||||
import { lamportsToSolString } from "utils";
|
import { lamportsToSolString } from "utils";
|
||||||
|
@ -11,11 +11,18 @@ export default function TopAccountsCard() {
|
||||||
const richList = useRichList();
|
const richList = useRichList();
|
||||||
const fetchRichList = useFetchRichList();
|
const fetchRichList = useFetchRichList();
|
||||||
|
|
||||||
if (typeof richList === "boolean") {
|
// Fetch on load
|
||||||
if (richList) return <LoadingCard />;
|
React.useEffect(() => {
|
||||||
|
fetchRichList();
|
||||||
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
if (richList === Status.Disconnected) {
|
||||||
return <ErrorCard text="Not connected to the cluster" />;
|
return <ErrorCard text="Not connected to the cluster" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (richList === Status.Idle || richList === Status.Connecting)
|
||||||
|
return <LoadingCard />;
|
||||||
|
|
||||||
if (typeof richList === "string") {
|
if (typeof richList === "string") {
|
||||||
return <ErrorCard text={richList} retry={fetchRichList} />;
|
return <ErrorCard text={richList} retry={fetchRichList} />;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,19 @@ import React from "react";
|
||||||
import { AccountBalancePair, Connection } from "@solana/web3.js";
|
import { AccountBalancePair, Connection } from "@solana/web3.js";
|
||||||
import { useCluster, ClusterStatus } from "./cluster";
|
import { useCluster, ClusterStatus } from "./cluster";
|
||||||
|
|
||||||
|
export enum Status {
|
||||||
|
Idle,
|
||||||
|
Disconnected,
|
||||||
|
Connecting
|
||||||
|
}
|
||||||
|
|
||||||
type RichList = {
|
type RichList = {
|
||||||
accounts: AccountBalancePair[];
|
accounts: AccountBalancePair[];
|
||||||
totalSupply: number;
|
totalSupply: number;
|
||||||
circulatingSupply: number;
|
circulatingSupply: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = RichList | boolean | string;
|
type State = RichList | Status | string;
|
||||||
|
|
||||||
type Dispatch = React.Dispatch<React.SetStateAction<State>>;
|
type Dispatch = React.Dispatch<React.SetStateAction<State>>;
|
||||||
const StateContext = React.createContext<State | undefined>(undefined);
|
const StateContext = React.createContext<State | undefined>(undefined);
|
||||||
|
@ -17,13 +23,16 @@ const DispatchContext = React.createContext<Dispatch | undefined>(undefined);
|
||||||
|
|
||||||
type Props = { children: React.ReactNode };
|
type Props = { children: React.ReactNode };
|
||||||
export function RichListProvider({ children }: Props) {
|
export function RichListProvider({ children }: Props) {
|
||||||
const [state, setState] = React.useState<State>(false);
|
const [state, setState] = React.useState<State>(Status.Idle);
|
||||||
const { status, url } = useCluster();
|
const { status: clusterStatus, url } = useCluster();
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (status === ClusterStatus.Connecting) setState(false);
|
if (state !== Status.Idle) {
|
||||||
if (status === ClusterStatus.Connected) fetch(setState, url);
|
if (clusterStatus === ClusterStatus.Connecting)
|
||||||
}, [status, url]);
|
setState(Status.Disconnected);
|
||||||
|
if (clusterStatus === ClusterStatus.Connected) fetch(setState, url);
|
||||||
|
}
|
||||||
|
}, [clusterStatus, url]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StateContext.Provider value={state}>
|
<StateContext.Provider value={state}>
|
||||||
|
@ -35,7 +44,8 @@ export function RichListProvider({ children }: Props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetch(dispatch: Dispatch, url: string) {
|
async function fetch(dispatch: Dispatch, url: string) {
|
||||||
dispatch(true);
|
dispatch(Status.Connecting);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const connection = new Connection(url, "max");
|
const connection = new Connection(url, "max");
|
||||||
const supply = (await connection.getSupply()).value;
|
const supply = (await connection.getSupply()).value;
|
||||||
|
@ -43,9 +53,9 @@ async function fetch(dispatch: Dispatch, url: string) {
|
||||||
await connection.getLargestAccounts({ filter: "circulating" })
|
await connection.getLargestAccounts({ filter: "circulating" })
|
||||||
).value;
|
).value;
|
||||||
|
|
||||||
// Update state if selected cluster hasn't changed
|
// Update state if still connecting
|
||||||
dispatch(state => {
|
dispatch(state => {
|
||||||
if (!state) return state;
|
if (state !== Status.Connecting) return state;
|
||||||
return {
|
return {
|
||||||
accounts,
|
accounts,
|
||||||
totalSupply: supply.total,
|
totalSupply: supply.total,
|
||||||
|
|
|
@ -3,7 +3,13 @@ import React from "react";
|
||||||
import { Supply, Connection } from "@solana/web3.js";
|
import { Supply, Connection } from "@solana/web3.js";
|
||||||
import { useCluster, ClusterStatus } from "./cluster";
|
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>>;
|
type Dispatch = React.Dispatch<React.SetStateAction<State>>;
|
||||||
const StateContext = React.createContext<State | undefined>(undefined);
|
const StateContext = React.createContext<State | undefined>(undefined);
|
||||||
|
@ -11,13 +17,16 @@ const DispatchContext = React.createContext<Dispatch | undefined>(undefined);
|
||||||
|
|
||||||
type Props = { children: React.ReactNode };
|
type Props = { children: React.ReactNode };
|
||||||
export function SupplyProvider({ children }: Props) {
|
export function SupplyProvider({ children }: Props) {
|
||||||
const [state, setState] = React.useState<State>(false);
|
const [state, setState] = React.useState<State>(Status.Idle);
|
||||||
const { status, url } = useCluster();
|
const { status: clusterStatus, url } = useCluster();
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (status === ClusterStatus.Connecting) setState(false);
|
if (state !== Status.Idle) {
|
||||||
if (status === ClusterStatus.Connected) fetch(setState, url);
|
if (clusterStatus === ClusterStatus.Connecting)
|
||||||
}, [status, url]);
|
setState(Status.Disconnected);
|
||||||
|
if (clusterStatus === ClusterStatus.Connected) fetch(setState, url);
|
||||||
|
}
|
||||||
|
}, [clusterStatus, url]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StateContext.Provider value={state}>
|
<StateContext.Provider value={state}>
|
||||||
|
@ -29,14 +38,15 @@ export function SupplyProvider({ children }: Props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetch(dispatch: Dispatch, url: string) {
|
async function fetch(dispatch: Dispatch, url: string) {
|
||||||
dispatch(true);
|
dispatch(Status.Connecting);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const connection = new Connection(url, "max");
|
const connection = new Connection(url, "max");
|
||||||
const supply = (await connection.getSupply()).value;
|
const supply = (await connection.getSupply()).value;
|
||||||
|
|
||||||
// Update state if selected cluster hasn't changed
|
// Update state if still connecting
|
||||||
dispatch(state => {
|
dispatch(state => {
|
||||||
if (!state) return state;
|
if (state !== Status.Connecting) return state;
|
||||||
return supply;
|
return supply;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Reference in New Issue