Skip to content

Commit

Permalink
Configure rainbowkit to look better
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Sep 9, 2024
1 parent 7233032 commit ab1fa04
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 5 deletions.
69 changes: 69 additions & 0 deletions apps/web/src/components/AddressAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { cn } from "@repo/ui/lib/utils";
import makeBlockie from "ethereum-blockies-base64";
import Image from "next/image";
import { useMemo } from "react";
import { isAddress } from "viem";
import { normalize } from "viem/ens";
import { useEnsAddress, useEnsAvatar, useEnsName } from "wagmi";
import { mainnetConfig } from "../utils/wallet";

const transparentPixel =
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";

function AddressAvatar({
addressOrEns,
size,
}: { addressOrEns: string; size: number }) {
const isEnsName = addressOrEns.endsWith(".eth");
const isAddr = isAddress(addressOrEns);

// If addressOrEns is an address, get the ens name
const { data: ensName } = useEnsName({
address: isAddr ? addressOrEns : undefined,
chainId: 1,
query: { enabled: !isEnsName && isAddr },
config: mainnetConfig,
});

let normalizedName: string | undefined;
try {
normalizedName = isEnsName
? normalize(addressOrEns)
: ensName?.toLowerCase();
} catch (error) {}

// If we did not have the address and normalizedName is an ens name, get the ens address
const { data: ensAddress } = useEnsAddress({
name: normalizedName,
chainId: 1,
query: { enabled: !!normalizedName && !isAddr },
config: mainnetConfig,
});

// If normalizedName is an ens name, get the ens avatar
const { data: ensAvatar, isLoading } = useEnsAvatar({
name: normalizedName,
chainId: 1,
query: { enabled: !!normalizedName },
config: mainnetConfig,
});
const address = ensAddress ?? addressOrEns;
const src = useMemo(() => {
if (isLoading) return transparentPixel;
if (ensAvatar) return ensAvatar;
if (!isLoading && isAddress(address)) return makeBlockie(address);
return transparentPixel;
}, [ensAvatar, address, isLoading]);

return (
<Image
src={src}
alt=""
width={size}
height={size}
className={cn("rounded bg-gray-600")}
/>
);
}

export default AddressAvatar;
13 changes: 12 additions & 1 deletion apps/web/src/components/ConnectButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
"use client";

import { ConnectButton as RainbowConnectButton } from "@rainbow-me/rainbowkit";
import { useConnectModal } from "@rainbow-me/rainbowkit";
import { Button } from "@repo/ui/components/ui/button";
import { useAccount } from "wagmi";

export function ConnectButton() {
return <RainbowConnectButton showBalance={false} />;
const { address } = useAccount();
const { openConnectModal } = useConnectModal();
return address ? (
<RainbowConnectButton showBalance={false} />
) : (
<Button onClick={openConnectModal}>Connect Wallet</Button>
);
}
5 changes: 3 additions & 2 deletions apps/web/src/components/VotingCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const VotingCard = ({
) : (
<>
<h4 className="text-xl mb-6 text-accent">
Cast Your Vote ({votedProjects.length} / {maxVotedProjects})
Cast Your Vote ({votedProjects.length} / {maxVotedProjects}{" "}
projects)
</h4>
{Object.entries(votes).map(([project, voteCount]) => (
<div
Expand Down Expand Up @@ -98,7 +99,7 @@ const VotingCard = ({
disabled={votedProjects.length < 1 || !address}
className="w-full py-2 rounded-lg mt-4 font-bold"
>
{address ? "Vote" : "Connect Wallet"}
{address ? "Vote" : "Wallet not connected"}
</Button>
</CardFooter>
</Card>
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const SITE_NAME = "Councilhaus";
export const SITE_DESCRIPTION =
"Democratically allocate a budget across projects";
15 changes: 14 additions & 1 deletion apps/web/src/context/WalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"use client";

import { RainbowKitProvider, darkTheme } from "@rainbow-me/rainbowkit";
import {
type AvatarComponent,
RainbowKitProvider,
darkTheme,
} from "@rainbow-me/rainbowkit";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import type { PropsWithChildren } from "react";
import colors from "tailwindcss/colors";
import { type State, WagmiProvider } from "wagmi";
import AddressAvatar from "../components/AddressAvatar";
import { WALLETCONNECT_CONFIG } from "../utils/wallet";

interface Props extends PropsWithChildren {
Expand All @@ -13,6 +18,13 @@ interface Props extends PropsWithChildren {

const queryClient = new QueryClient();

const CustomAvatar: AvatarComponent = ({
address,
size,
}: { address: string; size: number }) => {
return <AddressAvatar addressOrEns={address} size={size} />;
};

export function WalletProvider(props: Props) {
return (
<WagmiProvider
Expand All @@ -22,6 +34,7 @@ export function WalletProvider(props: Props) {
<QueryClientProvider client={queryClient}>
<RainbowKitProvider
modalSize="compact"
avatar={CustomAvatar}
theme={darkTheme({
accentColor: colors.yellow[500],
accentColorForeground: colors.gray[900],
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/utils/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use client";

import "@rainbow-me/rainbowkit/styles.css";
import { getDefaultConfig } from "@rainbow-me/rainbowkit";
import { createPublicClient } from "viem";
Expand All @@ -14,7 +15,9 @@ if (!WALLETCONNECT_PROJECT_ID) {
);
}

export const WALLETCONNECT_CONFIG = getDefaultConfig({
type RainbowKitConfig = ReturnType<typeof getDefaultConfig>;

export const WALLETCONNECT_CONFIG: RainbowKitConfig = getDefaultConfig({
appName: SITE_NAME,
projectId: WALLETCONNECT_PROJECT_ID || "dummy",
chains: [optimism],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@rainbow-me/rainbowkit": "^2.1.5",
"@tanstack/react-query": "^5.55.4",
"ethereum-blockies-base64": "^1.0.2",
"viem": "2.x",
"wagmi": "^2.12.8"
}
Expand Down

0 comments on commit ab1fa04

Please sign in to comment.