Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PE-5031): Block network calls when no network and show banner #400

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/components/layout/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';

import { useRegistrationState } from '../../../state/contexts/RegistrationState';
Expand All @@ -8,12 +9,36 @@ import './styles.css';

function NavBar() {
const [, dispatchRegisterState] = useRegistrationState();
const [online, setOnline] = useState(navigator.onLine);

useEffect(() => {
window.addEventListener('online', () => setOnline(true));
window.addEventListener('offline', () => setOnline(false));
return () => {
window.removeEventListener('online', () => setOnline(true));
window.removeEventListener('offline', () => setOnline(false));
};
}, []);

return (
<div
className="flex flex-column"
style={{ gap: '0px', position: 'relative' }}
>
{online ? null : (
<div
className="flex flex-row center"
style={{
backgroundColor: 'var(--accent)',
padding: '10px',
}}
>
<span className="text black bold">
We can&apos;t connect to the Internet. Please check your connection
and try again.
</span>
</div>
)}
<div className="navbar" style={{ position: 'relative' }}>
<div className="flex-row flex-left" style={{ width: 'fit-content' }}>
<Link
Expand Down
5 changes: 5 additions & 0 deletions src/components/layout/Notifications/Notifications.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Sentry from '@sentry/react';
import { NoNetworkError } from '@src/utils/errors';
import { notification } from 'antd';
import { ArgsProps } from 'antd/es/notification/interface';
import { useEffect } from 'react';
Expand All @@ -24,6 +25,10 @@ export default function Notifications() {
console.debug('Error sent to sentry:', error, sentryID);
}

if (error instanceof NoNetworkError) {
return;
}

showNotification({
type: 'error',
title: error.name,
Expand Down
40 changes: 40 additions & 0 deletions src/services/ArweaveServiceController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NoNetworkError } from '@src/utils/errors';

import { ArweaveCompositeDataProvider } from './arweave/ArweaveCompositeDataProvider';

class ArweaveServiceController {
private _provider: ArweaveCompositeDataProvider;

constructor(provider: ArweaveCompositeDataProvider) {
this._provider = provider;
return new Proxy(this, this._methodInterceptor());
}

private _methodInterceptor(): ProxyHandler<ArweaveServiceController> {
return {
get: (target, prop: string | symbol, receiver: any) => {
const origMethod = (target._provider as any)[prop];
if (typeof origMethod === 'function') {
return (...args: any[]) => {
// Call the validation function
this._validateMethodAccess();

// Proceed with the original method
return origMethod.apply(target._provider, args);
};
}
return Reflect.get(target._provider, prop, receiver);
},
};
}

private _validateMethodAccess() {
// TODO: add gateway and arns service validation based on health checks

if (!navigator.onLine) {
throw new NoNetworkError('No Network Connection, please try again later');
}
}
}

export default ArweaveServiceController;
13 changes: 8 additions & 5 deletions src/state/actions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ArweaveServiceController from '@src/services/ArweaveServiceController';
import { ArweaveWalletConnector } from '@src/types';
import Arweave from 'arweave';
import { Dispatch } from 'react';
Expand Down Expand Up @@ -28,11 +29,13 @@ export async function dispatchNewGateway(
arweave: arweaveDataProvider,
});

const provider = new ArweaveCompositeDataProvider(
arweaveDataProvider,
warpDataProvider,
contractCacheProviders,
);
const provider = new ArweaveServiceController(
new ArweaveCompositeDataProvider(
arweaveDataProvider,
warpDataProvider,
contractCacheProviders,
),
) as any as ArweaveCompositeDataProvider;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't do this. Need to review more to offer a suggestion but you're force casting the type of the class to another class.

Copy link
Contributor

@dtfiedler dtfiedler Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we wrap fetch with an interceptor that checks for connection before sending an HTTP request? Also, not sure you entirely need to go down this route. I agree with the UI showing lack of connection but the way this implemented is not how/where I'd expect. I believe it should be at the HTTP client level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the service controller wrapper, will just do the UI notification for now.

const blockHeight = await provider.getCurrentBlockHeight();
dispatch({
type: 'setBlockHeight',
Expand Down
13 changes: 8 additions & 5 deletions src/state/contexts/GlobalState.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ArweaveServiceController from '@src/services/ArweaveServiceController';
import { ArConnectWalletConnector } from '@src/services/wallets';
import React, {
Dispatch,
Expand Down Expand Up @@ -48,11 +49,13 @@ const initialState: GlobalState = {
gateway: 'ar-io.dev',
blockHeight: undefined,
lastBlockUpdateTimestamp: undefined,
arweaveDataProvider: new ArweaveCompositeDataProvider(
defaultArweave,
defaultWarp,
defaultContractCache,
),
arweaveDataProvider: new ArweaveServiceController(
new ArweaveCompositeDataProvider(
defaultArweave,
defaultWarp,
defaultContractCache,
),
) as any as ArweaveCompositeDataProvider,
};

const GlobalStateContext = createContext<[GlobalState, Dispatch<GlobalAction>]>(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export class ContractInteractionError extends Error {
this.name = 'Contract Interaction Error';
}
}

export class NoNetworkError extends Error {
constructor(message: string) {
super(message);
this.name = 'No Network Detected';
}
}
Loading