|
| 1 | +import { |
| 2 | + constructJSONFarcasterSignatureAccountAssociationPaylod, |
| 3 | + sign, |
| 4 | + type SignResult, |
| 5 | +} from "frames.js/farcaster-v2/json-signature"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { |
| 8 | + Dialog, |
| 9 | + DialogContent, |
| 10 | + DialogFooter, |
| 11 | + DialogHeader, |
| 12 | + DialogTitle, |
| 13 | +} from "@/components/ui/dialog"; |
| 14 | +import { useAccount, useSignMessage, useSwitchChain } from "wagmi"; |
| 15 | +import { FormEvent, useCallback, useState } from "react"; |
| 16 | +import { CopyIcon, CopyCheckIcon, CopyXIcon, Loader2Icon } from "lucide-react"; |
| 17 | +import { Input } from "@/components/ui/input"; |
| 18 | +import { Label } from "@/components/ui/label"; |
| 19 | +import { useFarcasterIdentity } from "../hooks/useFarcasterIdentity"; |
| 20 | +import { optimism } from "viem/chains"; |
| 21 | +import { useToast } from "@/components/ui/use-toast"; |
| 22 | +import { useCopyToClipboard } from "../hooks/useCopyToClipboad"; |
| 23 | + |
| 24 | +type FarcasterDomainAccountAssociationDialogProps = { |
| 25 | + onClose: () => void; |
| 26 | +}; |
| 27 | + |
| 28 | +export function FarcasterDomainAccountAssociationDialog({ |
| 29 | + onClose, |
| 30 | +}: FarcasterDomainAccountAssociationDialogProps) { |
| 31 | + const copyCompact = useCopyToClipboard(); |
| 32 | + const copyJSON = useCopyToClipboard(); |
| 33 | + const account = useAccount(); |
| 34 | + const { toast } = useToast(); |
| 35 | + const farcasterSigner = useFarcasterIdentity(); |
| 36 | + const { switchChainAsync } = useSwitchChain(); |
| 37 | + const { signMessageAsync } = useSignMessage(); |
| 38 | + const [isGenerating, setIsGenerating] = useState(false); |
| 39 | + const [associationResult, setAssociationResult] = useState<SignResult | null>( |
| 40 | + null |
| 41 | + ); |
| 42 | + |
| 43 | + const handleSubmit = useCallback( |
| 44 | + async (event: FormEvent<HTMLFormElement>) => { |
| 45 | + event.preventDefault(); |
| 46 | + |
| 47 | + const data = new FormData(event.currentTarget); |
| 48 | + |
| 49 | + try { |
| 50 | + if (farcasterSigner.signer?.status !== "approved") { |
| 51 | + throw new Error("Farcaster signer is not approved"); |
| 52 | + } |
| 53 | + |
| 54 | + if (!account.address) { |
| 55 | + throw new Error("Account address is not available"); |
| 56 | + } |
| 57 | + |
| 58 | + const domain = data.get("domain"); |
| 59 | + |
| 60 | + if (typeof domain !== "string" || !domain) { |
| 61 | + throw new Error("Domain is required"); |
| 62 | + } |
| 63 | + |
| 64 | + setIsGenerating(true); |
| 65 | + |
| 66 | + await switchChainAsync({ |
| 67 | + chainId: optimism.id, |
| 68 | + }); |
| 69 | + |
| 70 | + const result = await sign({ |
| 71 | + fid: farcasterSigner.signer.fid, |
| 72 | + payload: |
| 73 | + constructJSONFarcasterSignatureAccountAssociationPaylod(domain), |
| 74 | + signer: { |
| 75 | + type: "custody", |
| 76 | + custodyAddress: account.address, |
| 77 | + }, |
| 78 | + signMessage(message) { |
| 79 | + return signMessageAsync({ |
| 80 | + message, |
| 81 | + }); |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + setAssociationResult(result); |
| 86 | + } catch (e) { |
| 87 | + console.error(e); |
| 88 | + toast({ |
| 89 | + title: "An error occurred", |
| 90 | + description: "Please check the console for more information", |
| 91 | + variant: "destructive", |
| 92 | + }); |
| 93 | + } finally { |
| 94 | + setIsGenerating(false); |
| 95 | + } |
| 96 | + }, |
| 97 | + [ |
| 98 | + account.address, |
| 99 | + farcasterSigner.signer, |
| 100 | + signMessageAsync, |
| 101 | + switchChainAsync, |
| 102 | + toast, |
| 103 | + ] |
| 104 | + ); |
| 105 | + |
| 106 | + return ( |
| 107 | + <Dialog |
| 108 | + open |
| 109 | + onOpenChange={(isOpen) => { |
| 110 | + if (!isOpen) { |
| 111 | + onClose(); |
| 112 | + } |
| 113 | + }} |
| 114 | + > |
| 115 | + <DialogContent> |
| 116 | + <DialogHeader> |
| 117 | + <DialogTitle>Domain Account Association</DialogTitle> |
| 118 | + </DialogHeader> |
| 119 | + {!associationResult && ( |
| 120 | + <form id="domain-account-association-form" onSubmit={handleSubmit}> |
| 121 | + <Label htmlFor="domain">Domain</Label> |
| 122 | + <Input |
| 123 | + id="domain" |
| 124 | + name="domain" |
| 125 | + pattern="^.+\..+$" |
| 126 | + required |
| 127 | + type="text" |
| 128 | + /> |
| 129 | + </form> |
| 130 | + )} |
| 131 | + {associationResult && ( |
| 132 | + <div className="flex flex-col gap-4"> |
| 133 | + <div> |
| 134 | + <Label htmlFor="domain-account-association-form-compact-signature"> |
| 135 | + Compact signature |
| 136 | + </Label> |
| 137 | + <div className="flex w-full items-center space-x-2"> |
| 138 | + <Input |
| 139 | + id="domain-account-association-form-compact-signature" |
| 140 | + readOnly |
| 141 | + value={associationResult.compact} |
| 142 | + /> |
| 143 | + <Button |
| 144 | + onClick={() => { |
| 145 | + copyCompact.copyToClipboard(associationResult.compact); |
| 146 | + }} |
| 147 | + size="icon" |
| 148 | + type="button" |
| 149 | + variant="secondary" |
| 150 | + > |
| 151 | + {copyCompact.copyState === "idle" && <CopyIcon size={14} />} |
| 152 | + {copyCompact.copyState === "copied" && ( |
| 153 | + <CopyCheckIcon size={14} /> |
| 154 | + )} |
| 155 | + {copyCompact.copyState === "failed" && ( |
| 156 | + <CopyXIcon size={14} /> |
| 157 | + )} |
| 158 | + </Button> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + <div> |
| 162 | + <Label htmlFor="domain-account-association-form-json-signature"> |
| 163 | + JSON |
| 164 | + </Label> |
| 165 | + <div className="relative"> |
| 166 | + <textarea |
| 167 | + className="p-2 bg-gray-100 rounded-md font-mono w-full resize-none text-sm" |
| 168 | + readOnly |
| 169 | + name="json" |
| 170 | + rows={5} |
| 171 | + id="domain-account-association-form-json-signature" |
| 172 | + value={JSON.stringify(associationResult.json, null, 2)} |
| 173 | + ></textarea> |
| 174 | + <Button |
| 175 | + className="absolute top-0 right-0 p-2" |
| 176 | + onClick={() => { |
| 177 | + copyJSON.copyToClipboard( |
| 178 | + JSON.stringify(associationResult.json, null, 2) |
| 179 | + ); |
| 180 | + }} |
| 181 | + size="icon" |
| 182 | + type="button" |
| 183 | + variant="ghost" |
| 184 | + > |
| 185 | + {copyJSON.copyState === "idle" && <CopyIcon size={14} />} |
| 186 | + {copyJSON.copyState === "copied" && ( |
| 187 | + <CopyCheckIcon size={14} /> |
| 188 | + )} |
| 189 | + {copyJSON.copyState === "failed" && <CopyXIcon size={14} />} |
| 190 | + </Button> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + </div> |
| 194 | + )} |
| 195 | + <DialogFooter> |
| 196 | + {associationResult && ( |
| 197 | + <Button |
| 198 | + onClick={() => { |
| 199 | + setAssociationResult(null); |
| 200 | + }} |
| 201 | + type="button" |
| 202 | + > |
| 203 | + Reset |
| 204 | + </Button> |
| 205 | + )} |
| 206 | + {!associationResult && ( |
| 207 | + <Button |
| 208 | + disabled={isGenerating} |
| 209 | + form="domain-account-association-form" |
| 210 | + type="submit" |
| 211 | + > |
| 212 | + {isGenerating ? ( |
| 213 | + <> |
| 214 | + <Loader2Icon className="animate-spin mr-2" /> |
| 215 | + Generating... |
| 216 | + </> |
| 217 | + ) : ( |
| 218 | + "Generate" |
| 219 | + )} |
| 220 | + </Button> |
| 221 | + )} |
| 222 | + </DialogFooter> |
| 223 | + </DialogContent> |
| 224 | + </Dialog> |
| 225 | + ); |
| 226 | +} |
0 commit comments