diff options
Diffstat (limited to 'src/utils/hooks/use-comments.ts')
| -rw-r--r-- | src/utils/hooks/use-comments.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils/hooks/use-comments.ts b/src/utils/hooks/use-comments.ts new file mode 100644 index 0000000..ac723e9 --- /dev/null +++ b/src/utils/hooks/use-comments.ts @@ -0,0 +1,23 @@ +import useSWR from 'swr'; +import { getAllComments } from '../../services/graphql'; +import type { SingleComment } from '../../types'; + +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} + */ +export const useComments = ({ + contentId, + fallback, +}: UseCommentsConfig): SingleComment[] | undefined => { + const { data } = useSWR(contentId ? { contentId } : null, getAllComments, {}); + + return data ?? fallback; +}; |
