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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
import Link from '@components/atoms/links/link';
import DescriptionList, {
type DescriptionListProps,
type DescriptionListItem,
} from '@components/atoms/lists/description-list';
import { getFormattedDate, getFormattedTime } from '@utils/helpers/dates';
import { FC, ReactNode } from 'react';
import { useIntl } from 'react-intl';
export type CustomMeta = {
label: string;
value: ReactNode | ReactNode[];
};
export type MetaComments = {
/**
* The comments count.
*/
count: number;
/**
* Wrap the comments count with a link to the given target.
*/
target?: string;
};
export type MetaDate = {
/**
* A date string. Ex: `2022-04-30`.
*/
date: string;
/**
* A time string. Ex: `10:25:59`.
*/
time?: string;
/**
* Wrap the date with a link to the given target.
*/
target?: string;
};
export type MetaData = {
/**
* The author name.
*/
author?: string;
/**
* The comments count.
*/
comments?: MetaComments;
/**
* The creation date.
*/
creation?: MetaDate;
/**
* A custom label/value metadata.
*/
custom?: CustomMeta;
/**
* The license name.
*/
license?: string;
/**
* The popularity.
*/
popularity?: string | JSX.Element;
/**
* The publication date.
*/
publication?: MetaDate;
/**
* The estimated reading time.
*/
readingTime?: string | JSX.Element;
/**
* An array of repositories.
*/
repositories?: string[] | JSX.Element[];
/**
* An array of technologies.
*/
technologies?: string[];
/**
* An array of thematics.
*/
thematics?: string[] | JSX.Element[];
/**
* An array of thematics.
*/
topics?: string[] | JSX.Element[];
/**
* A total.
*/
total?: string;
/**
* The update date.
*/
update?: MetaDate;
};
export type MetaKey = keyof MetaData;
export type MetaProps = Omit<
DescriptionListProps,
'items' | 'withSeparator'
> & {
/**
* The meta data.
*/
data: MetaData;
/**
* The items layout.
*/
itemsLayout?: DescriptionListItem['layout'];
/**
* If true, use a slash to delimitate multiple values. Default: true.
*/
withSeparator?: DescriptionListProps['withSeparator'];
};
/**
* Meta component
*
* Renders the given metadata.
*/
const Meta: FC<MetaProps> = ({
data,
itemsLayout = 'inline-values',
withSeparator = true,
...props
}) => {
const intl = useIntl();
/**
* Retrieve the item label based on its key.
*
* @param {keyof MetaData} key - The meta key.
* @returns {string} The item label.
*/
const getLabel = (key: keyof MetaData): string => {
switch (key) {
case 'author':
return intl.formatMessage({
defaultMessage: 'Written by:',
id: 'OI0N37',
description: 'Meta: author label',
});
case 'comments':
return intl.formatMessage({
defaultMessage: 'Comments:',
id: 'jTVIh8',
description: 'Meta: comments label',
});
case 'creation':
return intl.formatMessage({
defaultMessage: 'Created on:',
id: 'b4fdYE',
description: 'Meta: creation date label',
});
case 'license':
return intl.formatMessage({
defaultMessage: 'License:',
id: 'AuGklx',
description: 'Meta: license label',
});
case 'popularity':
return intl.formatMessage({
defaultMessage: 'Popularity:',
id: 'pWTj2W',
description: 'Meta: popularity label',
});
case 'publication':
return intl.formatMessage({
defaultMessage: 'Published on:',
id: 'QGi5uD',
description: 'Meta: publication date label',
});
case 'readingTime':
return intl.formatMessage({
defaultMessage: 'Reading time:',
id: 'EbFvsM',
description: 'Meta: reading time label',
});
case 'repositories':
return intl.formatMessage({
defaultMessage: 'Repositories:',
id: 'DssFG1',
description: 'Meta: repositories label',
});
case 'technologies':
return intl.formatMessage({
defaultMessage: 'Technologies:',
id: 'ADQmDF',
description: 'Meta: technologies label',
});
case 'thematics':
return intl.formatMessage({
defaultMessage: 'Thematics:',
id: 'bz53Us',
description: 'Meta: thematics label',
});
case 'topics':
return intl.formatMessage({
defaultMessage: 'Topics:',
id: 'gJNaBD',
description: 'Meta: topics label',
});
case 'total':
return intl.formatMessage({
defaultMessage: 'Total:',
id: '92zgdp',
description: 'Meta: total label',
});
case 'update':
return intl.formatMessage({
defaultMessage: 'Updated on:',
id: 'tLC7bh',
description: 'Meta: update date label',
});
default:
return '';
}
};
/**
* Retrieve a formatted date (and time).
*
* @param {MetaDate} dateTime - A date object.
* @returns {JSX.Element} The formatted date wrapped in a time element.
*/
const getDate = (dateTime: MetaDate): JSX.Element => {
const { date, time, target } = dateTime;
if (!dateTime.time) {
const isoDate = new Date(`${date}`).toISOString();
return target ? (
<Link href={target}>
<time dateTime={isoDate}>{getFormattedDate(dateTime.date)}</time>
</Link>
) : (
<time dateTime={isoDate}>{getFormattedDate(dateTime.date)}</time>
);
}
const isoDateTime = new Date(`${date}T${time}`).toISOString();
const dateString = intl.formatMessage(
{
defaultMessage: '{date} at {time}',
description: 'Meta: publication date and time',
id: 'fcHeyC',
},
{
date: getFormattedDate(dateTime.date),
time: getFormattedTime(`${dateTime.date}T${dateTime.time}`),
}
);
return target ? (
<Link href={target}>
<time dateTime={isoDateTime}>{dateString}</time>
</Link>
) : (
<time dateTime={isoDateTime}>{dateString}</time>
);
};
/**
* Retrieve the formatted comments count.
*
* @param comments - The comments object.
* @returns {string | JSX.Element} - The comments count.
*/
const getCommentsCount = (comments: MetaComments) => {
const { count, target } = comments;
const commentsCount = intl.formatMessage(
{
defaultMessage:
'{commentsCount, plural, =0 {No comments} one {# comment} other {# comments}}',
id: 'adTrj7',
description: 'Meta: comments count',
},
{ commentsCount: count }
);
return target ? <Link href={target}>{commentsCount}</Link> : commentsCount;
};
/**
* Retrieve the formatted item value.
*
* @param {keyof MetaData} key - The meta key.
* @param {ValueOf<MetaData>} value - The meta value.
* @returns {string|ReactNode|ReactNode[]} - The formatted value.
*/
const getValue = <T extends MetaKey>(
key: T,
value: MetaData[T]
): string | ReactNode | ReactNode[] => {
switch (key) {
case 'comments':
return getCommentsCount(value as MetaComments);
case 'creation':
case 'publication':
case 'update':
return getDate(value as MetaDate);
default:
return value as string | ReactNode | ReactNode[];
}
};
/**
* Transform the metadata to description list item format.
*
* @param {MetaData} items - The meta.
* @returns {DescriptionListItem[]} The formatted description list items.
*/
const getItems = (items: MetaData): DescriptionListItem[] => {
const listItems: DescriptionListItem[] = Object.entries(items)
.map(([key, value]) => {
if (!key || !value) return;
const metaKey = key as MetaKey;
return {
id: metaKey,
label:
metaKey === 'custom'
? (value as CustomMeta).label
: getLabel(metaKey),
layout: itemsLayout,
value:
metaKey === 'custom' && (value as CustomMeta)
? (value as CustomMeta).value
: getValue(metaKey, value),
} as DescriptionListItem;
})
.filter((item): item is DescriptionListItem => !!item);
return listItems;
};
return (
<DescriptionList
items={getItems(data)}
withSeparator={withSeparator}
{...props}
/>
);
};
export default Meta;
|