-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathReleaseCodeBox.tsx
More file actions
190 lines (166 loc) · 6.24 KB
/
ReleaseCodeBox.tsx
File metadata and controls
190 lines (166 loc) · 6.24 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
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
'use client';
import { highlightToHtml } from '@node-core/rehype-shiki/minimal';
import AlertBox from '@node-core/ui-components/Common/AlertBox';
import Skeleton from '@node-core/ui-components/Common/Skeleton';
import { useTranslations } from 'next-intl';
import { use, useMemo } from 'react';
import CodeBox from '#site/components/Common/CodeBox';
import LinkWithArrow from '#site/components/Common/LinkWithArrow';
import Link from '#site/components/Link';
import WithReleaseAlertBox from '#site/components/withReleaseAlertBox';
import {
ReleaseContext,
ReleasesContext,
} from '#site/providers/releaseProvider';
import { INSTALL_METHODS } from '#site/util/download';
import createInterpreter from '#site/util/interpreter';
import type { DownloadSnippet } from '#site/types/download';
import type { ReleaseContextType } from '#site/types/release';
import type { FC } from 'react';
// Creates a minimal JavaScript interpreter for parsing the JavaScript code from the snippets
// Note: that the code runs inside a sandboxed environment and cannot interact with any code outside of the sandbox
// It also does not have access to any Global or Window objects, nor it can execute code on the end-user's browser
// It also only allows a return statement for a string and it forces the return value to also be a string and only be used
// by Shiki to render the highlighted syntax. Hence XSS attacks or JavaScript injections are not possible.
const interpreter = createInterpreter({}, 'script');
/**
* Parses a snippet string using the interpreter with the given release context
*/
const parseSnippet = (snippet: string, releaseContext: ReleaseContextType) => {
interpreter.import({ props: releaseContext });
interpreter.run(`exports.content = \`${snippet}\``);
return interpreter.exports.content;
};
/**
* Custom hook to handle snippet processing logic
*/
const useSnippetProcessor = (
snippets: Array<DownloadSnippet>,
context: ReleaseContextType
) => {
return useMemo(() => {
// Find relevant snippets
const installMethodSnippet = snippets.find(
({ name }) => name === context.installMethod.toLowerCase()
);
const packageManagerSnippet = snippets.find(
({ name }) => name === context.packageManager.toLowerCase()
);
// Only process if both snippets are available
if (!installMethodSnippet || !packageManagerSnippet) {
return '';
}
const verifyNodeSnippet = snippets.find(({ name }) => name === 'node');
const installCorepackSnippet =
context.packageManager !== 'NPM' &&
// Corepack is no longer distributed with Node.js v25
context.release.major >= 25 &&
snippets.find(({ name }) => name === 'corepack');
// Combine and parse snippets
const parsedContent = parseSnippet(
[
installMethodSnippet,
verifyNodeSnippet,
installCorepackSnippet,
packageManagerSnippet,
]
.filter(Boolean)
.map(snippet => (snippet as DownloadSnippet).content)
.join('\n'),
context
);
// Convert to HTML using Shiki's highlighter
// This is faster than JSX rendering as it avoids React runtime overhead
return highlightToHtml(
parsedContent,
context.os === 'WIN' ? 'ps1' : 'bash'
);
}, [snippets, context]);
};
/**
* Custom hook to get current platform information
*/
const usePlatformInfo = (installMethod: string) => {
return useMemo(() => {
const platform = INSTALL_METHODS.find(
({ value }) => value === installMethod
);
// Provide defaults for destructuring
return {
label: platform?.label || '',
url: platform?.url || '',
info: platform?.info || 'layouts.download.codeBox.platformInfo.default',
recommended: platform?.recommended || false,
exists: !!platform,
};
}, [installMethod]);
};
/**
* ReleaseCodeBox component displays installation instructions based on platform and context
*/
const ReleaseCodeBox: FC = () => {
const { snippets } = use(ReleasesContext);
const context = use(ReleaseContext);
const t = useTranslations();
// Process platform information
const platformInfo = usePlatformInfo(context.installMethod);
// Process snippets
const parsedSnippets = useSnippetProcessor(snippets, context);
// UI state calculations
const displayLanguage = context.os === 'WIN' ? 'PowerShell' : 'Bash';
const isLoading = context.os === 'LOADING' || context.installMethod === '';
return (
<div className="mt-4 mb-6 flex flex-col gap-2">
{/* NoScript warning */}
<noscript>
<AlertBox
title={t('components.common.alertBox.warning')}
level="warning"
size="small"
>
{t.rich('layouts.download.codeBox.noScriptDetected', {
link: text => (
<Link href="/download/archive/current">
<b>{text}</b>
</Link>
),
})}
</AlertBox>
</noscript>
{/* Release status alert */}
<WithReleaseAlertBox status={context.release.status} />
{/* Community platform notice */}
{platformInfo.exists && !platformInfo.recommended && (
<AlertBox
title={t('components.common.alertBox.info')}
level="info"
size="small"
>
{t('layouts.download.codeBox.communityPlatformInfo')}
</AlertBox>
)}
{/* Code display with skeleton loading */}
<Skeleton loading={isLoading}>
<CodeBox language={displayLanguage} className="min-h-[19rem]">
{/* eslint-disable-next-line @eslint-react/dom/no-dangerously-set-innerhtml */}
<code dangerouslySetInnerHTML={{ __html: parsedSnippets }} />
</CodeBox>
</Skeleton>
{/* Platform info footer */}
<span className="text-center text-xs text-neutral-800 dark:text-neutral-200">
<Skeleton loading={isLoading} hide={!platformInfo.exists}>
{t(platformInfo.info, { platform: platformInfo.label })}{' '}
{t.rich('layouts.download.codeBox.externalSupportInfo', {
platform: platformInfo.label,
link: text => (
<LinkWithArrow href={platformInfo.url}>
<b>{text}</b>
</LinkWithArrow>
),
})}
</Skeleton>
</span>
</div>
);
};
export default ReleaseCodeBox;