Skip to content

Commit fb4b26f

Browse files
committed
CrateSidebar: Show "Try on Rust Playground" button for crates that are available there
1 parent e378107 commit fb4b26f

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

app/components/crate-sidebar.hbs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,17 @@
171171
</div>
172172
{{/if}}
173173
</div>
174+
175+
{{#if this.playgroundLink}}
176+
<div>
177+
<a
178+
href={{this.playgroundLink}}
179+
target="_blank"
180+
rel="noopener noreferrer"
181+
local-class="playground-button"
182+
>
183+
Try on Rust Playground
184+
</a>
185+
</div>
186+
{{/if}}
174187
</section>

app/components/crate-sidebar.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const NUM_VERSIONS = 5;
77

88
export default class DownloadGraph extends Component {
99
@service session;
10+
@service playground;
1011

1112
@computed('args.crate.owner_user', 'session.currentUser.id')
1213
get isOwner() {
@@ -21,4 +22,14 @@ export default class DownloadGraph extends Component {
2122
}
2223

2324
@gt('sortedVersions.length', NUM_VERSIONS) hasMoreVersions;
25+
26+
get playgroundLink() {
27+
let playgroundCrates = this.playground.crates;
28+
if (!playgroundCrates) return;
29+
30+
let playgroundCrate = playgroundCrates.find(it => it.name === this.crate.name);
31+
if (!playgroundCrate) return;
32+
33+
return `https://play.rust-lang.org/?code=use%20${playgroundCrate.id}%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20%2F%2F%20try%20using%20the%20%60${playgroundCrate.id}%60%20crate%20here%0A%7D`;
34+
}
2435
}

app/components/crate-sidebar.module.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,9 @@ ul.owners, ul.keywords {
118118
.more-versions-link {
119119
composes: small from '../styles/shared/typography.module.css';
120120
}
121+
122+
.playground-button {
123+
composes: yellow-button small from '../styles/shared/buttons.module.css';
124+
justify-content: center;
125+
margin-top: 20px;
126+
}

app/routes/crate/version.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AjaxError } from '../../utils/ajax';
77

88
export default class VersionRoute extends Route {
99
@service notifications;
10+
@service playground;
1011

1112
async model(params) {
1213
let crate = this.modelFor('crate');
@@ -49,6 +50,9 @@ export default class VersionRoute extends Route {
4950
}
5051
});
5152
}
53+
54+
// load Rust Playground crates list
55+
this.playground.loadCrates();
5256
}
5357

5458
serialize(model) {

0 commit comments

Comments
 (0)