Skip to content

Commit d5a8e81

Browse files
committed
feat: load snippet by id
1 parent 9bfcd07 commit d5a8e81

File tree

9 files changed

+50
-21
lines changed

9 files changed

+50
-21
lines changed

material-go-playground/example/index.jsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
22
import { render } from 'react-dom';
33
import GoPlayground, { ShareButton } from 'material-go-playground';
44

5-
const code = `package main
5+
const defaultCode = `package main
66
77
import (
88
"fmt"
@@ -36,8 +36,14 @@ function Logo({style, color = '#00acd7'}) {
3636

3737
function App() {
3838
const {width, height} = useWindowSize();
39+
const {code, loading} = useSnippet();
40+
3941
const _height = height - (width > 500 ? 64 : 56) - 8;
4042

43+
if(loading) {
44+
return null
45+
}
46+
4147
return <GoPlayground
4248
title={width > 500 ? "The Go playground" : (width < 350 ? null : <Logo color="#fff" style={{position: "relative", top: 4, left: -4}}/>)}
4349
code={code}
@@ -68,6 +74,29 @@ function App() {
6874
/>
6975
}
7076

77+
function useSnippet() {
78+
const snippet = window.location.hash.substring(1);
79+
80+
const [code, setCode] = useState(defaultCode)
81+
const [loading, setLoading] = useState(snippet.length > 1)
82+
83+
84+
useEffect(() => {
85+
if(!loading) return;
86+
87+
fetch(`https://play.golang.org/p/${snippet}.go`).then((res) => {
88+
return res.text().then((code) => {
89+
setCode(code);
90+
setLoading(false);
91+
})
92+
}).catch(() => {
93+
setLoading(false);
94+
})
95+
}, [])
96+
97+
return { code, loading }
98+
}
99+
71100
function useWindowSize() {
72101
function getSize() {
73102
return {

material-go-playground/example/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material-go-playground/example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@material-ui/core": "^4.11.0",
1717
"@material-ui/icons": "^4.9.1",
1818
"@material-ui/styles": "^4.10.0",
19-
"material-go-playground": "^0.2.9",
19+
"material-go-playground": "^0.2.10",
2020
"react": "^16.14.0",
2121
"react-dom": "^16.14.0"
2222
},

material-go-playground/example/vendors.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material-go-playground/example/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5187,10 +5187,10 @@ map-visit@^1.0.0:
51875187
dependencies:
51885188
object-visit "^1.0.0"
51895189

5190-
material-go-playground@^0.2.9:
5191-
version "0.2.9"
5192-
resolved "https://registry.yarnpkg.com/material-go-playground/-/material-go-playground-0.2.9.tgz#0db3d16754f0cf9374306e7faf95567c2afd7129"
5193-
integrity sha512-Yl33fhF5jI6rQKJdDj9LCdyQqnTB7Unnx8Y7HPe16wBadfVNRt8wjZ7InVQ3vrIHyz64/v/wRHPEneV29+aFxg==
5190+
material-go-playground@^0.2.10:
5191+
version "0.2.10"
5192+
resolved "https://registry.yarnpkg.com/material-go-playground/-/material-go-playground-0.2.10.tgz#0d8594f6da8222593aace6aa9a8d9145e0f750e9"
5193+
integrity sha512-g9AFfCXI96mZRq9U0f6/7fwDEvzLhb+oTRyGs0rbTgqDLo1Lko1lTtJRbeEJUuJBAusmIc3vpZZQhgqYflqD8Q==
51945194
dependencies:
51955195
"@material-ui/icons" "^4.2.1"
51965196
codemirror "^5.48.2"

0 commit comments

Comments
 (0)