From dcd2a7ab382fece8e0ae2979aad4a180b6a105e1 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 16 Nov 2023 18:16:09 +0100 Subject: build(components, hooks): fix type errors introduced by deps upgrade Since #7e37f2b Typescript was complaining about some types. --- src/utils/hooks/use-article.ts | 37 +++++++++++++++++++++++++++++++++++++ src/utils/hooks/use-article.tsx | 36 ------------------------------------ src/utils/hooks/use-comments.ts | 23 +++++++++++++++++++++++ src/utils/hooks/use-comments.tsx | 23 ----------------------- 4 files changed, 60 insertions(+), 59 deletions(-) create mode 100644 src/utils/hooks/use-article.ts delete mode 100644 src/utils/hooks/use-article.tsx create mode 100644 src/utils/hooks/use-comments.ts delete mode 100644 src/utils/hooks/use-comments.tsx (limited to 'src/utils/hooks') diff --git a/src/utils/hooks/use-article.ts b/src/utils/hooks/use-article.ts new file mode 100644 index 0000000..5cf0e51 --- /dev/null +++ b/src/utils/hooks/use-article.ts @@ -0,0 +1,37 @@ +import useSWR from 'swr'; +import { + articleBySlugQuery, + fetchAPI, + getArticleFromRawData, +} from '../../services/graphql'; +import type { Article, RawArticle } from '../../types'; + +export type UseArticleConfig = { + /** + * A fallback article + */ + fallback?: Article; + /** + * The article slug + */ + slug?: string; +}; + +/** + * Retrieve an article by slug. + * + * @param {UseArticleConfig} config - The config. + * @returns {Article|undefined} The matching article if it exists. + */ +export const useArticle = ({ + slug, + fallback, +}: UseArticleConfig): Article | undefined => { + const { data } = useSWR( + slug ? { query: articleBySlugQuery, variables: { slug } } : null, + fetchAPI, + {} + ); + + return data ? getArticleFromRawData(data.post) : fallback; +}; diff --git a/src/utils/hooks/use-article.tsx b/src/utils/hooks/use-article.tsx deleted file mode 100644 index 86d8e38..0000000 --- a/src/utils/hooks/use-article.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import useSWR from 'swr'; -import { - articleBySlugQuery, - fetchAPI, - getArticleFromRawData, -} from '../../services/graphql'; -import { type Article, type RawArticle } from '../../types'; - -export type UseArticleConfig = { - /** - * A fallback article - */ - fallback?: Article; - /** - * The article slug - */ - slug?: string; -}; - -/** - * Retrieve an article by slug. - * - * @param {UseArticleConfig} config - The config. - * @returns {Article|undefined} The matching article if it exists. - */ -export const useArticle = ({ - slug, - fallback, -}: UseArticleConfig): Article | undefined => { - const { data } = useSWR( - slug ? { query: articleBySlugQuery, variables: { slug } } : null, - fetchAPI - ); - - return data ? getArticleFromRawData(data.post) : fallback; -}; 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; +}; diff --git a/src/utils/hooks/use-comments.tsx b/src/utils/hooks/use-comments.tsx deleted file mode 100644 index 6ac3d42..0000000 --- a/src/utils/hooks/use-comments.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import useSWR from 'swr'; -import { getAllComments } from '../../services/graphql'; -import { 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; -}; -- cgit v1.2.3