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

detect if user has flow network selected and don't prompt to add network #647

Merged
merged 1 commit into from Mar 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/components/addNetworkButton.tsx
@@ -1,12 +1,34 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import {
Button,
ButtonLink,
} from '@site/src/ui/design-system/src/lib/Components/Button/index';

export const AddNetworkButton = () => {
const targetChainIds = [747, 646, 545]; // Your target chain IDs
const [isNetworkAdded, setIsNetworkAdded] = useState<boolean>(false);
const [chainId, setChainId] = useState<string>(''); // Flow Previewnet

const getChainId = async () => {
if (!window?.ethereum) return;
const chainId = await window?.ethereum.request({ method: 'eth_chainId' });
setChainId(parseInt(chainId, 16)); // Convert chainId from hex to decimal
};

useEffect(() => {
getChainId();
}, []);

const hasEthereum = window?.ethereum !== undefined;

useEffect(() => {
if (targetChainIds.includes(chainId)) {
setIsNetworkAdded(true);
} else {
setIsNetworkAdded(false);
}
}, [chainId]);

const addFlowNetwork = async () => {
try {
// Define your network details here
Expand Down Expand Up @@ -36,16 +58,21 @@ export const AddNetworkButton = () => {

// eslint-disable-next-line @typescript-eslint/no-misused-promises
return hasEthereum ? (
<Button className='my-5 ' variant='secondary' onClick={addFlowNetwork}>
Add Flow Network
<Button
className="my-5 "
disabled={isNetworkAdded}
variant="secondary"
onClick={addFlowNetwork}
>
{isNetworkAdded ? 'Flow Network Added!' : 'Add Flow Network'}
</Button>
) : (
<ButtonLink
className='my-5'
variant='primary'
href='https://metamask.io/download/'
className="my-5"
variant="primary"
href="https://metamask.io/download/"
>
Install MetaMask
</ButtonLink>
)
);
};
3 changes: 3 additions & 0 deletions src/ui/design-system/src/lib/Components/Button/index.tsx
Expand Up @@ -15,11 +15,13 @@ const VARIANTS = {
"dark:bg-white dark:text-black",
"dark:hover:border-white dark:hover:bg-black dark:hover:text-white",
"dark:active:border-gray-500 dark:active:bg-black dark:active:text-gray-500",
"disabled:opacity-50 disabled:bg-gray-200 disabled:cursor-not-allowed",
],
"primary-no-darkmode": [
"bg-black text-white border-transparent",
"hover:border-black hover:bg-white hover:text-black",
"active:border-gray-500 active:bg-white active:text-gray-500",
"disabled:opacity-50 disabled:bg-gray-200 disabled:cursor-not-allowed",
],
secondary: [
"text-primary-blue border-primary-blue",
Expand All @@ -28,6 +30,7 @@ const VARIANTS = {
"dark:bg-black dark:text-blue-dark dark:border-blue-dark",
"dark:hover:bg-blue-dark dark:hover:text-white",
"dark:active:bg-blue-hover-dark dark:active:text-white dark:active:border-blue-hover-dark",
"disabled:opacity-50 disabled:cursor-not-allowed",
],
}

Expand Down