Skip to content

Commit

Permalink
Refactor and increase limit of latest post ids to 7
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Mar 1, 2024
1 parent b3bd784 commit 039dd54
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/components/posts/pinned-post.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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
if (randomizedPostId) return randomizedPostId
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
Expand Down
10 changes: 6 additions & 4 deletions src/graphql/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? []
Expand Down
6 changes: 3 additions & 3 deletions src/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 039dd54

Please sign in to comment.