blob: df9dfe403fd500805f8878b569a14613d1ea15da (
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
|
import { t } from '@lingui/macro';
import { PostsList as PostsListData } from '@ts/types/blog';
import styles from './PostsList.module.scss';
import PostPreview from '@components/PostPreview/PostPreview';
import { ForwardedRef, forwardRef, Fragment } from 'react';
import { sortPostsByYear } from '@utils/helpers/sort';
const PostsList = (
{
data,
showYears,
}: {
data: PostsListData[];
showYears: boolean;
},
ref: ForwardedRef<HTMLSpanElement>
) => {
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">{t`Published in`} </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">{t`No results found.`}</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);
|