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

Michael/agora 1686 copy addresses when clicking on 0x5f4 shorten addresses UI #246

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
1 change: 1 addition & 0 deletions src/components/Delegates/DelegateCard/DelegateCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function DelegateCard({ delegate }: { delegate: Delegate }) {
address={delegate.address}
citizen={delegate.citizen}
votingPower={delegate.votingPower}
copyable={true}
/>
</VStack>

Expand Down
11 changes: 9 additions & 2 deletions src/components/Delegates/DelegateCard/DelegateProfileImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import ENSAvatar from "../../shared/ENSAvatar";
import { HStack, VStack } from "@/components/Layout/Stack";
import HumanAddress from "../../shared/HumanAddress";
import HumanAddress from "@/components/shared/HumanAddress";
import CopyableHumanAddress from "../../shared/CopyableHumanAddress";
import { useEnsName } from "wagmi";
import { formatNumber } from "@/lib/tokenUtils";
import { useMemo } from "react";
Expand All @@ -18,10 +19,12 @@ export function DelegateProfileImage({
address,
votingPower,
citizen,
copyable = false,
}: {
address: string;
votingPower: string;
citizen?: boolean;
copyable?: boolean;
}) {
const { refetchDelegate, setRefetchDelegate } = useConnectButtonContext();
const { token } = Tenant.current();
Expand Down Expand Up @@ -77,7 +80,11 @@ export function DelegateProfileImage({

<VStack>
<div className={styles.address}>
<HumanAddress address={address} />
{copyable ? (
<CopyableHumanAddress address={address} />
) : (
<HumanAddress address={address} />
)}
</div>
<div className={styles.token}>
{formattedNumber} {token.symbol}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
.address {
font-size: $font-size-base;
font-weight: $font-weight-semibold;

:hover {
opacity: 0.9;
}
}

.token {
Expand Down
19 changes: 15 additions & 4 deletions src/components/Layout/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Props = {
| "items-start"
| "items-end"
| "items-baseline";
onClick?: (e?: any) => void;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I found myself wanting to add onClick to an HStack and was about to wrap it in a div so I could add an onClick to the div wrapping it. But then I figured the HStack is just a div itself... theres no reason it shouldn't just accept an onClick prop. So I'm adding it here... if we don't like this I can take it out!

Copy link
Contributor

@yitongzhang yitongzhang Apr 22, 2024

Choose a reason for hiding this comment

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

lol i've been here a thousand times. i've been out of the code base long enough that i've forgotten we don't have a click handler on stack. guess it's to keep it to a positioning only thing

(please don't mind anything i say on this tho, i'm just a tourist in eng lol)

children: ReactNode;
};

Expand All @@ -45,7 +46,8 @@ function Stack({
}

export function VStack(props: Props) {
const { className, gap, alignItems, justifyContent, children } = props;
const { className, gap, alignItems, justifyContent, children, onClick } =
props;

const classes = [
"flex",
Expand All @@ -58,11 +60,16 @@ export function VStack(props: Props) {
.filter(Boolean)
.join(" ");

return <div className={classes}>{children}</div>;
return (
<div className={classes} onClick={onClick}>
{children}
</div>
);
}

export function HStack(props: Props) {
const { className, gap, alignItems, justifyContent, children } = props;
const { className, gap, alignItems, justifyContent, children, onClick } =
props;

const classes = [
"flex",
Expand All @@ -75,5 +82,9 @@ export function HStack(props: Props) {
.filter(Boolean)
.join(" ");

return <div className={classes}>{children}</div>;
return (
<div className={classes} onClick={onClick}>
{children}
</div>
);
}
54 changes: 54 additions & 0 deletions src/components/shared/CopyableHumanAddress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use client";

import React, { useState, useEffect } from "react";
import Image from "next/image";
import ENSName from "./ENSName"; // adjust the import path as per your project structure
import { HStack } from "@/components/Layout/Stack";
import { CheckCircleIcon } from "@heroicons/react/20/solid";
import { icons } from "@/icons/icons";

// This component will display the ENS name for a given address
// It will also be copyable, meaning that when clicked, it will copy the address to the clipboard
// It will also show a checkmark when the address has been copied
function CopyableHumanAddress({ address }: { address: string }) {
const [isInCopiedState, setIsInCopiedState] = useState<boolean>(false);

useEffect(() => {
let id: NodeJS.Timeout | number | null = null;
if (isInCopiedState) {
id = setTimeout(() => {
setIsInCopiedState(false);
}, 750);
}
return () => {
if (id) clearTimeout(id);
};
}, [isInCopiedState]);

return (
<HStack
alignItems="items-center"
className="cursor-pointer group"
gap={1}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
navigator.clipboard.writeText(address);
setIsInCopiedState(true);
}}
>
<ENSName address={address} />
{isInCopiedState ? (
<CheckCircleIcon className="text-green-600 w-4 h-4" />
) : (
<Image
src={icons.clipboard}
alt={"clipboard icon"}
className="w-4 h-4 hidden group-hover:block group-hover:opacity-90"
/>
)}
</HStack>
);
}

export default CopyableHumanAddress;
3 changes: 3 additions & 0 deletions src/icons/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/icons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import shareCard from "./shareCard.svg";
import scroll from "./scroll.svg";
import github from "./github.svg";
import world from "./world.svg";
import clipboard from "./clipboard.svg";

export const icons = {
badge,
Expand Down Expand Up @@ -58,4 +59,5 @@ export const icons = {
scroll,
github,
world,
clipboard,
};
Loading