diff options
Diffstat (limited to 'src/utils/hooks')
| -rw-r--r-- | src/utils/hooks/use-article.tsx | 16 | ||||
| -rw-r--r-- | src/utils/hooks/use-comments.tsx | 17 |
2 files changed, 25 insertions, 8 deletions
diff --git a/src/utils/hooks/use-article.tsx b/src/utils/hooks/use-article.tsx index 3a173dd..6281a54 100644 --- a/src/utils/hooks/use-article.tsx +++ b/src/utils/hooks/use-article.tsx @@ -5,16 +5,26 @@ import { Article } from '@ts/types/app'; import { RawArticle } from '@ts/types/raw-data'; import useSWR from 'swr'; +export type UseArticleConfig = { + fallback?: Article; + slug?: string; +}; + /** * Retrieve an article by slug. * * @param {string} slug - The article slug. * @param {Article} fallback - A fallback article. - * @returns {Article} The matching article. + * @returns {Article|undefined} The matching article if it exists. */ -const useArticle = (slug: string, fallback: Article): Article => { +const useArticle = ({ + slug, + fallback, +}: UseArticleConfig): Article | undefined => { const { data } = useSWR( - { api: getAPIUrl(), query: articleBySlugQuery, variables: { slug } }, + slug + ? { api: getAPIUrl(), query: articleBySlugQuery, variables: { slug } } + : null, fetchAPI<RawArticle, typeof articleBySlugQuery> ); diff --git a/src/utils/hooks/use-comments.tsx b/src/utils/hooks/use-comments.tsx index f430f19..9076888 100644 --- a/src/utils/hooks/use-comments.tsx +++ b/src/utils/hooks/use-comments.tsx @@ -8,18 +8,25 @@ import { Comment } from '@ts/types/app'; import { RawComment } from '@ts/types/raw-data'; import useSWR from 'swr'; +export type UseCommentsConfig = { + contentId?: string | number; + fallback?: Comment[]; +}; + /** * Retrieve the comments of a page/article. * * @param {string | number} contentId - A page/article id. * @returns {Comment[]|undefined} */ -const useComments = ( - contentId: string | number, - fallback?: Comment[] -): Comment[] | undefined => { +const useComments = ({ + contentId, + fallback, +}: UseCommentsConfig): Comment[] | undefined => { const { data } = useSWR( - { api: getAPIUrl(), query: commentsQuery, variables: { contentId } }, + contentId + ? { api: getAPIUrl(), query: commentsQuery, variables: { contentId } } + : null, fetchAPI<RawComment, typeof commentsQuery> ); |
