diff --git a/src/components/posts/pinned-post.ts b/src/components/posts/pinned-post.ts index f40f1db07..ca408f355 100644 --- a/src/components/posts/pinned-post.ts +++ b/src/components/posts/pinned-post.ts @@ -1,11 +1,14 @@ -import { getLast3CommunityHighlights } from 'src/graphql/apis' +import { getLastestPostIdsInSpace } from 'src/graphql/apis' import { GqlClient } from 'src/graphql/ApolloProvider' const PINNED_POST_IDS: string[] = [] const PINNED_POST_ID = PINNED_POST_IDS[Math.floor(Math.random() * PINNED_POST_IDS.length)] +const COMMUNITY_SPACE_ID = '1244' +const LATEST_LIMIT = 7 + // precalculate the randomized index to avoid having different pinned post on multiple calls -const randomizedIndex = Math.floor(Math.random() * 3) +const randomizedIndex = Math.floor(Math.random() * LATEST_LIMIT) let randomizedPostId: string | null = null export async function getPinnedPost(client: GqlClient | undefined) { if (PINNED_POST_ID) return PINNED_POST_ID @@ -13,7 +16,10 @@ export async function getPinnedPost(client: GqlClient | undefined) { if (!client) return null try { - const postIds = await getLast3CommunityHighlights(client) + const postIds = await getLastestPostIdsInSpace(client, { + limit: LATEST_LIMIT, + spaceId: COMMUNITY_SPACE_ID, + }) const randomIndex = Math.min(randomizedIndex, postIds.length - 1) randomizedPostId = postIds[randomIndex] return randomizedPostId diff --git a/src/graphql/apis/index.ts b/src/graphql/apis/index.ts index f453ada9b..88b4e5c92 100644 --- a/src/graphql/apis/index.ts +++ b/src/graphql/apis/index.ts @@ -395,11 +395,13 @@ export async function searchResults(client: GqlClient, variables: GetSearchResul return hits } -const COMMUNITY_SPACE_ID = '1244' -export async function getLast3CommunityHighlights(client: GqlClient) { +export async function getLastestPostIdsInSpace( + client: GqlClient, + variables: { spaceId: string; limit: number }, +) { const res = await client.query<{ posts: { id: string }[] }, { spaceId: string }>({ - query: q.GET_LAST_3_COMMUNITY_HIGHLIGHTS, - variables: { spaceId: COMMUNITY_SPACE_ID }, + query: q.GET_LASTEST_POST_IDS_IN_SPACE, + variables, }) const posts = res.data.posts ?? [] diff --git a/src/graphql/queries.ts b/src/graphql/queries.ts index 52cee65ef..340422986 100644 --- a/src/graphql/queries.ts +++ b/src/graphql/queries.ts @@ -787,9 +787,9 @@ export const GET_SEARCH_RESULTS = gql` // Community Highlights // ------------------------------------------------------------------------------------ -export const GET_LAST_3_COMMUNITY_HIGHLIGHTS = gql` - query GetLast3CommunityHighlights($spaceId: String!) { - posts(limit: 3, orderBy: createdAtTime_DESC, where: { space: { id_eq: $spaceId } }) { +export const GET_LASTEST_POST_IDS_IN_SPACE = gql` + query GetLatestPostIdsInSpace($spaceId: String!, $limit: Int!) { + posts(limit: $limit, orderBy: createdAtTime_DESC, where: { space: { id_eq: $spaceId } }) { id } }