Skip to content

Commit

Permalink
feat: ink-701 add ink network buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
ink-victor committed Dec 26, 2024
1 parent f7b6b18 commit 35f6206
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 123 deletions.
3 changes: 3 additions & 0 deletions global-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Window {
ethereum: any;
}
125 changes: 74 additions & 51 deletions src/components/AddNetworkButton.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,88 @@
import { useEffect, useState } from "react";

import { addNetwork, isNetworkAdded, NetworkType } from "@/utils/add-network";
import { CheckIcon } from "@/icons/Check";
import { ConnectedPulse } from "@/icons/ConnectedPulse";
import {
networkParams,
NetworkType,
useNetwork,
UseNetworkResponse,
} from "@/utils/networks";

import { Button } from "./Button";

interface AddNetworkButtonProps {
network: NetworkType;
heading: string;
}

export const AddNetworkButton = ({ network }: AddNetworkButtonProps) => {
const [isAdded, setIsAdded] = useState<boolean>(false);

useEffect(() => {
const checkNetwork = async () => {
if ((window as any).ethereum) {
const added = await isNetworkAdded(network);
setIsAdded(added);
}
};
export const AddNetworkButton = ({
network,
heading,
}: AddNetworkButtonProps) => {
const { isWalletInstalled, isAdded, isSelected, addNetwork, selectNetwork } =
useNetwork(network);

checkNetwork();

if ((window as any).ethereum) {
(window as any).ethereum.on("chainChanged", checkNetwork);
}
return (
<div className="h-36">
<div className="flex flex-col gap-2">
<h3 className="nx-font-semibold nx-tracking-tight nx-text-slate-900 dark:nx-text-slate-100 nx-mt-8 nx-text-2xl relative flex gap-2 items-center">
<span>{heading}</span>
{isSelected && <ConnectedPulse className="size-6" />}
<a
href="#mainnet"
id="mainnet"
className="subheading-anchor"
aria-label="Permalink for this section"
></a>
</h3>
<AddNetworkButtonContent
isWalletInstalled={isWalletInstalled}
isAdded={isAdded}
isSelected={isSelected}
addNetwork={addNetwork}
selectNetwork={selectNetwork}
network={network}
/>
</div>
</div>
);
};
const AddNetworkButtonContent = ({
isWalletInstalled,
isAdded,
isSelected,
addNetwork,
selectNetwork,
network,
}: UseNetworkResponse & { network: NetworkType }) => {
if (!isWalletInstalled) {
return <p>No wallet connected</p>;
}

return () => {
if ((window as any).ethereum) {
(window as any).ethereum.removeListener("chainChanged", checkNetwork);
}
};
}, [network]);
if (isAdded && isSelected) {
return (
<span className="text-green-500 font-bold flex gap-1 items-center">
<span>Network added & selected.</span>
</span>
);
}

const networkName = network === "mainnet" ? "Ink Mainnet" : "Ink Sepolia";
if (isAdded) {
return (
<span className="text-green-500 font-bold flex flex-col gap-1">
<p>Network added.</p>
<p
onClick={selectNetwork}
className="underline hover:cursor-pointer hover:opacity-90"
>
Click here to select it.
</p>
</span>
);
}

return (
<div className="space-y-2 mt-4">
{isAdded ? (
<div className="flex items-center text-green-600 dark:text-green-400 font-semibold">
<svg
className="w-5 h-5 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
{networkName} Network Added
</div>
) : (
<div className="space-y-4">
<Button variant="primary" onClick={() => addNetwork(network)}>
Add {networkName} Network
</Button>
</div>
)}
</div>
<Button variant="primary" onClick={addNetwork} className="w-60 mt-2">
Add {networkParams[network].chainName}
</Button>
);
};
9 changes: 6 additions & 3 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import clsx from "clsx";
interface ButtonProps {
variant: "primary" | "secondary";
onClick?: () => void;
className?: string;
}

export const Button: React.FC<PropsWithChildren<ButtonProps>> = ({
children,
variant,
onClick,
className,
}) => {
return (
<button
className={clsx(
"font-bold py-5 px-8 inline-flex items-center transition-all rounded-full text-xl backdrop-blur-[32px]",
"font-bold py-5 px-8 inline-flex items-center justify-center transition-all rounded-full text-xl backdrop-blur-[32px]",
{
"text-magic-white bg-magic-purple shadow-[0px_3px_84px_-10px_rgba(63,107,175,0.5)]":
"text-magic-white bg-magic-purple hover:opacity-90 shadow-[0px_3px_84px_-10px_rgba(63,107,175,0.5)]":
variant === "primary",
"text-magic-purple bg-magic-semi-deep-purple/15 shadow-[0px_3px_84px_-10px_rgba(63,107,175,0.5)]":
variant === "secondary",
}
},
className
)}
onClick={onClick}
>
Expand Down
19 changes: 19 additions & 0 deletions src/icons/Check.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const CheckIcon: React.FC<{ className?: string }> = ({
className = "size-6",
}) => {
return (
<svg
className={className}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
);
};
12 changes: 12 additions & 0 deletions src/icons/ConnectedPulse.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import clsx from "clsx";

export const ConnectedPulse: React.FC<{ className?: string }> = ({
className,
}) => {
return (
<span className={clsx("relative flex h-3 w-3", className)}>
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
<span className="relative inline-flex rounded-full h-3 w-3 bg-green-500"></span>
</span>
);
};
25 changes: 11 additions & 14 deletions src/pages/general/connect-wallet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import { AddNetworkButton } from "../../components/AddNetworkButton";

## Connect Wallet

### Mainnet
<AddNetworkButton network="mainnet" />
<AddNetworkButton network="mainnet" heading="Mainnet" />

### Testnet (Sepolia)
<AddNetworkButton network="sepolia" />

---
<AddNetworkButton network="sepolia" heading="Testnet (Sepolia)" />

## Kraken Wallet

Expand All @@ -34,16 +30,17 @@ To manually add **Ink Mainnet** as a custom network, follow these steps:
4. In the new window, click **"Add a network manually"** at the bottom of the list.
5. Enter the details provided in the dialog that appears.

| Field | Information |
| -------------------------------- | -------------------------------------------------------- |
| Network Name | Ink |
| RPC Endpoint (HTTPS) (primary) | <CopyableCode code="https://rpc-gel.inkonchain.com" /> |
| Chain ID | 57073 |
| Currency Symbol | ETH |
| Block Explorer | <CopyableCode code="https://explorer.inkonchain.com" /> |
| Field | Information |
| ------------------------------ | ------------------------------------------------------- |
| Network Name | Ink |
| RPC Endpoint (HTTPS) (primary) | <CopyableCode code="https://rpc-gel.inkonchain.com" /> |
| Chain ID | 57073 |
| Currency Symbol | ETH |
| Block Explorer | <CopyableCode code="https://explorer.inkonchain.com" /> |

6. Click **"Save"**.
7. Ink Mainnet will now be available in the network selection dropdown.

---

*For a more detailed walkthrough, please refer to the [official MetaMask tutorial](https://support.metamask.io/networks-and-sidechains/managing-networks/how-to-add-a-custom-network-rpc/).*
_For a more detailed walkthrough, please refer to the [official MetaMask tutorial](https://support.metamask.io/networks-and-sidechains/managing-networks/how-to-add-a-custom-network-rpc/)._
55 changes: 0 additions & 55 deletions src/utils/add-network.ts

This file was deleted.

Loading

0 comments on commit 35f6206

Please sign in to comment.