-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pinned post to use randomized last 3 community highlights if not …
…provided
- Loading branch information
1 parent
8a59b3f
commit 22c2371
Showing
7 changed files
with
65 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { getLast3CommunityHighlights } 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)] | ||
|
||
// precalculate the randomized index to avoid having different pinned post on multiple calls | ||
const randomizedIndex = Math.floor(Math.random() * 3) | ||
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 randomIndex = Math.min(randomizedIndex, postIds.length - 1) | ||
console.log(postIds, randomIndex, postIds[randomIndex]) | ||
randomizedPostId = postIds[randomIndex] | ||
return randomizedPostId | ||
} catch (err) { | ||
console.log('Error getting community highlights', err) | ||
return null | ||
} | ||
} | ||
|
||
export function isPinnedPost(postId: string) { | ||
return postId === PINNED_POST_ID || postId === randomizedPostId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters