-
Notifications
You must be signed in to change notification settings - Fork 1
/
GithubSnippet.tsx
137 lines (121 loc) · 4.47 KB
/
GithubSnippet.tsx
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
import React, {
FunctionComponent,
MouseEventHandler,
useEffect,
useState,
} from "react";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import { Link, Text, Spinner, useColorMode } from "@chakra-ui/react";
import getGithubFileDetailsByUrl from "./lib/getGithubFileDetailsByUrl";
import copyTextToClipboard from "./lib/copyTextToClipboard";
import js from "react-syntax-highlighter/src/languages/hljs/javascript";
import ts from "react-syntax-highlighter/src/languages/hljs/typescript";
import yaml from "react-syntax-highlighter/src/languages/hljs/yaml";
import css from "react-syntax-highlighter/src/languages/hljs/css";
import scss from "react-syntax-highlighter/src/languages/hljs/scss";
import bash from "react-syntax-highlighter/src/languages/hljs/bash";
import xml from "react-syntax-highlighter/src/languages/hljs/xml";
SyntaxHighlighter.registerLanguage("javascript", js);
SyntaxHighlighter.registerLanguage("typescript", ts);
SyntaxHighlighter.registerLanguage("yaml", yaml);
SyntaxHighlighter.registerLanguage("css", css);
SyntaxHighlighter.registerLanguage("scss", scss);
SyntaxHighlighter.registerLanguage("xml", xml);
SyntaxHighlighter.registerLanguage("sh", bash);
const getLanguageByFileExtension = (extension: string) =>
({
js: "javascript",
jsx: "javascript",
ts: "typescript",
tsx: "typescript",
yml: "yaml",
}[extension] || extension);
import styles from "./GithubSnippet.module.scss";
interface Props {
src: string;
showLineNumbers?: boolean;
showFileMeta?: boolean;
}
const GithubSnippet: FunctionComponent<Props> = ({
src,
showLineNumbers = true,
showFileMeta = true,
}) => {
const [canCopy, setCanCopy] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string>(null);
const [fileContents, setFileContents] = useState<string>(null);
const { colorMode } = useColorMode();
const { fileExtension, filename, rawFileURL } =
getGithubFileDetailsByUrl(src);
useEffect(() => {
setCanCopy(Boolean(window.navigator?.clipboard));
}, []);
useEffect(() => {
setIsLoading(true);
setFileContents(undefined);
setError(null);
fetch(rawFileURL)
.then((response) => {
setIsLoading(false);
if (response.ok) {
return response.text();
} else {
throw new Error(
`${response.status} ${response.statusText}`
);
}
})
.then(setFileContents)
.catch((error: Error) => setError(error.message));
}, [rawFileURL]);
const copyToClipboard: MouseEventHandler = (event) => {
event.preventDefault();
copyTextToClipboard(fileContents);
};
const themeClassName =
colorMode === "light" ? styles.github : styles.githubDark;
if (!fileContents) {
return null;
}
return (
<div className={[styles.wrapper, themeClassName].join(' ')}>
{isLoading && <Spinner className={styles.spinner} />}
{error && <Text color="red.300">{error}</Text>}
{fileContents && (
<SyntaxHighlighter
showLineNumbers={showLineNumbers}
useInlineStyles={false}
language={getLanguageByFileExtension(fileExtension)}
>
{fileContents}
</SyntaxHighlighter>
)}
{showFileMeta && (
<div className={styles.meta}>
<Link className={styles.action} target="__blank" href={src}>
{filename}
</Link>
<div>
{canCopy && fileContents && (
<Link
className={styles.action}
onClick={copyToClipboard}
>
Copy
</Link>
)}
<Link
className={styles.action}
target="__blank"
href={src}
>
View raw
</Link>
</div>
</div>
)}
</div>
);
};
export default GithubSnippet;