-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathwithMetaBar.tsx
More file actions
81 lines (72 loc) · 2.73 KB
/
withMetaBar.tsx
File metadata and controls
81 lines (72 loc) · 2.73 KB
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
'use client';
import MetaBar from '@node-core/ui-components/Containers/MetaBar';
import GitHubIcon from '@node-core/ui-components/Icons/Social/GitHub';
import { defaultLocale } from '@node-core/website-i18n';
import { useFormatter, useLocale, useTranslations } from 'next-intl';
import Link from '#site/components/Link';
import WithAvatarGroup from '#site/components/withAvatarGroup';
import useClientContext from '#site/hooks/useClientContext';
import useMediaQuery from '#site/hooks/useMediaQuery';
import { DEFAULT_DATE_FORMAT } from '#site/next.calendar.constants.mjs';
import { TRANSLATION_URL } from '#site/next.constants.mjs';
import { getGitHubBlobUrl } from '#site/util/github';
import type { FC } from 'react';
const WithMetaBar: FC = () => {
const { headings, readingTime, frontmatter, filename } = useClientContext();
const formatter = useFormatter();
const lastUpdated = frontmatter.date
? // "frontmatter.date" is deterministic
// eslint-disable-next-line @eslint-react/purity
formatter.dateTime(new Date(frontmatter.date), DEFAULT_DATE_FORMAT)
: undefined;
const readingTimeText = formatter.number(readingTime.minutes, {
style: 'unit',
unit: 'minute',
maximumFractionDigits: 0,
});
const usernames =
frontmatter.authors?.split(',').map(author => author.trim()) ?? [];
const t = useTranslations();
const locale = useLocale();
// Since we cannot show the same number of avatars in Mobile / Tablet
// resolution as we do on desktop and there is overflow, we are adjusting
// the number of avatars manually for the resolutions below
const isSmallerThanDesktop = useMediaQuery('(max-width: 1280px)');
return (
<MetaBar
heading={t('components.metabar.tableOfContents')}
as={Link}
aria-label={t('components.metabar.metadata')}
items={{
[t('components.metabar.lastUpdated')]: lastUpdated,
[t('components.metabar.readingTime')]: readingTimeText,
...(usernames.length && {
[t(
`components.metabar.${usernames.length > 1 ? 'authors' : 'author'}`
)]: (
<WithAvatarGroup
usernames={usernames}
limit={isSmallerThanDesktop ? 5 : 8}
/>
),
}),
[t('components.metabar.contribute')]: (
<>
<GitHubIcon className="fill-neutral-700 dark:fill-neutral-100" />
<Link
href={
locale === defaultLocale.code
? getGitHubBlobUrl(filename)
: TRANSLATION_URL
}
>
{t('components.metabar.contributeText')}
</Link>
</>
),
}}
headings={{ items: headings }}
/>
);
};
export default WithMetaBar;