diff --git a/src/components/chats/ChatPreview/ChatUnreadCount.tsx b/src/components/chats/ChatPreview/ChatUnreadCount.tsx index 6cb43495a..ba17cdabf 100644 --- a/src/components/chats/ChatPreview/ChatUnreadCount.tsx +++ b/src/components/chats/ChatPreview/ChatUnreadCount.tsx @@ -1,7 +1,7 @@ import useLastReadTimeFromStorage from '@/components/chats/hooks/useLastReadMessageTimeFromStorage' import useUnreadCount from '@/components/chats/hooks/useUnreadCount' import { cx } from '@/utils/class-names' -import { ComponentProps } from 'react' +import { ComponentProps, useMemo } from 'react' export type ChatUnreadCountProps = ComponentProps<'div'> & { chatId: string @@ -12,7 +12,7 @@ export default function ChatUnreadCount({ ...props }: ChatUnreadCountProps) { const { getLastReadTime } = useLastReadTimeFromStorage(chatId) - const lastReadTime = getLastReadTime() ?? '' + const lastReadTime = useMemo(() => getLastReadTime() ?? '', [getLastReadTime]) const unreadCount = useUnreadCount(chatId, lastReadTime) if (unreadCount <= 0) return null diff --git a/src/services/subsocial/datahub/generated-query.ts b/src/services/subsocial/datahub/generated-query.ts index f7c49d9d3..25ca6716e 100644 --- a/src/services/subsocial/datahub/generated-query.ts +++ b/src/services/subsocial/datahub/generated-query.ts @@ -466,6 +466,7 @@ export type GetPostsQueryVariables = Exact<{ ids?: InputMaybe< Array | Scalars['String']['input'] > + pageSize: Scalars['Int']['input'] }> export type GetPostsQuery = { @@ -758,7 +759,7 @@ export const DatahubPostFragment = gql` } ` export const GetPosts = gql` - query GetPosts($ids: [String!]) { + query GetPosts($ids: [String!], $pageSize: Int!) { findPosts(where: { persistentIds: $ids }) { data { ...DatahubPostFragment diff --git a/src/services/subsocial/datahub/posts/fetcher.ts b/src/services/subsocial/datahub/posts/fetcher.ts index dfadd7378..6e0a4f140 100644 --- a/src/services/subsocial/datahub/posts/fetcher.ts +++ b/src/services/subsocial/datahub/posts/fetcher.ts @@ -1,5 +1,6 @@ import { PostData } from '@subsocial/api/types' import { gql } from 'graphql-request' +import { getPostsFollowersCountFromSquid } from '../../posts/fetcher' import { GetOptimisticPostsQuery, GetOptimisticPostsQueryVariables, @@ -86,8 +87,8 @@ export const DATAHUB_POST_FRAGMENT = gql` const GET_POSTS = gql` ${DATAHUB_POST_FRAGMENT} - query GetPosts($ids: [String!]) { - findPosts(where: { persistentIds: $ids }) { + query GetPosts($ids: [String!], $pageSize: Int!) { + findPosts(where: { persistentIds: $ids, pageSize: $pageSize }) { data { ...DatahubPostFragment } @@ -124,15 +125,25 @@ export async function getPostsFromDatahub(postIds: string[]) { let optimisticPosts: PostData[] = [] if (persistentIds.length > 0) { - const res = await datahubQueryRequest< - GetPostsQuery, - GetPostsQueryVariables - >({ - document: GET_POSTS, - variables: { ids: persistentIds }, - }) - persistentPosts = res.findPosts.data.map((post) => { + const [datahubResPromise, squidResPromise] = await Promise.allSettled([ + datahubQueryRequest({ + document: GET_POSTS, + variables: { ids: persistentIds, pageSize: persistentIds.length }, + }), + getPostsFollowersCountFromSquid(persistentIds), + ] as const) + const followersCountMap = new Map() + if (squidResPromise.status === 'fulfilled') { + squidResPromise.value.forEach((post) => { + followersCountMap.set(post.id, post.followersCount) + }) + } + if (datahubResPromise.status !== 'fulfilled') { + throw new Error(datahubResPromise.reason) + } + persistentPosts = datahubResPromise.value.findPosts.data.map((post) => { post.id = post.persistentId || post.id + post.followersCount = followersCountMap.get(post.id) || 0 return { ...mapDatahubPostFragment(post), requestedId: post.id } }) } diff --git a/src/services/subsocial/posts/fetcher.ts b/src/services/subsocial/posts/fetcher.ts index 507202d2d..73bcda680 100644 --- a/src/services/subsocial/posts/fetcher.ts +++ b/src/services/subsocial/posts/fetcher.ts @@ -2,7 +2,12 @@ import { SubsocialQueryData } from '@/subsocial-query/subsocial/query' import { gql } from 'graphql-request' import { getPostsFromDatahub } from '../datahub/posts/fetcher' import { POST_FRAGMENT } from '../squid/fragments' -import { GetPostsQuery, GetPostsQueryVariables } from '../squid/generated' +import { + GetPostsFollowersCountQuery, + GetPostsFollowersCountQueryVariables, + GetPostsQuery, + GetPostsQueryVariables, +} from '../squid/generated' import { mapPostFragment } from '../squid/mappers' import { squidRequest } from '../squid/utils' import { standaloneDynamicFetcherWrapper } from '../utils' @@ -38,3 +43,23 @@ export const getPostsFromSubsocial = standaloneDynamicFetcherWrapper({ squid: getPostsFromSquid, datahub: getPostsFromDatahub, }) + +const GET_POSTS_FOLLOWERS_COUNT = gql` + query GetPostsFollowersCount($ids: [String!]) { + posts(where: { id_in: $ids }) { + id + followersCount + } + } +` +export async function getPostsFollowersCountFromSquid(postIds: string[]) { + if (postIds.length === 0) return [] + const res = await squidRequest< + GetPostsFollowersCountQuery, + GetPostsFollowersCountQueryVariables + >({ + document: GET_POSTS_FOLLOWERS_COUNT, + variables: { ids: postIds }, + }) + return res.posts +} diff --git a/src/services/subsocial/squid/generated.ts b/src/services/subsocial/squid/generated.ts index d9297bb56..44b99870e 100644 --- a/src/services/subsocial/squid/generated.ts +++ b/src/services/subsocial/squid/generated.ts @@ -5870,14 +5870,6 @@ export type WhereIdInput = { id: Scalars['String']['input']; }; -export type GetMessagesCountAfterTimeQueryVariables = Exact<{ - chatId: Scalars['String']['input']; - time: Scalars['DateTime']['input']; -}>; - - -export type GetMessagesCountAfterTimeQuery = { __typename?: 'Query', postsConnection: { __typename?: 'PostsConnection', totalCount: number } }; - export type GetEvmAddressesQueryVariables = Exact<{ addresses?: InputMaybe | Scalars['String']['input']>; }>; @@ -5892,6 +5884,13 @@ export type GetPostsQueryVariables = Exact<{ export type GetPostsQuery = { __typename?: 'Query', posts: Array<{ __typename?: 'Post', content?: string | null, createdAtBlock?: any | null, createdAtTime?: any | null, title?: string | null, body?: string | null, summary?: string | null, isShowMore?: boolean | null, image?: string | null, link?: string | null, downvotesCount: number, hidden: boolean, id: string, isComment: boolean, kind?: PostKind | null, repliesCount: number, sharesCount: number, upvotesCount: number, updatedAtTime?: any | null, inReplyToKind?: InReplyToKind | null, followersCount: number, canonical?: string | null, tagsOriginal?: string | null, createdByAccount: { __typename?: 'Account', id: string }, inReplyToPost?: { __typename?: 'Post', id: string } | null, ownedByAccount: { __typename?: 'Account', id: string }, space?: { __typename?: 'Space', id: string } | null, rootPost?: { __typename?: 'Post', id: string, space?: { __typename?: 'Space', id: string } | null } | null, sharedPost?: { __typename?: 'Post', id: string } | null, extensions: Array<{ __typename?: 'ContentExtension', image?: string | null, amount?: any | null, chain?: string | null, collectionId?: string | null, decimals?: number | null, extensionSchemaId: ContentExtensionSchemaId, id: string, nftId?: string | null, token?: string | null, txHash?: string | null, message?: string | null, nonce?: string | null, url?: string | null, recipient?: { __typename?: 'Account', id: string } | null, fromEvm?: { __typename?: 'EvmAccount', id: string } | null, toEvm?: { __typename?: 'EvmAccount', id: string } | null, pinnedResources: Array<{ __typename?: 'ExtensionPinnedResource', post?: { __typename?: 'Post', id: string } | null }> }> }> }; +export type GetPostsFollowersCountQueryVariables = Exact<{ + ids?: InputMaybe | Scalars['String']['input']>; +}>; + + +export type GetPostsFollowersCountQuery = { __typename?: 'Query', posts: Array<{ __typename?: 'Post', id: string, followersCount: number }> }; + export type GetPostsByContentQueryVariables = Exact<{ search: Scalars['String']['input']; spaceIds: Array | Scalars['String']['input']; @@ -6029,16 +6028,6 @@ export const PostFragment = gql` } } `; -export const GetMessagesCountAfterTime = gql` - query GetMessagesCountAfterTime($chatId: String!, $time: DateTime!) { - postsConnection( - where: {createdAtTime_gt: $time, rootPost: {id_eq: $chatId}} - orderBy: id_ASC - ) { - totalCount - } -} - `; export const GetEvmAddresses = gql` query getEvmAddresses($addresses: [String!]) { evmSubstrateAccountLinks( @@ -6060,6 +6049,14 @@ export const GetPosts = gql` } } ${PostFragment}`; +export const GetPostsFollowersCount = gql` + query GetPostsFollowersCount($ids: [String!]) { + posts(where: {id_in: $ids}) { + id + followersCount + } +} + `; export const GetPostsByContent = gql` query GetPostsByContent($search: String!, $spaceIds: [String!]!, $postIds: [String!]!) { posts(