|
| 1 | +import { Button, Flex, VStack } from "@chakra-ui/react" |
| 2 | +import { |
| 3 | + createQR, |
| 4 | + encodeURL, |
| 5 | + findReference, |
| 6 | + FindReferenceError, |
| 7 | + TransactionRequestURLFields, |
| 8 | + ValidateTransferError, |
| 9 | +} from "@solana/pay" |
| 10 | +import { clusterApiUrl, Connection, Keypair } from "@solana/web3.js" |
| 11 | +import { useEffect, useRef, useState } from "react" |
| 12 | +import Confirmed from "./Confirmed" |
| 13 | + |
| 14 | +interface Props { |
| 15 | + onClose: () => void |
| 16 | +} |
| 17 | + |
| 18 | +const QrModal = ({ onClose }: Props) => { |
| 19 | + const [confirmed, setConfirmed] = useState(false) |
| 20 | + const connection = new Connection(clusterApiUrl("devnet")) |
| 21 | + const qrRef = useRef<HTMLDivElement>(null) |
| 22 | + const [reference] = useState(Keypair.generate().publicKey) |
| 23 | + |
| 24 | + const [size, setSize] = useState(() => |
| 25 | + typeof window === "undefined" ? 100 : Math.min(window.outerWidth - 10, 512) |
| 26 | + ) |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const listener = () => setSize(Math.min(window.outerWidth - 10, 512)) |
| 30 | + window.addEventListener("resize", listener) |
| 31 | + return () => window.removeEventListener("resize", listener) |
| 32 | + }, []) |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const { location } = window |
| 36 | + const params = new URLSearchParams() |
| 37 | + params.append("reference", reference.toString()) |
| 38 | + |
| 39 | + const apiUrl = `${location.protocol}//${ |
| 40 | + location.host |
| 41 | + }/api/mintCnft?${params.toString()}` |
| 42 | + const urlParams: TransactionRequestURLFields = { |
| 43 | + link: new URL(apiUrl), |
| 44 | + label: "Label", |
| 45 | + message: "Message", |
| 46 | + } |
| 47 | + const solanaUrl = encodeURL(urlParams) |
| 48 | + console.log(solanaUrl) |
| 49 | + const qr = createQR(solanaUrl, size, "white") |
| 50 | + if (qrRef.current) { |
| 51 | + qrRef.current.innerHTML = "" |
| 52 | + qr.append(qrRef.current) |
| 53 | + } |
| 54 | + }, [window, size, reference]) |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + const interval = setInterval(async () => { |
| 58 | + try { |
| 59 | + const signatureInfo = await findReference(connection, reference, { |
| 60 | + finality: "confirmed", |
| 61 | + }) |
| 62 | + setConfirmed(true) |
| 63 | + } catch (e) { |
| 64 | + if (e instanceof FindReferenceError) return |
| 65 | + if (e instanceof ValidateTransferError) { |
| 66 | + console.error("Transaction is invalid", e) |
| 67 | + return |
| 68 | + } |
| 69 | + console.error("Unknown error", e) |
| 70 | + } |
| 71 | + }, 500) |
| 72 | + return () => { |
| 73 | + clearInterval(interval) |
| 74 | + setConfirmed(false) |
| 75 | + } |
| 76 | + }, [reference.toString()]) |
| 77 | + |
| 78 | + return ( |
| 79 | + <VStack |
| 80 | + position="fixed" |
| 81 | + top="50%" |
| 82 | + left="50%" |
| 83 | + transform="translate(-50%, -50%)" |
| 84 | + backgroundColor="white" |
| 85 | + padding="10px" |
| 86 | + rounded="2xl" |
| 87 | + > |
| 88 | + {confirmed ? ( |
| 89 | + <div style={{ width: size }}> |
| 90 | + <Confirmed /> |
| 91 | + </div> |
| 92 | + ) : ( |
| 93 | + <Flex ref={qrRef} /> |
| 94 | + )} |
| 95 | + <Button |
| 96 | + color="gray" |
| 97 | + onClick={() => { |
| 98 | + setConfirmed(false) |
| 99 | + onClose() |
| 100 | + }} |
| 101 | + > |
| 102 | + Close |
| 103 | + </Button> |
| 104 | + </VStack> |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +export default QrModal |
0 commit comments