aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-comments.tsx
blob: a695bd729d208cfe214547ad37ed0d0fbf0292e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { getAllComments } from '@services/graphql/comments';
import { SingleComment } from '@ts/types/app';
import useSWR from 'swr';

export type UseCommentsConfig = {
  contentId?: string | number;
  fallback?: SingleComment[];
};

/**
 * Retrieve the comments of a page/article.
 *
 * @param {string | number} contentId - A page/article id.
 * @returns {SingleComment[]|undefined}
 */
const useComments = ({
  contentId,
  fallback,
}: UseCommentsConfig): SingleComment[] | undefined => {
  const { data } = useSWR(contentId ? { contentId } : null, getAllComments);

  return data || fallback;
};

export default useComments;