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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
import {
type ForwardRefRenderFunction,
forwardRef,
type ReactNode,
} from 'react';
import { useIntl } from 'react-intl';
import type { PageLink } from '../../../types';
import { getReadingTimeFrom } from '../../../utils/helpers';
import { Header, Heading, type HeaderProps, Link, Time } from '../../atoms';
import { MetaList, MetaItem } from '../../molecules';
import styles from './page.module.scss';
export type PageHeaderMetaData = {
author: string;
publicationDate: string;
thematics: PageLink[];
total: number;
updateDate: string;
website: string;
wordsCount: number;
};
export type PageHeaderProps = Omit<HeaderProps, 'children'> & {
/**
* The page main title.
*/
heading: ReactNode;
/**
* The page introduction.
*/
intro?: ReactNode;
/**
* The page meta.
*/
meta?: Partial<PageHeaderMetaData>;
};
const PageHeaderWithRef: ForwardRefRenderFunction<
HTMLElement,
PageHeaderProps
> = ({ className = '', heading, intro, meta, ...props }, ref) => {
const headerClass = `${styles.header} ${className}`;
const intl = useIntl();
return (
<Header {...props} className={headerClass} ref={ref}>
<div className={styles.header__body}>
<Heading className={styles.heading} level={1}>
{heading}
</Heading>
{meta ? (
<MetaList className={styles.meta}>
{meta.author ? (
<MetaItem
isInline
label={intl.formatMessage({
defaultMessage: 'Written by:',
description: 'PageHeader: author meta label',
id: '/unaGZ',
})}
value={meta.author}
/>
) : null}
{meta.publicationDate ? (
<MetaItem
isInline
label={intl.formatMessage({
defaultMessage: 'Published on:',
description: 'PageHeader: publication date label',
id: 'pUBhKy',
})}
value={<Time date={meta.publicationDate} />}
/>
) : null}
{meta.updateDate && meta.updateDate !== meta.publicationDate ? (
<MetaItem
isInline
label={intl.formatMessage({
defaultMessage: 'Updated on:',
description: 'PageHeader: update date label',
id: 'sR5hah',
})}
value={<Time date={meta.updateDate} />}
/>
) : null}
{meta.wordsCount ? (
<MetaItem
isInline
label={intl.formatMessage({
defaultMessage: 'Reading time:',
description: 'PageHeader: reading time label',
id: 'jJm8wd',
})}
value={intl.formatMessage(
{
defaultMessage:
'{minutesCount, plural, =0 {Less than one minute} one {# minute} other {# minutes}}',
description: 'PageHeader: rounded minutes count',
id: 'NNDqRg',
},
{
minutesCount: getReadingTimeFrom(
meta.wordsCount
).inMinutes(),
}
)}
/>
) : null}
{meta.thematics ? (
<MetaItem
isInline
label={intl.formatMessage(
{
defaultMessage:
'{thematicsCount, plural, =0 {Thematics:} one {Thematic:} other {Thematics:}}',
description: 'PageHeader: thematics label',
id: 'ODwkBI',
},
{ thematicsCount: meta.thematics.length }
)}
value={meta.thematics.map((thematic) => {
return {
id: `thematic-${thematic.id}`,
value: <Link href={thematic.url}>{thematic.name}</Link>,
};
})}
/>
) : null}
{meta.total ? (
<MetaItem
isInline
label={intl.formatMessage({
defaultMessage: 'Total:',
description: 'PageHeader: total meta label',
id: 'a6DzIj',
})}
value={intl.formatMessage(
{
defaultMessage:
'{postsCount, plural, =0 {No posts} one {# post} other {# posts}}',
description: 'PageHeader: total meta value',
id: 'bAXtMT',
},
{ postsCount: meta.total }
)}
/>
) : null}
{meta.website ? (
<MetaItem
isInline
label={intl.formatMessage({
defaultMessage: 'Website:',
description: 'PageHeader: website meta label',
id: '9jh0r2',
})}
value={<Link href={meta.website}>{meta.website}</Link>}
/>
) : null}
</MetaList>
) : null}
{typeof intro === 'string' ? (
// eslint-disable-next-line react/no-danger -- Intro can contain tags.
<div dangerouslySetInnerHTML={{ __html: intro }} />
) : (
intro
)}
</div>
</Header>
);
};
export const PageHeader = forwardRef(PageHeaderWithRef);
|