Skip to content

Commit

Permalink
expect different response
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-balazs committed Jul 4, 2024
1 parent 98078b5 commit 301d311
Showing 1 changed file with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
useBreakpointValue,
useDisclosure,
} from "@chakra-ui/react"
import { FarcasterProfile as FarcasterProfileType } from "@guildxyz/types"
import { ArrowCounterClockwise, DeviceMobileCamera } from "@phosphor-icons/react"
import useUser from "components/[guild]/hooks/useUser"
import { usePostHogContext } from "components/_app/PostHogProvider"
Expand Down Expand Up @@ -86,8 +87,10 @@ const ConnectFarcasterButton = ({

const submitSignedKeyRequest = (
signedPayload: SignedValidation
): Promise<{ url: string; deadline: number; deadlineRelative: number }> =>
fetcher(`/v2/users/${userId}/farcaster-signed-keys`, signedPayload)
): Promise<
| { url: string; deadline: number; deadlineRelative: number }
| ({ usedExistingKey: true } & FarcasterProfileType)
> => fetcher(`/v2/users/${userId}/farcaster-signed-keys`, signedPayload)

const [seconds, { startCountdown, resetCountdown }] = useCountdown({
countStart: 11 * 60 - 1, // -1 Just so we don't show "11 minutes" for a second at start
Expand All @@ -96,7 +99,20 @@ const ConnectFarcasterButton = ({
const shouldEnableRegenerateButton = seconds < ENABLE_REGENERATE_BUTTON_AT_SEC

const signedKeyRequest = useSubmitWithSign(submitSignedKeyRequest, {
onSuccess: ({ deadlineRelative }) => {
onSuccess: (response) => {
if ("usedExistingKey" in response) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { usedExistingKey, ...profile } = response
mutate((prev) => (prev ? { ...prev, farcasterProfiles: [profile] } : prev), {
revalidate: false,
}).then(() => {
onApprove()
})
return
}

const { deadlineRelative } = response

const deadline = Date.now() + deadlineRelative
startCountdown()

Expand Down Expand Up @@ -159,6 +175,10 @@ const ConnectFarcasterButton = ({
onClose()
}

const url =
signedKeyRequest.response && "url" in signedKeyRequest.response
? signedKeyRequest.response.url
: null
const qrSize = useBreakpointValue({ base: 300, md: 400 })

return (
Expand Down Expand Up @@ -194,10 +214,10 @@ const ConnectFarcasterButton = ({
<ModalBody pt={8}>
<VStack justifyContent="center">
<Skeleton isLoaded={!signedKeyRequest.isLoading} borderRadius={"md"}>
{!isMobile && signedKeyRequest.response?.url && (
{!isMobile && url && (
<Box borderRadius="md" borderWidth={3} overflow="hidden">
<QRCodeSVG
value={signedKeyRequest.response.url}
value={url}
size={qrSize}
style={{ maxWidth: "100%" }}
/>
Expand Down Expand Up @@ -257,12 +277,12 @@ const ConnectFarcasterButton = ({
</Accordion>
</VStack>
</ModalBody>
{isMobile && (
{isMobile && url && (
<ModalFooter>
<Button
w="full"
as="a"
href={signedKeyRequest.response?.url}
href={url}
target="_blank"
colorScheme="FARCASTER"
>
Expand Down

0 comments on commit 301d311

Please sign in to comment.