import type { SelfID } from '@self.id/framework' import copy from 'copy-to-clipboard' import { Box, Button, Heading, Spinner, Text } from 'grommet' import { useRouter } from 'next/router' import { useCallback, useState } from 'react' import toast from 'react-hot-toast' import { useConnectionState, useAddGitHubAttestation, useIdentityLink } from '../../hooks' import AddSocialAccountContainer from './AddSocialAccountContainer' type Props = { selfID: SelfID } function AddGitHubAccount({ selfID }: Props) { const identityLink = useIdentityLink() const addGitHubAttestation = useAddGitHubAttestation(identityLink) const [challengeLoading, setChallengeLoading] = useState(false) const [challenge, setChallenge] = useState(null) const [verifyLoading, setVerifyLoading] = useState(false) const router = useRouter() const { username } = router.query const copyMessage = useCallback(() => { if (typeof username !== 'string' || challengeLoading) { return } const toastId = toast.loading('Loading challenge...') setChallengeLoading(true) identityLink.requestGitHub(selfID.id, username).then( (challenge) => { setChallenge(challenge) if (copy(selfID.id)) { toast.success('Copied to clipboard!', { id: toastId }) } else { toast.error('Failed to copy to clipboard', { id: toastId }) } setChallengeLoading(false) }, (err: Error) => { toast.error(`Failed to get challenge: ${err.message}`, { id: toastId }) setChallengeLoading(false) } ) }, [challengeLoading, selfID.id, identityLink, username]) const verify = useCallback(() => { if (challenge == null || typeof username !== 'string' || verifyLoading) { return } const toastId = toast.loading('Verifying...') setVerifyLoading(true) addGitHubAttestation(selfID.did, username, challenge).then( () => { toast.success('Attestation added!', { id: toastId }) setVerifyLoading(false) return router.push('/me/social-accounts') }, (err: Error) => { toast.error(`Failed to verify or add attestation: ${err.message}`, { id: toastId }) setVerifyLoading(false) } ) }, [addGitHubAttestation, challenge, router, selfID.did, username, verifyLoading]) return ( Step 1 Click this button to copy the verification message. {challengeLoading ? (