-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.tsx
More file actions
60 lines (51 loc) · 1.54 KB
/
index.tsx
File metadata and controls
60 lines (51 loc) · 1.54 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
import type { Heading } from '@vcarl/remark-headings';
import { useTranslations } from 'next-intl';
import { Fragment, useMemo } from 'react';
import type { FC } from 'react';
import Link from '@/components/Link';
import styles from './index.module.css';
type MetaBarProps = {
items: Partial<Record<IntlMessageKeys, React.ReactNode>>;
headings?: {
items: Array<Heading>;
minDepth?: number;
};
};
const MetaBar: FC<MetaBarProps> = ({ items, headings }) => {
const t = useTranslations();
// The default depth of headings to display in the table of contents.
const { minDepth = 2, items: headingItems = [] } = headings || {};
const heading = useMemo(
() => headingItems.filter(({ depth }) => depth >= minDepth && depth <= 4),
[minDepth, headingItems]
);
return (
<div className={styles.wrapper}>
<dl>
{Object.entries(items)
.filter(([, value]) => !!value)
.map(([key, value]) => (
<Fragment key={key}>
<dt>{t(key as IntlMessageKeys)}</dt>
<dd>{value}</dd>
</Fragment>
))}
{heading.length > 0 && (
<>
<dt>{t('components.metabar.tableOfContents')}</dt>
<dd>
<ol>
{heading.map(head => (
<li key={head.value}>
<Link href={`#${head?.data?.id}`}>{head.value}</Link>
</li>
))}
</ol>
</dd>
</>
)}
</dl>
</div>
);
};
export default MetaBar;