MCPcopy Create free account
hub / github.com/dailydotdev/apps / useRelatedPosts

Function useRelatedPosts

packages/shared/src/hooks/post/useRelatedPosts.ts:39–112  ·  view source on GitHub ↗
({
  postId,
  relationType,
  perPage = RELATED_POSTS_PER_PAGE_DEFAULT,
}: UseRelatedPostsProps)

Source from the content-addressed store, hash-verified

37};
38
39export const useRelatedPosts = ({
40 postId,
41 relationType,
42 perPage = RELATED_POSTS_PER_PAGE_DEFAULT,
43}: UseRelatedPostsProps): UseRelatedPosts => {
44 const { requestMethod } = useRequestProtocol();
45 const queryClient = useQueryClient();
46
47 const {
48 data: relatedPosts,
49 isLoading,
50 hasNextPage,
51 fetchNextPage,
52 isFetchingNextPage,
53 } = useInfiniteQuery({
54 queryKey: generateQueryKey(RequestKey.RelatedPosts, undefined, {
55 id: postId,
56 type: relationType,
57 }),
58 queryFn: async ({ pageParam }) => {
59 const result = await requestMethod<{
60 relatedPosts: RelatedPostsQueryData;
61 }>(RELATED_POSTS_QUERY, {
62 id: postId,
63 relationType,
64 first: perPage,
65 after: pageParam,
66 });
67
68 return result.relatedPosts;
69 },
70 initialPageParam: '',
71 enabled: !!postId,
72 getNextPageParam: (lastPage) => getNextPageParam(lastPage?.pageInfo),
73 select: useCallback((data: InfiniteData<RelatedPostsQueryData>) => {
74 if (!data) {
75 return undefined;
76 }
77
78 return {
79 ...data,
80 // filter out last page with no edges returned by api paginator
81 pages: data.pages.filter(
82 (pageItem: RelatedPostsQueryData) => !!pageItem?.edges.length,
83 ),
84 };
85 }, []),
86 staleTime: StaleTime.Default,
87 initialData: () => {
88 if (relationType === PostRelationType.Collection) {
89 const postQueryData = queryClient.getQueryData<PostData>(
90 getPostByIdKey(postId),
91 );
92
93 if (postQueryData?.relatedCollectionPosts) {
94 return {
95 pages: [postQueryData.relatedCollectionPosts],
96 pageParams: [null],

Callers 2

RelatedPostsWidgetFunction · 0.90
CollectionSourcesFunction · 0.90

Calls 4

useRequestProtocolFunction · 0.90
generateQueryKeyFunction · 0.90
getNextPageParamFunction · 0.90
getPostByIdKeyFunction · 0.90

Tested by

no test coverage detected