|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Suspense, useCallback, useState } from "react"; |
| 4 | +import { Header } from "@/components/header/header"; |
| 5 | +import { |
| 6 | + Button, |
| 7 | + Modal, |
| 8 | + ModalContent, |
| 9 | + ModalOverlay, |
| 10 | + ModalPortal, |
| 11 | + Typography, |
| 12 | +} from "@mochi-ui/core"; |
| 13 | +import { useDisclosure } from "@dwarvesf/react-hooks"; |
| 14 | +import { LoginWidget } from "@mochi-web3/login-widget"; |
| 15 | +import { useSearchParams } from "next/navigation"; |
| 16 | +import { WretchError } from "wretch"; |
| 17 | +import { API } from "@/constants/api"; |
| 18 | + |
| 19 | +const Verify = () => { |
| 20 | + const { isOpen, onOpenChange } = useDisclosure(); |
| 21 | + const searchParams = useSearchParams(); |
| 22 | + const [loading, setLoading] = useState(false); |
| 23 | + const [verified, setVerified] = useState(false); |
| 24 | + const [error, _setError] = useState(""); |
| 25 | + const setError = useCallback( |
| 26 | + (e: WretchError) => { |
| 27 | + _setError(e.json?.msg ?? "Something went wrong"); |
| 28 | + }, |
| 29 | + [_setError] |
| 30 | + ); |
| 31 | + |
| 32 | + const code = searchParams.get("code"); |
| 33 | + const guild_id = searchParams.get("guild_id"); |
| 34 | + |
| 35 | + if (error) { |
| 36 | + return ( |
| 37 | + <div className="py-8 px-8 mx-auto md:px-16 md:max-w-2xl min-h-[calc(100vh-56px)] flex flex-col items-center justify-center"> |
| 38 | + <div className="mb-2 font-medium text-center md:text-xl"> |
| 39 | + Something went wrong with error |
| 40 | + </div> |
| 41 | + <div className="py-2 px-4 w-full font-mono rounded bg-stone-200"> |
| 42 | + “{error}” |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (verified) { |
| 49 | + return ( |
| 50 | + <div className="py-8 px-8 mx-auto md:px-16 md:max-w-2xl min-h-[calc(100vh-56px)] flex flex-col items-center justify-center"> |
| 51 | + <div className="text-2xl font-black text-center md:text-3xl"> |
| 52 | + <span className="uppercase text-tono-gradient"> |
| 53 | + Your wallet is verified! You can close this window |
| 54 | + </span>{" "} |
| 55 | + ✨ |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="w-full min-h-[calc(100vh-56px)] flex flex-col items-center justify-center text-center px-4 space-y-6"> |
| 63 | + <Typography |
| 64 | + level="h4" |
| 65 | + className="mb-4 text-center uppercase !font-black text-tono-gradient" |
| 66 | + > |
| 67 | + Verify your wallet |
| 68 | + </Typography> |
| 69 | + <Typography level="p3" className="mb-3 text-center max-w-md"> |
| 70 | + Connect your wallet to verify and get full access to Tono with more |
| 71 | + exclusive privileges. |
| 72 | + </Typography> |
| 73 | + <div> |
| 74 | + <Modal open={isOpen} onOpenChange={onOpenChange}> |
| 75 | + <ModalPortal> |
| 76 | + <ModalOverlay /> |
| 77 | + <ModalContent |
| 78 | + className="p-3 w-full sm:w-auto" |
| 79 | + style={{ |
| 80 | + maxWidth: "calc(100% - 32px)", |
| 81 | + }} |
| 82 | + > |
| 83 | + <LoginWidget |
| 84 | + raw |
| 85 | + onchain |
| 86 | + onWalletConnectSuccess={async ({ |
| 87 | + address, |
| 88 | + signature, |
| 89 | + platform, |
| 90 | + }) => { |
| 91 | + if (!code || loading) return; |
| 92 | + setLoading(true); |
| 93 | + const payload = { |
| 94 | + wallet_address: address, |
| 95 | + code, |
| 96 | + signature, |
| 97 | + message: |
| 98 | + "Please sign this message to prove wallet ownership", |
| 99 | + }; |
| 100 | + |
| 101 | + await API.MOCHI_PROFILE.post( |
| 102 | + payload, |
| 103 | + `/profiles/me/accounts/connect-${platform.replace( |
| 104 | + "-chain", |
| 105 | + "" |
| 106 | + )}` |
| 107 | + ) |
| 108 | + .badRequest(setError) |
| 109 | + .json(async (r) => { |
| 110 | + const user_discord_id = r.associated_accounts.find( |
| 111 | + (aa: any) => aa.platform === "discord" |
| 112 | + )?.platform_identifier; |
| 113 | + if (!guild_id) { |
| 114 | + setVerified(true); |
| 115 | + } else if (user_discord_id) { |
| 116 | + await API.MOCHI.post( |
| 117 | + { |
| 118 | + user_discord_id, |
| 119 | + guild_id, |
| 120 | + }, |
| 121 | + `/verify/assign-role` |
| 122 | + ) |
| 123 | + .badRequest(setError) |
| 124 | + .res(() => { |
| 125 | + setVerified(true); |
| 126 | + }) |
| 127 | + .catch(setError) |
| 128 | + .finally(() => { |
| 129 | + setLoading(false); |
| 130 | + onOpenChange(false); |
| 131 | + }); |
| 132 | + } |
| 133 | + }); |
| 134 | + }} |
| 135 | + /> |
| 136 | + </ModalContent> |
| 137 | + </ModalPortal> |
| 138 | + </Modal> |
| 139 | + <Button color="primary" onClick={() => onOpenChange(true)}> |
| 140 | + Verify |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +export default function Page() { |
| 148 | + return ( |
| 149 | + <main> |
| 150 | + <Header /> |
| 151 | + <Suspense> |
| 152 | + <Verify /> |
| 153 | + </Suspense> |
| 154 | + </main> |
| 155 | + ); |
| 156 | +} |
0 commit comments