The feed and a post
An infinite feed, filters, one post, and a members-only lock on both.
The reading side. There is no provider here: the newsfeed is hooks and helpers, and every piece of the layout is yours.
The feed
import { Parser, resolveNewsfeedPremiumGate, useMembership, useNewsfeedFeed } from '@xborglabs/fanbase-react'
export function Feed() {
const { data: membership } = useMembership()
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = useNewsfeedFeed()
if (isLoading) return <YourSkeleton count={4} />
const posts = data?.pages.flatMap((page) => page.results) ?? []
if (!posts.length) return <YourEmptyState />
return (
<>
{posts.map((post) => (
<PostCard key={post.newsId} post={post} membership={membership} />
))}
{hasNextPage && (
<button onClick={() => fetchNextPage()} disabled={isFetchingNextPage}>
Load more
</button>
)}
</>
)
}useNewsfeedFeed is an infinite query. Each page is in data.pages, and each page's posts are in results, so flatten them as above. fetchNextPage loads the next 20. An intersection observer calling it works as well as a button.
Do not pass a communityId. The hook resolves it from the slug on FanbaseProvider.
Cards render from the row you already have. Do not load a post per card. Everything a card needs is on the row.
A card
function PostCard({ post, membership }) {
const gate = resolveNewsfeedPremiumGate({ post, membership })
return (
<article className="your-post">
<header>
<img src={post.publisher.mediaPath} alt="" />
<span>{post.publisher.name}</span>
<time dateTime={post.publishedAt}>{formatDate(post.publishedAt)}</time>
</header>
{gate.locked ? (
<p>For {gate.requiredTier?.name} members. <a href="/membership">Upgrade</a></p>
) : (
<Parser content={post.description} isPlain />
)}
<a href={`/news/${post.newsId}`}>Read</a>
</article>
)
}The body is structured content, the same format as a quest description. On a card, isPlain renders a plain text excerpt. Quest descriptions covers the format.
The members-only lock
Run the gate on every card and on the post page. Both.
resolveNewsfeedPremiumGate locks a post when the fan's membership is below the tier the post asks for. When it is locked, show the lock and leave the body out. The platform has already stripped the body for that fan, so there is no body to leak, but a card that assumes one and tries to render it shows an empty space where the post should be.
Name the tier, gate.requiredTier?.name, rather than a generic "Premium". It tells the fan exactly what unlocks it.
useMembership reads the fan's membership. It is the same one the battlepass and the store use. See Membership.
Filters
Pass them to the hook. The SDK builds the request.
import { useNewsfeedFeed, useNewsfeedPublishers } from '@xborglabs/fanbase-react'
const { data: publishers = [] } = useNewsfeedPublishers()
const feed = useNewsfeedFeed({
publisherIds: selectedPublisherIds,
featured: tab === 'featured' ? true : undefined,
})| Filter | Shows |
|---|---|
publisherIds | Posts from the publishers picked |
featured: true | Featured posts only |
pinned: true | Pinned posts only |
Filters run on the server, so paging stays correct. Keep the selected tab and publishers in your URL: the SDK holds no filter state.
A pinned strip
Pinned posts already come first on every page of the feed. If you would rather show them as their own strip above it, run a second feed:
const pinned = useNewsfeedFeed({ pinned: true })
const main = useNewsfeedFeed({ pinned: false })Hide the pinned strip on a Featured tab. Otherwise a pinned post that is also featured shows up twice.
A post
import { Parser, resolveNewsfeedPremiumGate, useMembership, useNewsfeedPost } from '@xborglabs/fanbase-react'
export function PostPage({ newsId }) {
const { data: membership } = useMembership()
const { data: post, isLoading } = useNewsfeedPost({ newsId })
if (isLoading) return <YourSkeleton />
if (!post) return <YourNotFound />
const gate = resolveNewsfeedPremiumGate({ post, membership })
return (
<article className="your-post-page">
<PostHeader post={post} />
{gate.locked ? (
<UpgradePrompt tier={gate.requiredTier} />
) : (
<>
<Parser content={post.description} />
<Media images={post.images} />
<Actions actions={post.actions} />
</>
)}
<Reactions post={post} />
<Comments post={post} />
</article>
)
}Same gate as the card. Leave isPlain off here for the full render.
Media
A post's images can hold photos and videos in the same list. Tell them apart with detectNewsfeedMedia:
import { detectNewsfeedMedia } from '@xborglabs/fanbase-react'
function Media({ images = [] }) {
return images.map((image) => {
const { isVideo, isImage } = detectNewsfeedMedia(image)
if (isVideo) return <video key={image.mediaUrl} src={image.mediaUrl} controls />
if (isImage) return <img key={image.mediaUrl} src={image.mediaUrl} alt="" />
return null
})
}It decides from the file extension, and returns neither for an extension it does not know. Skip those rather than guessing.
Buttons and links
A post can carry buttons your community team added, like "Buy tickets", and a link back to where it was first posted.
import { isNewsfeedActionUrl, resolveNewsfeedSocialUrl } from '@xborglabs/fanbase-react'
post.actions?.map((action) =>
isNewsfeedActionUrl(action.value) ? (
<a key={action.value} href={action.value} target="_blank" rel="noopener noreferrer">{action.label}</a>
) : null,
)
{post.socialData && (
<a href={resolveNewsfeedSocialUrl(post.socialData)} target="_blank" rel="noopener noreferrer">
View on {post.socialData.platform}
</a>
)}Check each button with isNewsfeedActionUrl before rendering it as a link. resolveNewsfeedSocialUrl builds the right link for X, YouTube and Twitch, including telling a Twitch video from a clip.
Checklist
- The feed renders with no
communityId - Cards render from the feed row, with no request per card
- Members-only posts are locked on the card and on the post page, and name the tier
- The body goes through
Parser, plain on cards - Load more fetches the next page
- Changing a filter changes the feed, and the filter is in the URL
- A pinned strip is hidden on the Featured tab
- Videos play and photos show, and unknown files are skipped