Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

matt-user
Copy link

@matt-user matt-user commented May 7, 2024

We have begun the process of integrating sway playground into the docs hub. To start we simply added a button below full code examples to open the examples in sway playground. For this to be possible this modification to sway-playground is required. Let me know if there is a better way to implement this functionality.

To test this locally, run sway-playground locally, checkout the corresponding docs hub and run that locally as well. You may need to run git submodule update --init --recursive.

corresponding docs hub pr FuelLabs/docs-hub#243

@matt-user matt-user requested a review from a team May 8, 2024 15:22
@matt-user matt-user marked this pull request as ready for review May 8, 2024 15:23
Copy link
Member

@sdankel sdankel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did need to run git submodule update --init --recursive -- thanks for that. Should that be added to the docs-hub readme?

I'm happy to help with this, let's sync up about your timeline for this feature.

Comment on lines +62 to +64
if (message.origin !== APPROVED_EXTERNAL_DOMAIN) {
return;
}
Copy link
Member

@sdankel sdankel May 9, 2024

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:

  • upload the code to a gist
  • use the gist ID as a query parameter
  • send the toolchain name as a query parameter as well
  • sway playground fetches the code from the gist

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.

Copy link
Author

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!

}
}

window.addEventListener('message', setLocalStorageFromExternalDomain);
Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Author

Choose a reason for hiding this comment

The 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 NODE_ENV=development pnpm dev

@sdankel
Copy link
Member

sdankel commented May 10, 2024

Done in #78

@sdankel sdankel closed this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants