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: show connected wallet info #22

Merged
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
17 changes: 17 additions & 0 deletions packages/nextjs/components/BuilderStateInBatch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { showBuilderNotification } from "./builderState/BuilderNotification";
import { BuilderStateIcon } from "./builderState/BuilderStateIcon";
import { useBuilderStateConfig } from "~~/hooks/useBuilderState";

export const BuilderStateInBatch = () => {
const { state, config } = useBuilderStateConfig();

return (
<div className="flex items-center">
<BuilderStateIcon
type={state}
tooltip={config.tooltip}
onClick={() => showBuilderNotification(config.notification)}
/>
</div>
);
};
2 changes: 2 additions & 0 deletions packages/nextjs/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useCallback, useRef, useState } from "react";
import Image from "next/image";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { BuilderStateInBatch } from "./BuilderStateInBatch";
import { Bars3Icon, BugAntIcon } from "@heroicons/react/24/outline";
import { FaucetButton, RainbowKitCustomConnectButton } from "~~/components/scaffold-eth";
import { useOutsideClick } from "~~/hooks/scaffold-eth";
Expand Down Expand Up @@ -102,6 +103,7 @@ export const Header = () => {
</ul>
</div>
<div className="navbar-end flex-grow mr-4">
<BuilderStateInBatch />
<RainbowKitCustomConnectButton />
<FaucetButton />
</div>
Expand Down
30 changes: 30 additions & 0 deletions packages/nextjs/components/builderState/BuilderNotification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { notification } from "~~/utils/scaffold-eth";

type NotificationLink = {
text: string;
url: string;
};

type NotificationConfig = {
message: string;
link?: NotificationLink;
type?: "info" | "success" | "warning";
};

export const showBuilderNotification = ({ message, link, type = "info" }: NotificationConfig) => {
const content = (
<div>
{message}
{link && (
<>
{" "}
<a href={link.url} target="_blank" className="underline">
{link.text}
</a>
</>
)}
</div>
);

notification[type](content);
};
28 changes: 28 additions & 0 deletions packages/nextjs/components/builderState/BuilderStateIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { WalletIcon } from "@heroicons/react/24/outline";
import { CheckCircleIcon, UserGroupIcon } from "@heroicons/react/24/solid";

type BuilderStateIconProps = {
type: "disconnected" | "notAllowlisted" | "notCheckedIn" | "checkedIn" | "wrongNetwork";
onClick: () => void;
tooltip: string;
};

export const BuilderStateIcon = ({ type, onClick, tooltip }: BuilderStateIconProps) => {
const baseStyles = "h-6 w-6 cursor-pointer";
const warningStyles = `${baseStyles} text-warning animate-pulse`;
const walletStyles = `${warningStyles} mr-2`;
Comment on lines +11 to +13
Copy link
Member

Choose a reason for hiding this comment

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

Tailwind purists will say not to do it this way, but I think this kind of "inheritance" pattern is easy to read, understand, and maintain in this case!


const icons = {
notAllowlisted: <UserGroupIcon className={warningStyles} />,
notCheckedIn: <CheckCircleIcon className={warningStyles} />,
checkedIn: <CheckCircleIcon className={`${baseStyles} text-success`} />,
disconnected: <WalletIcon className={walletStyles} />,
wrongNetwork: <WalletIcon className={walletStyles} />,
Comment on lines +16 to +20
Copy link
Member

Choose a reason for hiding this comment

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

image

};

return (
<div className="tooltip tooltip-bottom" data-tip={tooltip} onClick={onClick}>
{icons[type]}
</div>
);
};
79 changes: 79 additions & 0 deletions packages/nextjs/hooks/useBuilderState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { useScaffoldReadContract } from "./scaffold-eth/useScaffoldReadContract";
import { Address } from "viem";
import { useAccount, useChainId } from "wagmi";

type BuilderState = "disconnected" | "notAllowlisted" | "notCheckedIn" | "checkedIn" | "wrongNetwork";

const stateConfigs = {
disconnected: {
tooltip: "Please connect your wallet",
notification: {
message: "Please connect your wallet to check your builder status",
type: "warning",
},
},
notAllowlisted: {
tooltip: "Connected wallet is not allowlisted. Please apply to the next batch.",
notification: {
message: "Connected wallet is not allowlisted. Please ",
link: { text: "apply to the next batch", url: "https://buidlguidl.com/batches" },
},
},
notCheckedIn: {
tooltip: "You need to check in to start building",
notification: {
message: "You need to check in to start building. Follow instructions in ",
link: { text: "this GitHub issue", url: "https://github.com/BuidlGuidl/batch12.buidlguidl.com/issues/12" },
},
},
checkedIn: {
tooltip: "You are checked in and ready to build! Check ",
notification: {
message: "You are checked in and ready to build! Check ",
link: { text: "GitHub issues", url: "https://github.com/BuidlGuidl/batch12.buidlguidl.com/issues/12" },
type: "success" as const,
},
},
wrongNetwork: {
tooltip: "Switch to Optimism network",
notification: {
message: "Please switch to Optimism network",
type: "warning" as const,
},
},
} as const;

export const useBuilderState = (address?: Address) => {
const { data: isAllowListed } = useScaffoldReadContract({
contractName: "BatchRegistry",
functionName: "allowList",
args: [address],
});

const { data: contractAddress } = useScaffoldReadContract({
contractName: "BatchRegistry",
functionName: "yourContractAddress",
args: [address],
});

const isCheckedIn = contractAddress && contractAddress !== "0x0000000000000000000000000000000000000000";

return { isAllowListed, isCheckedIn };
};

export const useBuilderStateConfig = () => {
const { address } = useAccount();
const chainId = useChainId();
const { isAllowListed, isCheckedIn } = useBuilderState(address);

const getState = (): BuilderState => {
if (!address) return "disconnected";
if (chainId !== 10) return "wrongNetwork";
if (!isAllowListed) return "notAllowlisted";
if (!isCheckedIn) return "notCheckedIn";
return "checkedIn";
};

const state = getState();
return { state, config: stateConfigs[state] };
};
Loading