-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathwithMetaBar.tsx
More file actions
128 lines (115 loc) · 4.18 KB
/
Copy pathwithMetaBar.tsx
File metadata and controls
128 lines (115 loc) · 4.18 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
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
'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 CollapsedSidebarRail from '#site/components/Common/CollapsedSidebarRail';
import SidebarToggleButton from '#site/components/Common/SidebarToggleButton';
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 { useSidebarState } from '#site/providers/sidebarStateProvider';
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
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();
const { isRightSidebarCollapsed, toggleRightSidebar } = useSidebarState();
// 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)');
// Check if there's any content to show in the metabar
const hasContent =
lastUpdated ||
readingTimeText ||
usernames.length > 0 ||
headings.length > 0;
// Always show collapsed rail when right sidebar is collapsed
if (isRightSidebarCollapsed) {
return (
<CollapsedSidebarRail side="right">
<SidebarToggleButton
side="right"
isCollapsed={isRightSidebarCollapsed}
onToggle={toggleRightSidebar}
/>
</CollapsedSidebarRail>
);
}
// If no content, show empty metabar area with toggle button
if (!hasContent) {
return (
<div className="flex w-full flex-col border-l border-neutral-200 bg-white dark:border-neutral-900 dark:bg-neutral-950">
<div className="mb-6 flex justify-end pt-6 pr-2">
<SidebarToggleButton
side="right"
isCollapsed={isRightSidebarCollapsed}
onToggle={toggleRightSidebar}
/>
</div>
</div>
);
}
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 }}
>
<div className="mb-6 flex justify-end pr-2">
<SidebarToggleButton
side="right"
isCollapsed={isRightSidebarCollapsed}
onToggle={toggleRightSidebar}
/>
</div>
</MetaBar>
);
};
export default WithMetaBar;