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

Function useRelatedPosts

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

Source from the content-addressed store, hash-verified

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

Callers 2

CollectionSourcesFunction · 0.90
RelatedPostsWidgetFunction · 0.90

Calls 6

useRequestProtocolFunction · 0.90
generateQueryKeyFunction · 0.90
gqlBatchRequestFunction · 0.90
getNextPageParamFunction · 0.90
getPostByIdKeyFunction · 0.90
requestMethodFunction · 0.85

Tested by

no test coverage detected