From 9f6037b961e9a6665eed3c4a7399eafece81ed02 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 2 Apr 2024 18:21:02 +0300 Subject: [PATCH] Fix posts list on profile page --- src/components/activity/AccountActivity.tsx | 56 ++++++------------- src/components/chat/ChatButton.tsx | 2 - src/components/chat/CreateChatModal.tsx | 1 - .../posts/view-post/PostPreview.tsx | 8 ++- src/components/spaces/ViewSpace.tsx | 2 +- src/graphql/__generated__/GetPostsData.ts | 1 - src/graphql/apis/index.ts | 8 ++- src/graphql/queries.ts | 14 +++-- src/rtk/features/posts/postsSlice.ts | 3 +- 9 files changed, 40 insertions(+), 55 deletions(-) diff --git a/src/components/activity/AccountActivity.tsx b/src/components/activity/AccountActivity.tsx index c5729ea07..b9e666cb3 100644 --- a/src/components/activity/AccountActivity.tsx +++ b/src/components/activity/AccountActivity.tsx @@ -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' @@ -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' @@ -58,25 +55,10 @@ const CommentActivities = (props: BaseActivityProps) => { ) } -const PostActivities = (props: BaseActivityProps) => { - const getPostActivities = useGetPostActivities() - const loadMorePosts = createLoadMorePosts(getPostActivities) - - return ( - - ) -} - type ProfileSpacePostsProps = { address: string - postIds: string[] - space: SpaceStruct + postIds?: string[] + space?: SpaceStruct profilePostsCount?: number } @@ -113,18 +95,18 @@ const ProfileSpacePosts = ({ () => ( } + noDataExt={space && } getKey={postId => postId} renderItem={postId => } - beforeList={} + beforeList={space && } /> ), - [isApiReady, profilePostsCount], + [isApiReady], ) return @@ -150,7 +132,6 @@ const OffchainAccountActivity = ({ address, withWriteSomethingBlock = true, spaceId, - postsCount: profilePostsCount, withSpacePosts, }: ActivitiesByAddressProps) => { const initialActiveTab = 'posts' @@ -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(() => { @@ -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 @@ -200,15 +183,13 @@ const OffchainAccountActivity = ({ router.replace('#' + activeKey) } - const postsView = withSpacePosts ? ( + const postsView = ( - ) : ( - ) return ( @@ -226,10 +207,7 @@ const OffchainAccountActivity = ({ ) }} > - + {isMyAddress ? (
{withWriteSomethingBlock && } diff --git a/src/components/chat/ChatButton.tsx b/src/components/chat/ChatButton.tsx index c979ea71e..c6d4e03a1 100644 --- a/src/components/chat/ChatButton.tsx +++ b/src/components/chat/ChatButton.tsx @@ -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 ? ( diff --git a/src/components/chat/CreateChatModal.tsx b/src/components/chat/CreateChatModal.tsx index 8c84cbd15..7e8a5a7a8 100644 --- a/src/components/chat/CreateChatModal.tsx +++ b/src/components/chat/CreateChatModal.tsx @@ -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) diff --git a/src/components/posts/view-post/PostPreview.tsx b/src/components/posts/view-post/PostPreview.tsx index baa6b5ea5..5c95cc65a 100644 --- a/src/components/posts/view-post/PostPreview.tsx +++ b/src/components/posts/view-post/PostPreview.tsx @@ -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 diff --git a/src/components/spaces/ViewSpace.tsx b/src/components/spaces/ViewSpace.tsx index cfe95b56c..35b85a8b0 100644 --- a/src/components/spaces/ViewSpace.tsx +++ b/src/components/spaces/ViewSpace.tsx @@ -382,7 +382,7 @@ export const InnerViewSpace = (props: Props) => { }} withWriteSomethingBlock={false} address={spaceData.struct.ownerId} - spaceId={spaceId} + spaceId={ownerProfileSpaceId} postsCount={filteredPostsCount} /> ) : ( diff --git a/src/graphql/__generated__/GetPostsData.ts b/src/graphql/__generated__/GetPostsData.ts index faf821df3..6162a865e 100644 --- a/src/graphql/__generated__/GetPostsData.ts +++ b/src/graphql/__generated__/GetPostsData.ts @@ -1345,7 +1345,6 @@ export interface GetPostsData { } export interface GetPostsDataVariables { - orderBy?: any; offset?: number limit?: number where?: PostWhereInput | null; diff --git a/src/graphql/apis/index.ts b/src/graphql/apis/index.ts index b498d86dc..588c82c96 100644 --- a/src/graphql/apis/index.ts +++ b/src/graphql/apis/index.ts @@ -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 { const res = await client.query({ - 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 @@ -232,6 +232,7 @@ export async function getCommentActivities( query: q.GET_COMMENT_ACTIVITIES, variables, }) + const commentIds: string[] = [] activities.data.accountById?.posts.forEach(post => { const commentId = post?.id @@ -239,6 +240,7 @@ export async function getCommentActivities( commentIds.push(commentId) } }) + return commentIds } diff --git a/src/graphql/queries.ts b/src/graphql/queries.ts index 29ba8ceb3..ef89ad4ac 100644 --- a/src/graphql/queries.ts +++ b/src/graphql/queries.ts @@ -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 @@ -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 @@ -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 } @@ -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 } @@ -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 } diff --git a/src/rtk/features/posts/postsSlice.ts b/src/rtk/features/posts/postsSlice.ts index 271d9968d..4ef1e330a 100644 --- a/src/rtk/features/posts/postsSlice.ts +++ b/src/rtk/features/posts/postsSlice.ts @@ -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 {