fix: CharacterCard fetch every open (#482)

This commit is contained in:
birdgg 2023-05-06 07:03:23 +08:00 committed by GitHub
parent 104ff96844
commit 54b7ded4ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 19 deletions

View File

@ -1,14 +1,12 @@
import { useTranslation } from "next-i18next"
import { useEffect, useState } from "react"
import { FollowingButton } from "~/components/common/FollowingButton"
import { FollowingCount } from "~/components/common/FollowingCount"
import { Titles } from "~/components/common/Titles"
import { Avatar } from "~/components/ui/Avatar"
import { useDate } from "~/hooks/useDate"
import type { ExpandedCharacter } from "~/lib/types"
import { cn } from "~/lib/utils"
import * as siteModel from "~/models/site.model"
import { useGetCharacterCard } from "~/queries/site"
export const CharacterCard: React.FC<{
siteId?: string
@ -27,25 +25,14 @@ export const CharacterCard: React.FC<{
simple,
style,
}) => {
const [firstOpen, setFirstOpen] = useState("")
const [site, setSite] = useState<ExpandedCharacter>()
const date = useDate()
const { t } = useTranslation("common")
useEffect(() => {
if (open && (firstOpen !== (siteId || address) || !site)) {
if (siteId || address) {
setFirstOpen(siteId || address || "")
if (siteId) {
siteModel.getSite(siteId).then((site) => setSite(site))
} else if (address) {
siteModel.getSiteByAddress(address).then((site) => setSite(site))
}
} else {
setSite(undefined)
}
}
}, [open, firstOpen, siteId, address, site])
const { data: site } = useGetCharacterCard({
siteId,
address,
enabled: open || false,
})
return (
<span

View File

@ -401,3 +401,29 @@ export const useGetGreenfieldId = (cid?: string) => {
return siteModel.getGreenfieldId(cid)
})
}
export const useGetCharacterCard = ({
siteId,
address,
enabled,
}: {
siteId?: string
address?: string
enabled: boolean
}) => {
return useQuery(
["useGetCharacterCard", { siteId, address }],
async () => {
if (siteId) {
return siteModel.getSite(siteId)
} else if (address) {
return siteModel.getSiteByAddress(address)
} else {
return null
}
},
{
enabled,
},
)
}