fix: blank page if startup subscription fails (#2357)

* feat: add loading skeleton for network loader

* feat: add timeout for subscription check
This commit is contained in:
Matthew Russell 2022-12-08 02:51:01 -06:00 committed by GitHub
parent def34a329d
commit 09af8db1fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -8,7 +8,8 @@ import {
Web3Provider as Web3ProviderInternal, Web3Provider as Web3ProviderInternal,
useWeb3ConnectStore, useWeb3ConnectStore,
} from '@vegaprotocol/web3'; } from '@vegaprotocol/web3';
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit'; import { AsyncRenderer, Splash } from '@vegaprotocol/ui-toolkit';
import { t } from '@vegaprotocol/react-helpers';
interface AppLoaderProps { interface AppLoaderProps {
children: ReactNode; children: ReactNode;
@ -19,7 +20,14 @@ interface AppLoaderProps {
* that must happen for it can be used * that must happen for it can be used
*/ */
export function AppLoader({ children }: AppLoaderProps) { export function AppLoader({ children }: AppLoaderProps) {
return <NetworkLoader cache={cacheConfig}>{children}</NetworkLoader>; return (
<NetworkLoader
skeleton={<Splash>{t('Loading...')}</Splash>}
cache={cacheConfig}
>
{children}
</NetworkLoader>
);
} }
export const Web3Provider = ({ children }: { children: ReactNode }) => { export const Web3Provider = ({ children }: { children: ReactNode }) => {

View File

@ -12,6 +12,12 @@ type Callbacks = {
onSubscriptionFailure: () => void; onSubscriptionFailure: () => void;
}; };
const SUBSCRIPTION_TIMEOUT = 3000;
/**
* Makes a single stats request and attempts a subscrition to VegaTime
* to determine whether or not a node is suitable for use
*/
export const requestNode = ( export const requestNode = (
url: string, url: string,
{ {
@ -21,6 +27,7 @@ export const requestNode = (
onSubscriptionFailure, onSubscriptionFailure,
}: Callbacks }: Callbacks
) => { ) => {
// check url is a valid url
try { try {
new URL(url); new URL(url);
} catch (err) { } catch (err) {
@ -29,8 +36,11 @@ export const requestNode = (
return; return;
} }
let subscriptionSucceeded = false;
const client = createClient(url); const client = createClient(url);
// make a query for block height
client client
.query<StatisticsQuery>({ .query<StatisticsQuery>({
query: StatisticsDocument, query: StatisticsDocument,
@ -42,6 +52,7 @@ export const requestNode = (
onStatsFailure(); onStatsFailure();
}); });
// start a subscription for VegaTime and await the first message
const subscription = client const subscription = client
.subscribe<BlockTimeSubscription>({ .subscribe<BlockTimeSubscription>({
query: BlockTimeDocument, query: BlockTimeDocument,
@ -49,6 +60,7 @@ export const requestNode = (
}) })
.subscribe({ .subscribe({
next() { next() {
subscriptionSucceeded = true;
onSubscriptionSuccess(); onSubscriptionSuccess();
subscription.unsubscribe(); subscription.unsubscribe();
}, },
@ -58,5 +70,14 @@ export const requestNode = (
}, },
}); });
// start a timeout, if the above subscription doesn't yield any messages
// before the timeout has completed consider it failed
setTimeout(() => {
if (!subscriptionSucceeded) {
onSubscriptionFailure();
subscription.unsubscribe();
}
}, SUBSCRIPTION_TIMEOUT);
return client; return client;
}; };