|
| 1 | +import { useRouter } from "next/router" |
| 2 | +import { Button } from "~/components/ui/Button" |
| 3 | +import type { Variant } from "~/components/ui/Button" |
| 4 | +import { useAccount } from "wagmi" |
| 5 | +import { useEffect, useState } from "react" |
| 6 | +import { |
| 7 | + useGetLinks, |
| 8 | + useLinkCharacter, |
| 9 | + useUnlinkCharacter, |
| 10 | + useGetCharacters, |
| 11 | +} from "~/queries/character" |
| 12 | +import { useConnectModal } from "@rainbow-me/rainbowkit" |
| 13 | +import clsx from "clsx" |
| 14 | +import { BellIcon } from "@heroicons/react/24/solid" |
| 15 | + |
| 16 | +export const FollowingButton: React.FC<{ |
| 17 | + characterId?: number |
| 18 | + variant?: Variant |
| 19 | + className?: string |
| 20 | + size?: "sm" | "xl" |
| 21 | +}> = ({ characterId, variant, className, size }) => { |
| 22 | + const { address } = useAccount() |
| 23 | + const linkCharacter = useLinkCharacter() |
| 24 | + const unlinkCharacter = useUnlinkCharacter() |
| 25 | + const { openConnectModal } = useConnectModal() |
| 26 | + const [followProgress, setFollowProgress] = useState<boolean>(false) |
| 27 | + const userCharacters = useGetCharacters(address, true) |
| 28 | + const router = useRouter() |
| 29 | + |
| 30 | + const backlinks = useGetLinks( |
| 31 | + userCharacters.data?.list[0].characterId, |
| 32 | + characterId, |
| 33 | + ) |
| 34 | + |
| 35 | + const handleClickSubscribe = async (e: any) => { |
| 36 | + e.preventDefault() |
| 37 | + if (!address) { |
| 38 | + setFollowProgress(true) |
| 39 | + openConnectModal?.() |
| 40 | + } else if (!userCharacters.data?.count) { |
| 41 | + router.push("/new") |
| 42 | + } else if (characterId) { |
| 43 | + if (backlinks.data?.count) { |
| 44 | + unlinkCharacter.mutate({ |
| 45 | + fromCharacterId: userCharacters.data?.list[0].characterId, |
| 46 | + toCharacterId: characterId, |
| 47 | + }) |
| 48 | + } else { |
| 49 | + linkCharacter.mutate({ |
| 50 | + fromCharacterId: userCharacters.data?.list[0].characterId, |
| 51 | + toCharacterId: characterId, |
| 52 | + }) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if ( |
| 59 | + followProgress && |
| 60 | + address && |
| 61 | + backlinks.isSuccess && |
| 62 | + characterId && |
| 63 | + userCharacters.isSuccess |
| 64 | + ) { |
| 65 | + if (!userCharacters.data?.count) { |
| 66 | + router.push("/new") |
| 67 | + } |
| 68 | + if (!backlinks.data?.count) { |
| 69 | + linkCharacter.mutate({ |
| 70 | + fromCharacterId: userCharacters.data?.list[0].characterId, |
| 71 | + toCharacterId: characterId, |
| 72 | + }) |
| 73 | + } |
| 74 | + setFollowProgress(false) |
| 75 | + } |
| 76 | + }, [ |
| 77 | + backlinks.isSuccess, |
| 78 | + backlinks.data?.count, |
| 79 | + router, |
| 80 | + followProgress, |
| 81 | + address, |
| 82 | + userCharacters.isSuccess, |
| 83 | + userCharacters.data, |
| 84 | + characterId, |
| 85 | + linkCharacter, |
| 86 | + ]) |
| 87 | + |
| 88 | + return ( |
| 89 | + <Button |
| 90 | + variant={variant} |
| 91 | + onClick={handleClickSubscribe} |
| 92 | + className={clsx(className, "align-middle space-x-1")} |
| 93 | + isLoading={ |
| 94 | + backlinks.data?.count |
| 95 | + ? linkCharacter.isLoading || unlinkCharacter.isLoading |
| 96 | + : userCharacters.isLoading || |
| 97 | + unlinkCharacter.isLoading || |
| 98 | + linkCharacter.isLoading || |
| 99 | + backlinks.isLoading |
| 100 | + } |
| 101 | + size={size} |
| 102 | + aria-label="follow" |
| 103 | + > |
| 104 | + <BellIcon className="h-4 w-4" /> |
| 105 | + {backlinks.data?.count ? ( |
| 106 | + <> |
| 107 | + <span className="group-hover:hidden">Following</span> |
| 108 | + <span className="hidden group-hover:block">Unfollow</span> |
| 109 | + </> |
| 110 | + ) : ( |
| 111 | + <span>Follow</span> |
| 112 | + )} |
| 113 | + </Button> |
| 114 | + ) |
| 115 | +} |
0 commit comments