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

display network notices #94

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ChainConnection from 'components/ChainConnection';
import { INTER_LOGO } from 'assets/assets';
import { MdBarChart } from 'react-icons/md';
import { FiExternalLink } from 'react-icons/fi';
import NoticeBanner from 'components/NoticeBanner';

import 'styles/globals.css';

Expand All @@ -26,6 +27,7 @@ const App = () => {
}}
></ToastContainer>
<motion.div className="flex flex-col min-h-screen">
<NoticeBanner />
<motion.div className="min-w-screen container p-4 mx-auto flex justify-between items-center">
<a href="https://inter.trade/" target="inter.trade">
<img
Expand Down
73 changes: 0 additions & 73 deletions src/components/AnnouncementBanner.tsx

This file was deleted.

93 changes: 52 additions & 41 deletions src/components/ChainConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
governedParamsIndexAtom,
metricsIndexAtom,
chainConnectionAtom,
networkConfigAtom,
termsIndexAgreedUponAtom,
smartWalletProvisionedAtom,
provisionToastIdAtom,
ChainConnection as ChainConnectionStore,
networkConfigPAtom,
} from 'store/app';
import {
watchContract,
Expand All @@ -30,12 +30,12 @@ import TermsDialog, { currentTermsIndex } from './TermsDialog';
import clsx from 'clsx';
import { makeAgoricChainStorageWatcher } from '@agoric/rpc';
import { sample } from 'lodash-es';
import { loadNetworkConfig } from 'utils/networkConfig';
import ProvisionSmartWalletDialog from './ProvisionSmartWalletDialog';
import { querySwingsetParams } from 'utils/swingsetParams';

import 'react-toastify/dist/ReactToastify.css';
import 'styles/globals.css';
import { querySwingsetParams } from 'utils/swingsetParams';
import { loadable } from 'jotai/utils';

const autoCloseDelayMs = 7000;

Expand Down Expand Up @@ -81,13 +81,12 @@ const ChainConnection = () => {
const [isSmartWalletProvisioned, setSmartWalletProvisioned] = useAtom(
smartWalletProvisionedAtom
);
const networkConfig = useAtomValue(networkConfigAtom);
const termsAgreed = useAtomValue(termsIndexAgreedUponAtom);
const [isTermsDialogOpen, setIsTermsDialogOpen] = useState(false);
const [isProvisionDialogOpen, setIsProvisionDialogOpen] = useState(false);
const { smartWalletFee, error: smartWalletFeeError } =
useSmartWalletFeeQuery(chainConnection);

const networkConfig = useAtomValue(loadable(networkConfigPAtom));
const areLatestTermsAgreed = termsAgreed === currentTermsIndex;

const handleTermsDialogClose = () => {
Expand Down Expand Up @@ -169,46 +168,58 @@ const ChainConnection = () => {
setIsTermsDialogOpen(true);
return;
}

let connection;
setConnectionInProgress(true);
try {
const { rpcAddrs, chainName } = await loadNetworkConfig(
networkConfig.url
);
const rpc = sample(rpcAddrs);
if (!rpc) {
throw new Error(Errors.networkConfig);
};

useEffect(() => {
const connect = async () => {
if (networkConfig.state === 'loading') {
return;
}
const watcher = makeAgoricChainStorageWatcher(rpc, chainName, e => {
console.error(e);
throw e;
});
connection = await makeAgoricWalletConnection(watcher);
setChainConnection({
...connection,
watcher,
chainId: chainName,
});
} catch (e: any) {
switch (e.message) {
case Errors.enableKeplr:
toast.error('Enable the connection in Keplr to continue.', {
hideProgressBar: false,
autoClose: autoCloseDelayMs,
});
break;
case Errors.networkConfig:
toast.error('Network not found.');
break;
default:
toast.error('Error connecting to network:' + e.message);
break;
try {
if (networkConfig.state === 'hasError') {
throw new Error(Errors.networkConfig);
}
const config = networkConfig.data;
const rpc = sample(config.rpcAddrs);
if (!rpc) {
throw new Error(Errors.networkConfig);
}
const chainId = config.chainName;
const watcher = makeAgoricChainStorageWatcher(rpc, chainId, e => {
console.error(e);
throw e;
});
const connection = await makeAgoricWalletConnection(watcher);
setChainConnection({
...connection,
watcher,
chainId,
});
} catch (e: any) {
switch (e.message) {
case Errors.enableKeplr:
toast.error('Enable the connection in Keplr to continue.', {
hideProgressBar: false,
autoClose: autoCloseDelayMs,
});
break;
case Errors.networkConfig:
toast.error('Network not found.');
break;
default:
toast.error('Error connecting to network:' + e.message);
break;
}
} finally {
setConnectionInProgress(false);
}
} finally {
setConnectionInProgress(false);
};

if (connectionInProgress) {
connect();
}
};
}, [connectionInProgress, networkConfig, setChainConnection]);

const status = (() => {
if (connectionInProgress) {
Expand Down
56 changes: 56 additions & 0 deletions src/components/NoticeBanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { FiX } from 'react-icons/fi';
import { GrAnnounce } from 'react-icons/gr';
import { motion, AnimatePresence } from 'framer-motion';
import { useState } from 'react';
import { activeNotices } from 'utils/networkConfig';
import { useAtomValue } from 'jotai';
import { networkConfigPAtom } from 'store/app';
import { loadable } from 'jotai/utils';

const NoticeBanner = () => {
const [isDismissed, setIsDismissed] = useState(false);
const config = useAtomValue(loadable(networkConfigPAtom));
const bannerContent =
config.state === 'hasData' && activeNotices(config.data).join(' • ');
const isVisible =
!isDismissed && bannerContent && bannerContent.trim().length;

return (
<AnimatePresence initial={false}>
{isVisible && (
<motion.div
initial="open"
animate="open"
exit="collapsed"
variants={{
open: { height: 'auto' },
collapsed: { height: 0 },
}}
className="bg-yellow-400 overflow-hidden"
>
<motion.div className="mx-auto max-w-7xl py-3 px-3 sm:px-6 lg:px-8">
<motion.div className="flex flex-wrap items-center justify-between">
<motion.div className="flex w-0 flex-1 items-center">
<span className="flex rounded-lgp-2">
<GrAnnounce className="h-6 w-6" aria-hidden="true" />
</span>
<p className="ml-3 font-medium text-black">{bannerContent}</p>
</motion.div>
<motion.div className="order-2 flex-shrink-0 sm:order-3 sm:ml-3">
<button
onClick={() => setIsDismissed(true)}
type="button"
className="-mr-1 flex rounded-md p-2 hover:bg-black hover:bg-opacity-10 focus:outline-none focus:ring-2 focus:ring-white sm:-mr-2"
>
<FiX className="h-6 w-6" />
</button>
</motion.div>
</motion.div>
</motion.div>
</motion.div>
)}
</AnimatePresence>
);
};

export default NoticeBanner;
10 changes: 5 additions & 5 deletions src/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { makeAgoricWalletConnection } from '@agoric/web-components';
import type { Brand, DisplayInfo, Amount } from '@agoric/ertp/src/types';
import type { Id as ToastId } from 'react-toastify';
import { ChainStorageWatcher } from '@agoric/rpc';
import { loadNetworkConfig } from 'utils/networkConfig';

type Ratio = ReturnType<typeof makeRatio>;

Expand All @@ -20,11 +21,6 @@ export type BrandInfo = DisplayInfo<'nat'> & {
petname: string;
};

export const bannerIndexDismissedAtom = atomWithStorage(
'banner-index-dismissed',
-1
);

export const brandToInfoAtom = mapAtom<Brand, BrandInfo>();

export const chainConnectionAtom = atom<ChainConnection | null>(null);
Expand All @@ -38,6 +34,10 @@ export const networkConfigAtom = atomWithStorage(
networkConfigs.mainnet
);

export const networkConfigPAtom = atom(async get =>
loadNetworkConfig(get(networkConfigAtom).url)
);

export const termsIndexAgreedUponAtom = atomWithStorage('terms-agreed', -1);

/** A map of anchor brand petnames to their instance ids. */
Expand Down
32 changes: 31 additions & 1 deletion src/utils/networkConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
type MinimalNetworkConfig = { rpcAddrs: string[]; chainName: string };
type NetworkNotice = {
start: string;
// In the future this might be optional to indicate that it's user-dismissable.
// In that case the client would need some persistent state, perhaps keyed by `message`.
end: string;
message: string;
};

export type MinimalNetworkConfig = {
rpcAddrs: string[];
chainName: string;
notices?: NetworkNotice[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to have separate notices for PSM and Vaults?

Copy link
Member Author

Choose a reason for hiding this comment

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

if we do I think that has to go somewhere else. this is config for the network. appropriate for things like pending upgrades.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Makes sense

};

export const loadNetworkConfig = (url: string): Promise<MinimalNetworkConfig> =>
fetch(url).then(res => res.json());

export const activeNotices = (
config: Pick<MinimalNetworkConfig, 'notices'>
) => {
const { notices } = config;
if (!notices) return [];

const now = Date.now();
const active = notices.filter(n => {
const startD = Date.parse(n.start);
if (startD > now) {
return false;
}
const endD = Date.parse(n.end);
return startD < endD;
});
return active.map(n => n.message);
};
40 changes: 40 additions & 0 deletions test/utils/networkConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect, it, describe } from 'vitest';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yay tests


import { activeNotices } from '../../src/utils/networkConfig';

describe('activeNotices', () => {
it('handles empty', () => {
const notices = [];
expect(activeNotices({ notices })).toEqual([]);
});
it('handles future', () => {
const notices = [
{
start: '2030-01-01',
end: '2040-01-01',
message: 'hello from the future',
},
];
expect(activeNotices({ notices })).toEqual([]);
});
it('handles past', () => {
const notices = [
{
start: '2000-01-01',
end: '2000-01-01',
message: 'hello from the past',
},
];
expect(activeNotices({ notices })).toEqual([]);
});
it('handles started', () => {
const notices = [
{
start: '2020-01-01',
end: '2040-01-01',
message: 'hello now',
},
];
expect(activeNotices({ notices })).toEqual(['hello now']);
});
});
Loading