summaryrefslogtreecommitdiffstats
path: root/src/components/PostsList/PostsList.tsx
blob: 16deee31ed573690d067ebc6dd0b913acfafb104 (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
import PostPreview from '@components/PostPreview/PostPreview';
import { PostsList as PostsListData } from '@ts/types/blog';
import { sortPostsByYear } from '@utils/helpers/sort';
import { ForwardedRef, forwardRef, Fragment } from 'react';
import { useIntl } from 'react-intl';
import styles from './PostsList.module.scss';

const PostsList = (
  {
    data,
    showYears,
  }: {
    data: PostsListData[];
    showYears: boolean;
  },
  ref: ForwardedRef<HTMLSpanElement>
) => {
  const intl = useIntl();
  const titleLevel = showYears ? 3 : 2;

  const getPostsListByYear = () => {
    const posts = sortPostsByYear(data);
    const years = Object.keys(posts).reverse();

    const getLastPostId = () => {
      const oldestYear = Object.keys(posts)[0];
      const lastPost = posts[oldestYear][posts[oldestYear].length - 1];
      return lastPost.id;
    };

    return years.map((year) => {
      return (
        <section key={year} className={styles.section}>
          {showYears && (
            <h2 className={styles.year}>
              <span className="screen-reader-text">
                {intl.formatMessage({
                  defaultMessage: 'Published on',
                  description: 'PostsList: published on year label',
                })}{' '}
              </span>
              {year}
            </h2>
          )}
          <ol className={styles.list}>
            {posts[year].map((post) => {
              const isLastPost = post.id === getLastPostId();
              return (
                <Fragment key={post.id}>
                  <li className={styles.item}>
                    <PostPreview post={post} titleLevel={titleLevel} />
                  </li>
                  {isLastPost && <span ref={ref} tabIndex={-1} />}
                </Fragment>
              );
            })}
          </ol>
        </section>
      );
    });
  };

  const getPostsList = () => {
    return data.map((page) => {
      const getLastPostId = () => {
        const lastPost = page.posts[page.posts.length - 1];
        return lastPost.id;
      };

      if (page.posts.length === 0) {
        return (
          <p key="no-result">
            {intl.formatMessage({
              defaultMessage: 'No results found.',
              description: 'PostsList: no results',
            })}
          </p>
        );
      } else {
        return (
          <Fragment key={page.pageInfo.endCursor}>
            <ol className={styles.list}>
              {page.posts.map((post) => {
                const isLastPost = post.id === getLastPostId();
                return (
                  <Fragment key={post.id}>
                    <li key={post.id} className={styles.item}>
                      <PostPreview post={post} titleLevel={titleLevel} />
                    </li>
                    {isLastPost && <span ref={ref} tabIndex={-1} />}
                  </Fragment>
                );
              })}
            </ol>
          </Fragment>
        );
      }
    });
  };

  return <div>{showYears ? getPostsListByYear() : getPostsList()}</div>;
};

export default forwardRef(PostsList);