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 shared posts fetching #408

Merged
merged 1 commit into from
Apr 21, 2024
Merged
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
61 changes: 50 additions & 11 deletions src/rtk/features/posts/postsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,17 +459,10 @@ export const fetchProfilePosts = createAsyncThunk<
ThunkApiConfig
>(
'posts/fetchManyByAddress',
async ({
id,
spaceId,
client,
additionalData,
withHidden,
dispatch,
api,
limit,
offset,
}): Promise<PostStruct[]> => {
async (
{ id, spaceId, client, additionalData, withHidden, dispatch, api, limit, offset },
{ getState },
): Promise<PostStruct[]> => {
const address = id as AccountId

if (!client) return []
Expand All @@ -495,6 +488,52 @@ export const fetchProfilePosts = createAsyncThunk<

const entities = await changePostsContentId(posts)

const newIds = entities.map(x => x.id)

if (isClientSide()) {
dispatch(fetchPostRewards({ postIds: newIds as string[] }))
dispatch(fetchPostsViewCount({ postIds: newIds as string[] }))
}
const fetches: Promise<any>[] = [
dispatch(fetchSuperLikeCounts({ postIds: newIds as string[] })),
dispatch(fetchCanPostsSuperLiked({ postIds: newIds as string[] })),
// need to wait for this to be fetched before any post is rendered
dispatch(fetchBlockedResources({ appId })),
]

const alreadyLoadedIds = new Set(newIds)
const extPostIds = new Set<PostId>()

const addToExtPostIds = (id: PostId) => {
if (id && !alreadyLoadedIds.has(id)) {
extPostIds.add(id)
}
}

entities.forEach(x => {
if (x.isComment) {
addToExtPostIds(asCommentStruct(x).rootPostId)
} else if (x.isSharedPost) {
addToExtPostIds(asSharedPostStruct(x).originalPostId)
}
})

const newExtIds = selectUnknownPostIds(getState(), Array.from(extPostIds), true)

fetches.push(
dispatch(
fetchPosts({
api,
ids: newExtIds,
withRootPost: false,
withContent,
reload: true,
}),
),
)

await Promise.all(fetches)

await handleAfterDataFetch({
entities,
args: {
Expand Down
Loading