useFeedData

Overview

The useFeedData hook provides a powerful and flexible solution for managing and presenting entity data in a feed. Unlike individual hooks like useFetchEntities or useCreateEntity, this hook combines functionalities such as fetching, creating, deleting, and managing entities while offering advanced filter customizations and state management. It is ideal for implementing dynamic feeds with tailored sorting and filtering options.


Usage Example

import { useFeedData } from "@replyke/react-js";
 
function Feed() {
  const {
    entities,
    infusedEntities,
    loading,
    hasMore,
    sortBy,
    setSortBy,
    loadMore,
    resetEntities,
    createEntity,
    deleteEntity,
  } = useFeedData({ limit: 10, sortBy: "hot" });
 
  const handleCreateEntity = async () => {
    try {
      await createEntity({
        title: "New Post",
        content: "This is a new post content",
      });
    } catch (error) {
      console.error("Failed to create entity:", error.message);
    }
  };
 
  return (
    <div>
      <select
        value={sortBy}
        onChange={(e) => setSortBy(e.target.value as "hot" | "new")}
      >
        <option value="hot">Hot</option>
        <option value="new">New</option>
      </select>
 
      <ul>
        {entities.map((entity) => (
          <li key={entity.id}>{entity.title}</li>
        ))}
      </ul>
 
      {loading && <p>Loading...</p>}
      {hasMore && !loading && (
        <button onClick={loadMore}>Load More</button>
      )}
 
      <button onClick={handleCreateEntity}>Create New Entity</button>
    </div>
  );
}

Parameters & Returns

Parameters

The hook accepts an object with the following fields:

ParameterTypeRequiredDescription
limitnumberNoThe number of entities to fetch per page. Default is 10.
followedOnlybooleanNoIf true, fetches entities from followed users only. Default is false.
sortByFeedSortByOptionsNoThe default sorting criteria for the feed (e.g., hot, new).
timeFrameTimeFrameNoThe time range for fetching entities.
keywordsFiltersKeywordsFiltersNoFilters entities based on keywords.
titleFiltersTitleFiltersNoFilters entities based on titles.
contentFiltersContentFiltersNoFilters entities based on content.
mediaFiltersMediaFiltersNoFilters entities based on media properties.
locationFiltersLocationFiltersNoFilters entities based on location.
metadataFiltersMetadataFiltersNoFilters entities based on metadata.
userIdstring | nullNoFilters entities created by a specific user.
idlebooleanNoIf true, the hook starts in an idle state and fetches entities only when activated.
onReset() => voidNoCallback executed when the feed is reset.
infuseData(referenceId: string) => Promise<Record<string, any> | null>NoA function to fetch and add additional data to each entity.

Returns

The hook returns an object with the following fields:

Return ValueTypeDescription
entitiesEntity[]The flat list of entities fetched so far.
infusedEntities(Entity & Record<string, any>)[]The list of entities infused with additional data (if infuseData is provided).
loadingbooleanIndicates whether entities are being loaded.
hasMorebooleanIndicates whether there are more entities to fetch.
resettingbooleanIndicates whether the feed is resetting.
sortByFeedSortByOptions | nullThe current sorting criteria for the feed.
setSortBy(sortBy: FeedSortByOptions) => voidA function to update the sorting criteria.
timeFrameTimeFrame | nullThe current time range filter for the feed.
setTimeFrame(timeFrame: TimeFrame | null) => voidA function to update the time range filter.
followedOnlybooleanIndicates whether only followed users’ entities are fetched.
setFollowedOnly(state: boolean) => voidA function to toggle the followedOnly filter.
userIdstring | nullThe current user ID filter.
setUserId(userId: string | null) => voidA function to update the user ID filter.
keywordsFiltersKeywordsFilters | nullThe current keywords filter.
updateKeywordsFilters(type: "add" | "remove" | "reset" | "replace", key: "includes" | "doesNotInclude" | "both", value?: string | string[]) => voidA function to modify the keywords filter.
titleFiltersTitleFilters | nullThe current title filters.
setTitleFilters(newTitleFilters: TitleFilters | null) => voidA function to update the title filters.
contentFiltersContentFilters | nullThe current content filters.
setContentFilters(newContentFilters: ContentFilters | null) => voidA function to update the content filters.
mediaFiltersMediaFilters | nullThe current media filters.
setMediaFilters(newMediaFilters: MediaFilters | null) => voidA function to update the media filters.
locationFiltersLocationFilters | nullThe current location filters.
setLocationFilters(location: LocationFilters | null) => voidA function to update the location filters.
metadataFiltersMetadataFilters | nullThe current metadata filters.
setMetadataFilters(newMetadataFilters: MetadataFilters | null) => voidA function to update the metadata filters.
kickstart() => voidA function to activate the feed if it starts in an idle state.
loadMore() => voidA function to fetch the next page of entities.
resetEntities() => Promise<void>A function to reset the feed and fetch the first page of entities.
createEntity(props: { referenceId?: string; title?: string; content?: string; media?: any[]; keywords?: string[]; location?: { latitude: number; longitude: number }; metadata?: Record<string, any>; insertPosition?: "first" | "last" }) => Promise<void>A function to create a new entity and optionally insert it into the feed.
deleteEntity(props: { entityId: string }) => Promise<void>A function to delete an entity from the feed.