Feeds Hooks
Feeds Hooks in Bitsocial React Hooks. Usage, options, and examples.
useFeed({communities?: CommunityIdentifier[], sortType?: string, postsPerPage?: number, filter?: CommentsFilter, newerThan?: number, accountComments?: {newerThan: number, append?: boolean, limit?: number}, modQueue: ['pendingApproval'], includeBlockedCids?: boolean}): {feed: Comment[], loadMore: function, expandTimeWindow: function, hasMore: boolean, reset: function, updatedFeed: Comment[], bufferedFeed: Comment[], communityKeysWithNewerPosts: string[], communityKeysWithMore: string[], communityKeysWithUnavailableSort: string[]}
useBufferedFeeds({feedsOptions: Omit<UseFeedOptions, "includeBlockedCids">[]}) // preload or buffer feeds in the background, so they load faster when you call `useFeed`
useFeed() and useBufferedFeeds() keep their communities updating while mounted. A feed keeps its loaded posts after it unmounts, and stops fetching more pages 2 minutes later, until it is mounted again.
useFeed().reset() clears the current feed and refreshes the latest community snapshots before rebuilding it.
useFeed().expandTimeWindow(newerThan) broadens newerThan in place without constructing a different sort name, so older posts can be appended without replacing the feed instance.
useFeed().communityKeysWithMore lists the feed’s communities that still have posts to load: not loaded yet, a stale posts cache, a page not fetched yet, or posts buffered beyond the loaded pages. It holds every community until the feed is first calculated, and again right after reset() or expandTimeWindow(), so a client that needs a feed’s complete set (to sort it locally, say) can wait until it is empty or only lists communities that failed to load.
useFeed().state is failed when the feed has no posts and every community of the feed that isn’t blocked failed to load a record non-retriably, the case where useCommunity().state is failed: its load threw, community.update() rejected, or pkc-js reported failed. A community that is only retrying, for example an unreachable one (useCommunity().firstLoadFailed), is still loading, and so is the feed. It goes back to fetching-ipns when one of its communities loads. A feed with posts, or with a community that is still loading, retrying or has loaded a record, even one without posts, is not failed. error and errors stay the feed’s own errors from loadMore(), reset() and expandTimeWindow(); read a community’s load errors from useCommunity, or from useCommunities().communityLoadStatuses for several communities. A feed only knows the communities passed to it, so an app that passes only the communities that already loaded a record gets its failed state from communityLoadStatuses instead.
Feed and reply sort names are defined by each community record, not by a fixed hooks allowlist. Omit sortType to use the first preloaded sort of the record, or discover the requestable names with getAvailablePostSortTypes(community) and getAvailableReplySortTypes(comment). A community can preload several sorts, or none: getPreloadedPostSortTypes(community) and getPreloadedReplySortTypes(comment) return every sort whose first page is embedded in the record (posts.pages / replies.pages), in the record’s key order, and the singular getPreloadedPostSortType(community) and getPreloadedReplySortType(comment) return the first of them, or undefined when nothing is preloaded. That order is not guaranteed to be the community’s configured preference: pkc-js publishes records with sorted keys, so a remote reader can see the preloaded sorts alphabetically, and an app that preloads several sorts should pick from the list rather than treat the first as the community’s default. A record that preloads nothing still loads when sortType is omitted: resolvePostSortType(community) and resolveReplySortType(comment) then fall back to the first sort published in pageCids. pkc-js only publishes pageCids once a preloaded page overflows, so when every post or reply fits in the preloaded page the standard sorts (hot, new, active and top* for posts; best, new, old, newFlat and oldFlat for replies) are also requestable and are sorted client-side from that page, including the time window of top* timeframe sorts. getPostPageSortType(community, sortType) and getReplyPageSortType(comment, sortType) return the page that serves a sort. A requested sort that is neither published nor computable client-side is not silently replaced with another sort: the result stays empty, and useFeed().communityKeysWithUnavailableSort lists the loaded communities that do not serve the requested sortType, while useReplies().sortTypeUnavailable is true when the comment publishes replies but not under the requested sortType. Neither reports a community or comment that has not loaded yet or has no posts or replies at all, so an unavailable sort can be told apart from a feed that is still loading (hasMore, communityKeysWithMore) and from an empty one. Hooks preserve protocol page order for unknown custom sorts because their scoring algorithm is not available to the client. The same applies to a sort file that a community publishes under a built-in name (for example a keyword no-bump active), which community.pageSorts.posts[sortType] reveals through a different name or options of its own: a feed reading that sort’s pages keeps the community’s page order. A feed merging several communities never reorders such a community’s posts: it interleaves the communities by the built-in sorter’s rank of each one’s next post, and the others’ posts get the built-in sort as before. newerThan on an active feed filters by lastReplyTimestamp (the post’s timestamp when it has no replies), even when the community publishes a no-bump active, so a saged reply keeps its thread in the window.
Where pinned posts and replies go is the community’s decision, not a consequence of the sort name. A community publishes each sort’s pinnedFirst option in community.pageSorts.posts[sortType].publicOptions and community.pageSorts.replies[sortType].publicOptions (pkc-js defaults it to "true", and a community that publishes no pageSorts puts pinned comments first). A sort the hooks cannot score keeps the order the community published, pinned comments included. A sort the hooks re-sort client-side puts pinned comments first unless the community published pinnedFirst: "false", in which case they are ranked like any other comment, and a top* window computed client-side applies to them too. A feed over several communities leaves out the pinned posts of communities that put them first, and keeps the ones a community ranks like any other post. useReplies reads the option from the community record in the communities store (loaded by useCommunity or a feed) each time it builds reply pages, without subscribing to that store: the option applies to reply pages built after the community record is in the store, and replies loaded earlier keep the pinned-first order until the replies feed is reset (useReplies().reset()).
Do not assume a comment is unmoderated because of the feed it arrived in. A posts feed hides removed, deleted, pending-approval and disapproved posts only by default: each exclusion is a per-sort community option (excludeRemovedComments, excludeDeletedComments, excludeCommentPendingApproval, excludeCommentWithApprovedFalse, published in community.pageSorts.posts[sortType].publicOptions), so a community can publish a posts sort that includes them, for example an archive or mod-review sort, and reply pages include removed and deleted replies by default so threads can render tombstones. The hooks do not filter on these fields. Check comment.removed and comment.deleted (also mirrored as comment.commentModeration.removed) on every comment from useFeed, useReplies, useComment and useComments before rendering its content, or pass a filter to leave such comments out.