From 79f7688703fe42ce87c92567f5bc92b6691eaa20 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Mon, 14 Feb 2022 16:56:29 +0100
Subject: chore: load recent posts on homepage during build

This way, even whithout JS, user can see the latest posts.
---
 src/pages/index.tsx | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

(limited to 'src/pages/index.tsx')

diff --git a/src/pages/index.tsx b/src/pages/index.tsx
index 0fe1a4f..55bc14c 100644
--- a/src/pages/index.tsx
+++ b/src/pages/index.tsx
@@ -2,9 +2,12 @@ import FeedIcon from '@assets/images/icon-feed.svg';
 import { ButtonLink } from '@components/Buttons';
 import { ContactIcon } from '@components/Icons';
 import Layout from '@components/Layouts/Layout';
+import { RecentPosts } from '@components/Widgets';
 import HomePageContent from '@content/pages/homepage.mdx';
+import { getPublishedPosts } from '@services/graphql/queries';
 import styles from '@styles/pages/Home.module.scss';
 import { NextPageWithLayout } from '@ts/types/app';
+import { PostsList } from '@ts/types/blog';
 import { settings } from '@utils/config';
 import { loadTranslation } from '@utils/helpers/i18n';
 import { GetStaticProps, GetStaticPropsContext } from 'next';
@@ -13,7 +16,15 @@ import type { ReactElement } from 'react';
 import { useIntl } from 'react-intl';
 import { Graph, WebPage } from 'schema-dts';
 
-const Home: NextPageWithLayout = () => {
+type HomePageProps = {
+  recentPosts: PostsList;
+};
+
+const Home: NextPageWithLayout<HomePageProps> = ({
+  recentPosts,
+}: {
+  recentPosts: PostsList;
+}) => {
   const intl = useIntl();
 
   const CodingLinks = () => {
@@ -110,11 +121,16 @@ const Home: NextPageWithLayout = () => {
     );
   };
 
+  const getRecentPosts = () => {
+    return <RecentPosts posts={recentPosts} />;
+  };
+
   const components = {
     CodingLinks: CodingLinks,
     ColdarkRepos: ColdarkRepos,
     LibreLinks: LibreLinks,
     MoreLinks: MoreLinks,
+    RecentPosts: getRecentPosts,
   };
 
   const pageTitle = intl.formatMessage(
@@ -183,9 +199,11 @@ export const getStaticProps: GetStaticProps = async (
 ) => {
   const { locale } = context;
   const translation = await loadTranslation(locale);
+  const recentPosts = await getPublishedPosts({ first: 3 });
 
   return {
     props: {
+      recentPosts,
       translation,
     },
   };
-- 
cgit v1.2.3

