Skip to content

Commit

Permalink
refactor: infinite query in CyberConnect (#11290)
Browse files Browse the repository at this point in the history
* refactor: infinite query in CyberConnect

* refactor: remove comments

---------

Co-authored-by: guanbinrui <52657989+guanbinrui@users.noreply.github.com>
  • Loading branch information
UncleBill and guanbinrui committed Jan 16, 2024
1 parent 7ba62c8 commit 5a84283
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 62 deletions.
45 changes: 17 additions & 28 deletions packages/plugins/CyberConnect/src/SiteAdaptor/FollowersPage.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { LoadingBase, MaskLightTheme, makeStyles } from '@masknet/theme'
import { Box } from '@mui/material'
import { ThemeProvider } from '@emotion/react'
import { ElementAnchor, LoadingStatus, ReloadStatus } from '@masknet/shared'
import { useCyberConnectTrans } from '../locales/index.js'
import { useFollowers } from '../hooks/useFollowers.js'
import type { ProfileTab } from '../constants.js'
import { LoadingBase, MaskLightTheme } from '@masknet/theme'
import { Box } from '@mui/material'
import type { IFollowIdentity } from '../Worker/apis/index.js'
import type { ProfileTab } from '../constants.js'
import { useFollowers } from '../hooks/useFollowers.js'
import { useCyberConnectTrans } from '../locales/index.js'
import { FollowRow } from './FollowTab.js'
import { useEffect } from 'react'
import { useIterator } from '@masknet/web3-hooks-base'
import { ThemeProvider } from '@emotion/react'

const useStyles = makeStyles()((theme) => ({
root: {},
}))

interface FollowersPageProps {
address?: string
Expand All @@ -22,31 +16,26 @@ interface FollowersPageProps {

export function FollowersPage(props: FollowersPageProps) {
const t = useCyberConnectTrans()
const { classes } = useStyles()
const iterator = useFollowers(props.tab, props.address)
const { value, next, done, error, retry, loading } = useIterator<IFollowIdentity>(iterator)

useEffect(() => {
if (value?.length || loading) return
if (next) next()
}, [value?.length, loading, next])
const { data, fetchNextPage, hasNextPage, isPending, refetch, error } = useFollowers(props.tab, props.address)
const followers = data?.pages.flatMap((x) => x?.data || []) || []

if (error) {
return (
<ThemeProvider theme={MaskLightTheme}>
<ReloadStatus height={400} p={2} message={t.failed()} onRetry={retry} />
<ReloadStatus height={400} p={2} message={t.failed()} onRetry={refetch} />
</ThemeProvider>
)
}

if (!value?.length && loading) return <LoadingStatus height={400} omitText />
if (!followers.length && isPending) return <LoadingStatus height={400} omitText />

return (
<Box className={classes.root}>
{value?.length ?
value.map((x: IFollowIdentity) => <FollowRow key={x.ens || x.address} identity={x} />)
: props.hint}
<ElementAnchor callback={() => next?.()}>
{!done && value?.length ?
<Box>
{followers.map((x: IFollowIdentity) => (
<FollowRow key={x.ens || x.address} identity={x} />
))}
<ElementAnchor callback={() => fetchNextPage()}>
{hasNextPage && isPending ?
<LoadingBase />
: null}
</ElementAnchor>
Expand Down
20 changes: 11 additions & 9 deletions packages/plugins/CyberConnect/src/hooks/useFollowers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useMemo } from 'react'
import { pageableToIterator } from '@masknet/shared-base'
import { PluginCyberConnectRPC } from '../messages.js'
import { useInfiniteQuery } from '@tanstack/react-query'
import type { ProfileTab } from '../constants.js'
import { PluginCyberConnectRPC } from '../messages.js'

export function useFollowers(tab: ProfileTab, address?: string, size = 50) {
return useMemo(() => {
if (!address) return
return pageableToIterator(async (indicator) => {
return PluginCyberConnectRPC.fetchFollowers(tab, address, size, indicator)
})
}, [address, tab, size])
return useInfiniteQuery({
queryKey: ['cyber-connect', 'followers', tab, address, size],
initialPageParam: undefined as any,
queryFn: async ({ pageParam }) => {
if (!address) return
return PluginCyberConnectRPC.fetchFollowers(tab, address, size, pageParam)
},
getNextPageParam: (lastPage) => lastPage?.nextIndicator,
})
}
1 change: 0 additions & 1 deletion packages/web3-hooks/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export * from './useSocialAccountsFrom.js'
export * from './useSocialAddresses.js'
export * from './useSocialAddressesAll.js'
export * from './useSocialAccountListByAddressOrDomain.js'
export * from './useTransactions.js'
export * from './useTransferFungibleToken.js'
export * from './useTrustedFungibleTokens.js'
export * from './useWallet.js'
Expand Down
24 changes: 0 additions & 24 deletions packages/web3-hooks/base/src/useTransactions.ts

This file was deleted.

0 comments on commit 5a84283

Please sign in to comment.