Skip to content

Commit

Permalink
Fix posts list on profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
samchuk-vlad committed Apr 2, 2024
1 parent 1deb6c8 commit 9f6037b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 55 deletions.
56 changes: 17 additions & 39 deletions src/components/activity/AccountActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { useRouter } from 'next/router'
import { useCallback, useEffect, useState } from 'react'
import config from 'src/config'
import { ActivityCounts } from 'src/graphql/apis'
import {
useGetActivityCounts,
useGetCommentActivities,
useGetPostActivities,
} from 'src/graphql/hooks'
import { useGetActivityCounts, useGetCommentActivities } from 'src/graphql/hooks'
import { useFetchPosts, useSelectPost, useSelectSpace } from 'src/rtk/app/hooks'
import { useAppDispatch } from 'src/rtk/app/store'
import { fetchProfilePosts } from 'src/rtk/features/posts/postsSlice'
Expand All @@ -19,6 +15,7 @@ import ChatLinkButtonWithCounter from '../chat/ChatLinkButtonWithCounter'
import { InfinitePageList, InnerLoadMoreFn } from '../lists'
import { PublicPostPreviewById } from '../posts/PublicPostPreview'
import WriteSomething from '../posts/WriteSomething'
import { useIsMySpace } from '../spaces/helpers'
import { CreatePostButton } from '../spaces/helpers/CreatePostButton'
import { FollowerCanPostAlert } from '../spaces/permissions/FollowerCanPostAlert'
import { useSubsocialApi } from '../substrate'
Expand Down Expand Up @@ -58,25 +55,10 @@ const CommentActivities = (props: BaseActivityProps) => {
)
}

const PostActivities = (props: BaseActivityProps) => {
const getPostActivities = useGetPostActivities()
const loadMorePosts = createLoadMorePosts(getPostActivities)

return (
<FeedActivities
{...props}
showMuted
loadMore={loadMorePosts}
noDataDesc='No posts yet'
loadingLabel='Loading posts...'
/>
)
}

type ProfileSpacePostsProps = {
address: string
postIds: string[]
space: SpaceStruct
postIds?: string[]
space?: SpaceStruct
profilePostsCount?: number
}

Expand Down Expand Up @@ -113,18 +95,18 @@ const ProfileSpacePosts = ({
() => (
<InfinitePageList
loadingLabel='Loading more posts...'
initialPage={1}
initialPage={postIds ? 1 : 0}
dataSource={postIds}
loadMore={loadMore}
totalCount={profilePostsCount || 0}
noDataDesc='No posts yet'
noDataExt={<CreatePostButton space={space} />}
noDataExt={space && <CreatePostButton space={space} />}
getKey={postId => postId}
renderItem={postId => <PublicPostPreviewById postId={postId} />}
beforeList={<FollowerCanPostAlert space={space} />}
beforeList={space && <FollowerCanPostAlert space={space} />}
/>
),
[isApiReady, profilePostsCount],
[isApiReady],
)

return <List />
Expand All @@ -150,7 +132,6 @@ const OffchainAccountActivity = ({
address,
withWriteSomethingBlock = true,
spaceId,
postsCount: profilePostsCount,
withSpacePosts,
}: ActivitiesByAddressProps) => {
const initialActiveTab = 'posts'
Expand All @@ -169,6 +150,8 @@ const OffchainAccountActivity = ({

const post = useSelectPost(chatId)

const isMySpace = useIsMySpace(space?.struct)

// to make tweets tab doesn't disappear until the address is changed.

useEffect(() => {
Expand All @@ -177,10 +160,10 @@ const OffchainAccountActivity = ({
;(async () => {
if (!address) return

const counts = await getActivityCounts({ address })
const counts = await getActivityCounts({ address, withHidden: isMySpace || isMyAddress })
setCounts(counts)
})()
}, [address])
}, [address, isMySpace, isMyAddress])

useEffect(() => {
const hash = window.location.hash.substring(1) as ActivityTab
Expand All @@ -200,15 +183,13 @@ const OffchainAccountActivity = ({
router.replace('#' + activeKey)
}

const postsView = withSpacePosts ? (
const postsView = (
<ProfileSpacePosts
postIds={withSpacePosts.postIds}
profilePostsCount={profilePostsCount}
space={withSpacePosts.spaceData.struct}
postIds={withSpacePosts?.postIds}
profilePostsCount={postsCount}
space={space?.struct}
address={address}
/>
) : (
<PostActivities address={address} totalCount={postsCount} />
)

return (
Expand All @@ -226,10 +207,7 @@ const OffchainAccountActivity = ({
)
}}
>
<TabPane
tab={getTabTitle('Posts', withSpacePosts ? profilePostsCount || 0 : postsCount)}
key={getTab('posts')}
>
<TabPane tab={getTabTitle('Posts', postsCount)} key={getTab('posts')}>
{isMyAddress ? (
<div className={clsx('d-flex flex-column', withWriteSomethingBlock && 'mt-3')}>
{withWriteSomethingBlock && <WriteSomething />}
Expand Down
2 changes: 0 additions & 2 deletions src/components/chat/ChatButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const ChatButton = ({ spaceId }: ChatButtonProps) => {
const isPostHidden = !!post?.post.struct.hidden
const isRemovedPost = !post?.post.struct.spaceId

console.log(isRemovedPost, post?.id)

if (chat && spaceContent && !isEmptyArray(chats) && !isPostHidden) return null

return isPostHidden && !isRemovedPost ? (
Expand Down
1 change: 0 additions & 1 deletion src/components/chat/CreateChatModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const CreateChatModalButton = ({ size }: CreateChatModalButtonProps) => {
router.push(value)
setOpenModal(false)
} else if (name === 'redirect-hard') {
console.log(value)
// Using router push for redirect don't redirect properly, it just have loading for a bit and changes the url much later
window.location.href = value
setOpenModal(false)
Expand Down
8 changes: 7 additions & 1 deletion src/components/posts/view-post/PostPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ export function PostPreview(props: PreviewProps) {
const sharedPostOriginalId = isSharedPost ? asSharedPostStruct(post).originalPostId : undefined
usePostViewTracker(post.id, sharedPostOriginalId, inView && !!myAddress)

if (isUnlisted || isHiddenChatRoom || isBlocked || !post.spaceId) return null
if (
isUnlisted ||
isHiddenChatRoom ||
isBlocked ||
((post.isRegularPost || post.isSharedPost) && !post.spaceId)
)
return null

const postContent = postDetails.post.content
const isEmptyContent = !isSharedPost && !postContent?.title && !postContent?.body
Expand Down
2 changes: 1 addition & 1 deletion src/components/spaces/ViewSpace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export const InnerViewSpace = (props: Props) => {
}}
withWriteSomethingBlock={false}
address={spaceData.struct.ownerId}
spaceId={spaceId}
spaceId={ownerProfileSpaceId}
postsCount={filteredPostsCount}
/>
) : (
Expand Down
1 change: 0 additions & 1 deletion src/graphql/__generated__/GetPostsData.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/graphql/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export async function getPostIdsBySpaces(
export type ActivityCounts = Counts & { tweetsCount: number }
export async function getActivityCounts(
client: GqlClient,
variables: GetActivityCountsVariables,
variables: GetActivityCountsVariables & { withHidden?: boolean },
): Promise<ActivityCounts> {
const res = await client.query<GetActivityCounts, GetActivityCountsVariables>({
query: q.GET_ACTIVITY_COUNTS,
variables,
query: q.GET_ACTIVITY_COUNTS(variables.withHidden),
variables: { address: variables.address },
})

const { activities, comments, follows, posts, reactions, spaces, tweets } = res.data
Expand Down Expand Up @@ -232,13 +232,15 @@ export async function getCommentActivities(
query: q.GET_COMMENT_ACTIVITIES,
variables,
})

const commentIds: string[] = []
activities.data.accountById?.posts.forEach(post => {
const commentId = post?.id
if (commentId) {
commentIds.push(commentId)
}
})

return commentIds
}

Expand Down
14 changes: 9 additions & 5 deletions src/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export const GET_POST_IDS_BY_SPACES = gql`
export const GET_POSTS_DATA = gql`
${POST_FRAGMENT}
query GetPostsData($where: PostWhereInput, $offset: Int, $limit: Int) {
posts(where: $where, limit: $limit, offset: $offset) {
posts(where: $where, limit: $limit, offset: $offset, orderBy: createdAtTime_DESC) {
...PostFragment
sharedPost {
...PostFragment
Expand Down Expand Up @@ -435,7 +435,7 @@ export const GET_NEWS_FEEDS = gql`

// Activities
// ------------------------------------------------------------------------------------
export const GET_ACTIVITY_COUNTS = gql`
export const GET_ACTIVITY_COUNTS = (withHidden?: boolean) => gql`
query GetActivityCounts($address: String!) {
activities: activitiesConnection(
orderBy: id_ASC
Expand Down Expand Up @@ -469,7 +469,9 @@ export const GET_ACTIVITY_COUNTS = gql`
}
posts: postsConnection(
orderBy: id_ASC
where: { ownedByAccount: { id_eq: $address }, isComment_eq: false, hidden_eq: false }
where: { ownedByAccount: { id_eq: $address }, isComment_eq: false, space_isNull: false,
${!withHidden ? 'hidden_eq: false' : ''}
}
) {
totalCount
}
Expand All @@ -489,7 +491,9 @@ export const GET_ACTIVITY_COUNTS = gql`
}
comments: postsConnection(
orderBy: id_ASC
where: { ownedByAccount: { id_eq: $address }, isComment_eq: true, hidden_eq: false }
where: { ownedByAccount: { id_eq: $address }, isComment_eq: true, ${
!withHidden ? 'hidden_eq: false' : ''
} }
) {
totalCount
}
Expand Down Expand Up @@ -649,7 +653,7 @@ export const GET_POST_ACTIVITIES = gql`
limit: $limit
offset: $offset
orderBy: createdAtTime_DESC
where: { isComment_eq: false, hidden_eq: false }
where: { isComment_eq: false, hidden_eq: false, space_isNull: false }
) {
id
}
Expand Down
3 changes: 1 addition & 2 deletions src/rtk/features/posts/postsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,8 @@ export const fetchProfilePosts = createAsyncThunk<
limit,
where: {
ownedByAccount: { id_eq: address },
space_isNull: false,
AND: [{ space_isNull: false, isComment_eq: false }],
},
orderBy: 'createdAtTime_DESC',
})

const {
Expand Down

0 comments on commit 9f6037b

Please sign in to comment.