From baac7d6eeaf522ff5faa28906cb1200e60a19c07 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Sat, 23 Jul 2022 15:31:45 +0200
Subject: fix(article): prevent TypeError on build

By switching to custom hooks for revalidating articles and comments,
everything was working on development mode but articles failed to build
for production.
---
 src/utils/hooks/use-article.tsx | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

(limited to 'src/utils/hooks/use-article.tsx')

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>
   );
 
-- 
cgit v1.2.3

