Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Top Users not rendering #185

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/components/creators/TopUsersCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Skeleton } from 'antd'
import clsx from 'clsx'
import { ComponentProps, CSSProperties } from 'react'
import { useSelectProfileSpace, useSelectSpace } from 'src/rtk/app/hooks'
import { ComponentProps, CSSProperties, useMemo } from 'react'
import { useFetchProfileSpaces, useSelectProfileSpace, useSelectSpace } from 'src/rtk/app/hooks'
import { useFetchTopUsers } from 'src/rtk/features/activeStaking/hooks'
import { useIsMobileWidthOrDevice } from '../responsive'
import { SpaceAvatar } from '../spaces/helpers'
Expand All @@ -15,7 +15,15 @@ export default function TopUsersCard({ ...props }: TopUsersCardProps) {
const { data, loading } = useFetchTopUsers()
const isMobile = useIsMobileWidthOrDevice()

const isLoading = loading || !data
const args = useMemo(
() => ({
ids: [...(data?.stakers ?? []), ...(data?.creators ?? [])].map(({ address }) => address),
}),
[data],
)
const { loading: loadingSpaces } = useFetchProfileSpaces(args)

const isLoading = loading || !data || loadingSpaces

// const seeMoreButton = (
// <Button
Expand Down
18 changes: 11 additions & 7 deletions src/rtk/app/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,18 @@ export function createSimpleFetchWrapper<Args, ReturnValue>({
if (fetchedData && !shouldFetchCondition?.(fetchedData)) return fetchedData
}

const promise = fetchData(allArgs, getState(), dispatch)
currentlyFetchingMap.set(id, promise)
const res = await promise

await dispatch(saveToCacheAction(res))
currentlyFetchingMap.delete(id)
try {
const promise = fetchData(allArgs, getState(), dispatch)
currentlyFetchingMap.set(id, promise)
const res = await promise

return promise
await dispatch(saveToCacheAction(res))
return promise
} catch (err) {
throw err
} finally {
currentlyFetchingMap.delete(id)
}
},
)
}
Expand Down
7 changes: 5 additions & 2 deletions src/rtk/features/activeStaking/topUsersSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const sliceName = 'topUsers'

export const selectTopUsers = (state: RootState) => state.topUsers

export const fetchTopUsers = createSimpleFetchWrapper<null, TopUsers | null>({
export const fetchTopUsers = createSimpleFetchWrapper<{}, TopUsers | null>({
sliceName,
fetchData: async function () {
const data = await getTopUsers()
Expand All @@ -31,13 +31,16 @@ export async function fetchTopUsersWithSpaces(
dispatch: AppDispatch,
api: SubsocialApi,
) {
await dispatch(fetchTopUsers())
console.log('fetching top users...')
await dispatch(fetchTopUsers({ reload: true }))
const state = selectTopUsers(store.getState())
console.log('dont have state?', !state)
if (!state) return

const parsedState = state as TopUsers
const creators = parsedState.creators.map(user => user.address)
const stakers = parsedState.stakers.map(user => user.address)
console.log('fetching profiles', [...creators, ...stakers])

await dispatch(fetchProfileSpaces({ ids: [...creators, ...stakers], api }))
}
Expand Down
Loading