-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStackBlitzGithub.tsx
More file actions
45 lines (39 loc) · 1.33 KB
/
Copy pathStackBlitzGithub.tsx
File metadata and controls
45 lines (39 loc) · 1.33 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
import React from 'react';
import GithubCodeBlock from './GithubCodeBlock';
interface StackBlitzGithubProps {
repoPath: string;
openFile?: string | string[];
codeFiles?: string[];
startScript?: string;
}
const StackBlitzGithub: React.FC<StackBlitzGithubProps> = ({
repoPath,
openFile = 'main.ts',
codeFiles: plainCodeFiles = undefined,
startScript,
}) => {
const openFiles = Array.isArray(openFile) ? openFile : openFile ? openFile.split(',') : [];
// construct StackBlitz URL
const url = new URL(`https://stackblitz.com/~/github/${repoPath}`);
url.searchParams.append('view', 'editor');
if (openFiles && openFiles.length > 0) {
openFiles.forEach((f) => url.searchParams.append('file', f));
}
if (startScript) {
url.searchParams.append('startScript', startScript);
}
if (!plainCodeFiles) {
plainCodeFiles = [...openFiles];
}
return (
<>
<a href={url.toString()} target="_blank" rel="noopener noreferrer">
<img alt="Open in StackBlitz" src="https://developer.stackblitz.com/img/open_in_stackblitz.svg" />
</a>
{plainCodeFiles.map((file) => (
<GithubCodeBlock key={file} repoPath={repoPath} file={file} />
))}
</>
);
};
export default StackBlitzGithub;