Skip to content

Commit

Permalink
beneficiary (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
wraeth-eth authored Oct 30, 2023
1 parent 91cb5de commit 939a6c1
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 56 deletions.
38 changes: 27 additions & 11 deletions src/components/EthereumAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Skeleton } from './ui/Skeleton'
export type EthereumAddressProps = {
className?: string
address: `0x${string}` | undefined
showEnsIcon?: boolean
showEnsLoading?: boolean
ensDisabled?: boolean
truncateTo?: number
Expand All @@ -19,6 +20,7 @@ export type EthereumAddressProps = {
export const EthereumAddress: React.FC<EthereumAddressProps> = ({
className,
address,
showEnsIcon,
ensDisabled = false,
showEnsLoading = false,
truncateTo,
Expand All @@ -35,28 +37,42 @@ export const EthereumAddress: React.FC<EthereumAddressProps> = ({
return truncateEthAddress({ address, truncateTo })
}, [address, ensDisabled, ensName, truncateTo])

const ensAvatarIcon = useMemo(
() =>
address ? (
<Image
src={ensAvatarUrlForAddress(address, { size: 80 })}
fill
className="rounded-full"
alt={`Avatar for ${ensName ?? address}`}
loading="lazy"
/>
) : null,
[address, ensName],
)

return (
<HoverCard openDelay={150}>
<HoverCardTrigger
className={twMerge(
'hover:underline',
'inline-flex items-center gap-2',
showEnsLoading && isLoading && 'animate-pulse',
className,
)}
>
{showEnsIcon && ensAvatarIcon ? (
<div className="relative h-7 w-7 rounded-full bg-gray-100">
{ensAvatarIcon}
</div>
) : null}
{formattedAddress}
</HoverCardTrigger>
<HoverCardContent className="text-start">
<HoverCardContent className="w-fit text-start">
<div className="flex items-center gap-5">
{address ? (
<Image
src={ensAvatarUrlForAddress(address, { size: 80 })}
height={40}
width={40}
className={twMerge('h-10 w-10 rounded-full')}
alt={`Avatar for ${ensName ?? address}`}
loading="lazy"
/>
{ensAvatarIcon ? (
<div className="relative h-10 w-10 rounded-full bg-gray-100">
{ensAvatarIcon}
</div>
) : (
<Skeleton className="h-10 w-10 rounded-full" />
)}
Expand Down
44 changes: 26 additions & 18 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,41 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
) => {
if (prefix || suffix) {
return (
<div className="flex items-center rounded-lg border border-gray-300 bg-white outline-2 focus-within:outline focus-within:outline-bluebs-500">
<div
className={twMerge(
'flex h-full items-center justify-center py-2 pl-3 text-base leading-6 text-gray-500',
prefixSeparator ? 'border-r border-gray-300 pr-3' : '',
)}
>
{prefix}
</div>
<div
className={twMerge(
'flex items-center rounded-lg border border-gray-300 bg-white outline-2 focus-within:outline focus-within:outline-bluebs-500',
className,
)}
>
{prefix && (
<div
className={twMerge(
'flex h-full items-center justify-center py-2 pl-3 text-base leading-6 text-gray-500',
prefixSeparator ? 'border-r border-gray-300 pr-3' : '',
)}
>
{prefix}
</div>
)}

<input
ref={ref}
className={twMerge(
'w-full bg-transparent py-2 pl-2 pr-3 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none',
className,
)}
{...props}
/>

<div
className={twMerge(
'flex h-full items-center justify-center py-2 pr-3 text-base leading-6 text-gray-500',
suffixSeparator ? 'border-l border-gray-300 pl-3' : '',
)}
>
{suffix}
</div>
{suffix && (
<div
className={twMerge(
'flex h-full items-center justify-center py-2 pr-3 text-base leading-6 text-gray-500',
suffixSeparator ? 'border-l border-gray-300 pl-3' : '',
)}
>
{suffix}
</div>
)}
</div>
)
}
Expand Down
132 changes: 125 additions & 7 deletions src/components/Project/components/ProjectPayForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { EthereumAddress } from '@/components/EthereumAddress'
import { Input } from '@/components/Input'
import { Link } from '@/components/Link'
import { Spinner } from '@/components/Spinner'
import { EthereumIconFilled } from '@/components/icon/EthereumIconFilled'
import { Button } from '@/components/ui/Button'
import {
Expand All @@ -10,25 +13,35 @@ import {
FormLabel,
FormMessage,
} from '@/components/ui/Form'
import { useIpfsFilePicker } from '@/hooks/useIpfsFilePicker/useIpfsFilePicker'
import { useJbProject } from '@/hooks/useJbProject'
import { publicClient } from '@/lib/viem/publicClient'
import {
ChevronDownIcon,
EnvelopeIcon,
PencilSquareIcon,
PhotoIcon,
QuestionMarkCircleIcon,
XCircleIcon,
} from '@heroicons/react/24/outline'
import { zodResolver } from '@hookform/resolvers/zod'
import { formatEther } from 'juice-hooks'
import { PropsWithChildren, useCallback, useMemo, useState } from 'react'
import Image from 'next/image'
import {
ChangeEventHandler,
PropsWithChildren,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react'
import { useForm } from 'react-hook-form'
import { twMerge } from 'tailwind-merge'
import { parseEther } from 'viem'
import { isAddress, parseEther } from 'viem'
import { useAccount, useEnsName } from 'wagmi'
import { z } from 'zod'
import { useProjectPay } from '../providers/ProjectPayContext'
import { useIpfsFilePicker } from '@/hooks/useIpfsFilePicker/useIpfsFilePicker'
import { Link } from '@/components/Link'
import Image from 'next/image'

const WEI = 1e-18

Expand All @@ -37,7 +50,13 @@ const formSchema = z.object({
.number()
.min(WEI, 'Payment amount must be greater than 1e-18 (1 wei)'),
// TODO: make more robust for eth addresses / ENS
beneficiary: z.string().min(2, 'Beneficiary must be at least 2 characters'),
beneficiary: z
.string()
.optional()
.refine(value => {
if (!value) return true
return isAddress(value)
}, 'Invalid wallet address'),
email: z.string().email('Invalid email address').optional(),
message: z.string().optional(),
})
Expand Down Expand Up @@ -121,7 +140,7 @@ export const ProjectPayForm: React.FC<ProjectPayFormProps> = ({
className="mt-6"
label="NFTs and rewards will be sent to"
>
<Input {...field} />
<ProjectPayBeneficiaryInput {...field} />
</ProjectPayFormItem>
)}
/>
Expand All @@ -135,6 +154,7 @@ export const ProjectPayForm: React.FC<ProjectPayFormProps> = ({
description="Enter email to receive confirmation & updates"
>
<Input
placeholder="[email protected]"
prefix={<EnvelopeIcon className="h-5 w-5 text-gray-500" />}
suffix={
<QuestionMarkCircleIcon className="h-4 w-4 text-gray-400" />
Expand Down Expand Up @@ -338,3 +358,101 @@ const ProjectPayMessageInput: React.FC<ProjectPayMessageInputProps> = ({
</>
)
}

type ProjectPayBeneficiaryInputProps = {} & Omit<
React.InputHTMLAttributes<HTMLInputElement>,
'prefix'
>

const ProjectPayBeneficiaryInput: React.FC<ProjectPayBeneficiaryInputProps> = ({
className,
onChange: _onChange,
...props
}) => {
const { address: ownerAddress } = useAccount()

const currentAddress = useMemo(() => {
if (!props.value) return ownerAddress
if (!isAddress(props.value as string)) return ownerAddress

return props.value as `0x${string}`
}, [ownerAddress, props.value])

const { data: currentEnsFromAddress } = useEnsName({
address: currentAddress,
})
const [isEditing, setIsEditing] = useState(false)
const [isLoading, setIsLoading] = useState(false)

const inputRef = useRef<HTMLInputElement>(null)

/**
* hijacks the onChange event to check if the value is an ENS name or address
*/
const onChange: ChangeEventHandler<HTMLInputElement> = useCallback(
async e => {
const value = e.target.value
_onChange?.(e)
if (isEnsName(value)) {
setIsLoading(true)
try {
const ensAddress = await publicClient.getEnsAddress({ name: value })
if (!ensAddress) return
_onChange?.({ ...e, target: { ...e.target, value: ensAddress } })
setIsEditing(false)
} catch (e) {
console.error(e)
} finally {
setIsLoading(false)
}
} else if (isAddress(value)) {
setIsEditing(false)
}
},
[_onChange],
)

useEffect(() => {
if (!isEditing) return
inputRef.current?.focus()
}, [isEditing])

if (!isEditing) {
return (
<div className="flex h-11 justify-between gap-5 overflow-hidden rounded-lg border border-gray-200 bg-muted px-[14px] py-3 text-base leading-none">
{currentAddress ? (
<EthereumAddress showEnsIcon address={currentAddress} />
) : (
<>Payer's address</>
)}
<Button
size="child"
variant="ghost"
className="p-0"
onClick={() => setIsEditing(true)}
>
<PencilSquareIcon className="h-4 w-4" />
</Button>
</div>
)
}

return (
<>
<Input
{...props}
onChange={onChange}
ref={inputRef}
disabled={isLoading}
suffix={
isLoading ? <Spinner className="h-5 w-5 text-gray-900" /> : null
}
onBlur={() => setIsEditing(false)}
/>
</>
)
}

const isEnsName = (value: string) => {
return value.endsWith('.eth')
}
42 changes: 23 additions & 19 deletions src/components/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ type SpinnerProps = {

export const Spinner: React.FC<SpinnerProps> = ({ className }) => {
return (
<svg
className={twMerge('animate-spin', className)}
viewBox="0 0 24 24"
fill="none"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="white"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="white"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
></path>
</svg>
<div role="status">
<svg
aria-hidden="true"
className={twMerge(
'mr-2 animate-spin fill-bluebs-500 text-white',
className,
)}
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill="currentColor"
opacity="0.2"
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
)
}
2 changes: 1 addition & 1 deletion src/components/ui/HoverCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const HoverCardContent = React.forwardRef<
align={align}
sideOffset={sideOffset}
className={cn(
'z-50 w-64 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
'z-50 min-w-[256px] rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className,
)}
{...props}
Expand Down

0 comments on commit 939a6c1

Please sign in to comment.