Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and increase limit of latest post ids to 7 #304

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading