summaryrefslogtreecommitdiffstats
path: root/src/pages/blog/index.tsx
blob: 48fab1c79a51fffd6d6a309690a2a1118e360436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { GetStaticProps } from 'next';
import Head from 'next/head';
import { t } from '@lingui/macro';
import { getLayout } from '@components/Layouts/Layout';
import { seo } from '@config/seo';
import { config } from '@config/website';
import { NextPageWithLayout } from '@ts/types/app';
import { BlogPageProps, PostsList as PostsListData } from '@ts/types/blog';
import { loadTranslation } from '@utils/helpers/i18n';
import PostsList from '@components/PostsList/PostsList';
import useSWRInfinite from 'swr/infinite';
import { Button } from '@components/Buttons';
import { getPublishedPosts } from '@services/graphql/queries';
import PostHeader from '@components/PostHeader/PostHeader';
import { ThematicsList, TopicsList } from '@components/Widgets';
import Sidebar from '@components/Sidebar/Sidebar';
import styles from '@styles/pages/Page.module.scss';
import { useRef } from 'react';
import Spinner from '@components/Spinner/Spinner';

const Blog: NextPageWithLayout<BlogPageProps> = ({ fallback }) => {
  const lastPostRef = useRef<HTMLSpanElement>(null);

  const getKey = (pageIndex: number, previousData: PostsListData) => {
    if (previousData && !previousData.posts) return null;

    return pageIndex === 0
      ? { first: config.postsPerPage }
      : {
          first: config.postsPerPage,
          after: previousData.pageInfo.endCursor,
        };
  };

  const { data, error, size, setSize } = useSWRInfinite(
    getKey,
    getPublishedPosts,
    { fallback }
  );

  const isLoadingInitialData = !data && !error;
  const isLoadingMore: boolean =
    isLoadingInitialData ||
    (size > 0 && data !== undefined && typeof data[size - 1] === 'undefined');

  const hasNextPage = data && data[data.length - 1].pageInfo.hasNextPage;

  const loadMorePosts = () => {
    if (lastPostRef.current) {
      lastPostRef.current.focus();
    }
    setSize(size + 1);
  };

  const getPostsList = () => {
    if (error) return t`Failed to load.`;
    if (!data) return <Spinner />;

    return <PostsList ref={lastPostRef} data={data} showYears={true} />;
  };

  return (
    <>
      <Head>
        <title>{seo.blog.title}</title>
        <meta name="description" content={seo.blog.description} />
      </Head>
      <article
        className={`${styles.article} ${styles['article--no-comments']}`}
      >
        <PostHeader title={t`Blog`} />
        <div className={styles.body}>
          {getPostsList()}
          {hasNextPage && (
            <Button
              isDisabled={isLoadingMore}
              clickHandler={loadMorePosts}
              position="center"
            >{t`Load more?`}</Button>
          )}
        </div>
        <Sidebar position="right" title={t`Filter by`}>
          <ThematicsList title={t`Thematics`} />
          <TopicsList title={t`Topics`} />
        </Sidebar>
      </article>
    </>
  );
};

Blog.getLayout = getLayout;

export const getStaticProps: GetStaticProps = async (context) => {
  const translation = await loadTranslation(
    context.locale!,
    process.env.NODE_ENV === 'production'
  );
  const data = await getPublishedPosts({ first: config.postsPerPage });
  const breadcrumbTitle = t`Blog`;

  return {
    props: {
      breadcrumbTitle,
      fallback: {
        '/api/posts': data,
      },
      translation,
    },
  };
};

export default Blog;