-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathwithDownloadSection.tsx
More file actions
57 lines (45 loc) · 1.95 KB
/
withDownloadSection.tsx
File metadata and controls
57 lines (45 loc) · 1.95 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
import { getLocale } from 'next-intl/server';
import { getClientContext } from '#site/client-context';
import WithNodeRelease from '#site/components/withNodeRelease';
import provideDownloadSnippets from '#site/next-data/providers/downloadSnippets';
import { defaultLocale } from '#site/next.locales.mjs';
import {
ReleaseProvider,
ReleasesProvider,
} from '#site/providers/releaseProvider';
import type { NodeRelease } from '../types';
import type { FC, PropsWithChildren } from 'react';
type WithDownloadSectionProps = PropsWithChildren<{
releases: Array<NodeRelease>;
}>;
const WithDownloadSection: FC<WithDownloadSectionProps> = async ({
releases,
children,
}) => {
const locale = await getLocale();
const snippets = await provideDownloadSnippets();
const localeSnippets = snippets.get(locale) ?? [];
// By default the translated languages do not contain all the download snippets
// Hence we always merge any translated snippet with the fallbacks for missing snippets
const fallbackSnippets = snippets.get(defaultLocale.code) ?? [];
const { pathname } = getClientContext();
// Some available translations do not have download snippets translated or have them partially translated
// This aims to merge the available translated snippets with the fallback snippets
const memoizedSnippets = fallbackSnippets
.filter(snippet => !localeSnippets.some(s => s.name === snippet.name))
.concat(localeSnippets);
// Decides which initial release to use based on the current pathname
const initialRelease = pathname.endsWith('/current')
? 'Current'
: ['Active LTS' as const, 'Maintenance LTS' as const];
return (
<WithNodeRelease status={initialRelease}>
{({ release }) => (
<ReleasesProvider releases={releases} snippets={memoizedSnippets}>
<ReleaseProvider initialRelease={release}>{children}</ReleaseProvider>
</ReleasesProvider>
)}
</WithNodeRelease>
);
};
export default WithDownloadSection;