-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow sway playground to be opened from docs hub #76
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import { Toolchain } from './features/editor/components/ToolchainDropdown'; | |
import { useTranspile } from './features/editor/hooks/useTranspile'; | ||
import EditorView from './features/editor/components/EditorView'; | ||
import { Analytics, track } from '@vercel/analytics/react'; | ||
import { APPROVED_EXTERNAL_DOMAIN } from './constants'; | ||
|
||
const DRAWER_WIDTH = '40vw'; | ||
|
||
|
@@ -56,6 +57,28 @@ function App() { | |
// An error message to display to the user. | ||
const [drawerOpen, setDrawerOpen] = useState(false); | ||
|
||
useEffect(() => { | ||
function setLocalStorageFromExternalDomain(message: MessageEvent) { | ||
if (message.origin !== APPROVED_EXTERNAL_DOMAIN) { | ||
return; | ||
} | ||
|
||
const data = JSON.parse(message.data); | ||
const { swayCode } = data; | ||
// I'm only expecting us to set sway code from the docs hub. | ||
// If users want to see the corresponding solidity they can use the built-in transpiler. | ||
if (swayCode) { | ||
onSwayCodeChange(swayCode); | ||
} | ||
} | ||
|
||
window.addEventListener('message', setLocalStorageFromExternalDomain); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this out, but it doesn't seem to be working. When the playground link opens, it doesn't have the code from the example, and I don't see any messages in the network tab. I'm guessing it could be a timing issue; perhaps the message is sent before the playground page is rendered (and this useEffect would run). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it's probably a timing issue. The delay I added on the docshub was through trial and error. Another thing is you need to run the docs hub with |
||
|
||
return () => { | ||
window.removeEventListener('message', setLocalStorageFromExternalDomain); | ||
}; | ||
}); | ||
|
||
useEffect(() => { | ||
if (showSolidity) { | ||
setIsCompiled(false); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
export const FUEL_GREEN = '#00f58c'; | ||
|
||
export const LOCAL_SERVER_URI = 'http://0.0.0.0:8080'; | ||
export const SERVER_URI = process.env.REACT_APP_LOCAL_SERVER | ||
export const IS_LOCAL = !!process.env.REACT_APP_LOCAL_SERVER; | ||
export const SERVER_URI = IS_LOCAL | ||
? LOCAL_SERVER_URI | ||
: 'https://api.sway-playground.org'; | ||
export const APPROVED_EXTERNAL_DOMAIN = IS_LOCAL | ||
? 'http://localhost:3000' // Local domain of docs hub | ||
: 'https://docs-hub.vercel.app'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more secure to handle this on the server side in
cors.rs
. But I think an even better way would be to do what rust playground does:Essentially, we can use github gists as a simple database.
Example:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3c65be25f3334da7947b0e120dce23a1
For us it can be a little simpler:
https://sway-playground.org/?toolchain=latest&gist=3c65be25f3334da7947b0e120dce23a1
Sway playground will need to sanitize the string. But then we don't have to worry about cors, and there's no concerns about timing issues.
I would recommend using React router dom's useSearchParams for reading and writing the query params.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah this definitely seems like a better approach thanks for the feedback!