Skip to content

Commit b6075e9

Browse files
committed
crate/settings: Implement visibility logic for trustpub_only checkbox
The checkbox is now only visible when: - There are GitHub or GitLab trustpub configs, OR - The `trustpub_only` flag is currently enabled When the user disables the checkbox with no configs present, the checkbox stays visible for the duration of that page visit. On the next visit, it will be hidden.
1 parent 432c2e7 commit b6075e9

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

app/controllers/crate/settings/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ export default class CrateSettingsController extends Controller {
1313
username = '';
1414
@tracked addOwnerVisible = false;
1515

16+
/**
17+
* Tracks whether the trustpub_only checkbox was visible when the page loaded.
18+
* This prevents the checkbox from disappearing immediately when unchecked
19+
* if there are no configs - it will only disappear on the next page visit.
20+
*/
21+
trustpubOnlyCheckboxWasVisible = false;
22+
23+
get showTrustpubOnlyCheckbox() {
24+
let hasConfigs = this.githubConfigs?.length > 0 || this.gitlabConfigs?.length > 0;
25+
return hasConfigs || this.crate?.trustpub_only || this.trustpubOnlyCheckboxWasVisible;
26+
}
27+
1628
@action showAddOwnerForm() {
1729
this.addOwnerVisible = true;
1830
this.username = '';

app/routes/crate/settings/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ export default class SettingsIndexRoute extends Route {
1919
controller.set('crate', crate);
2020
controller.set('githubConfigs', githubConfigs);
2121
controller.set('gitlabConfigs', gitlabConfigs);
22+
23+
// Capture whether the trustpub_only checkbox should be visible on initial load
24+
let hasConfigs = githubConfigs?.length > 0 || gitlabConfigs?.length > 0;
25+
controller.set('trustpubOnlyCheckboxWasVisible', hasConfigs || crate.trustpub_only);
2226
}
2327
}

app/templates/crate/settings/index.gjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ import UserAvatar from 'crates-io/components/user-avatar';
226226
</tbody>
227227
</table>
228228

229-
<TrustpubOnlyCheckbox @crate={{@controller.crate}} class='trustpub-only-checkbox' />
229+
{{#if @controller.showTrustpubOnlyCheckbox}}
230+
<TrustpubOnlyCheckbox @crate={{@controller.crate}} class='trustpub-only-checkbox' />
231+
{{/if}}
230232
</div>
231233

232234
<h2 class='header'>Danger Zone</h2>

tests/routes/crate/settings-test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,94 @@ module('Route | crate.settings', hooks => {
278278
});
279279

280280
module('trustpub_only checkbox', function () {
281+
test('hidden when no configs and flag is false', async function (assert) {
282+
const { crate, user } = prepare(this);
283+
this.authenticateAs(user);
284+
285+
await visit(`/crates/${crate.name}/settings`);
286+
287+
assert.dom('[data-test-trustpub-only-checkbox]').doesNotExist();
288+
});
289+
290+
test('visible when GitHub configs exist', async function (assert) {
291+
const { crate, user } = prepare(this);
292+
this.authenticateAs(user);
293+
294+
this.db.trustpubGithubConfig.create({
295+
crate,
296+
repository_owner: 'rust-lang',
297+
repository_name: 'crates.io',
298+
workflow_filename: 'ci.yml',
299+
});
300+
301+
await visit(`/crates/${crate.name}/settings`);
302+
303+
assert.dom('[data-test-trustpub-only-checkbox]').exists();
304+
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isNotChecked();
305+
});
306+
307+
test('visible when GitLab configs exist', async function (assert) {
308+
const { crate, user } = prepare(this);
309+
this.authenticateAs(user);
310+
311+
this.db.trustpubGitlabConfig.create({
312+
crate,
313+
namespace: 'rust-lang',
314+
project: 'crates.io',
315+
workflow_filepath: '.gitlab-ci.yml',
316+
});
317+
318+
await visit(`/crates/${crate.name}/settings`);
319+
320+
assert.dom('[data-test-trustpub-only-checkbox]').exists();
321+
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isNotChecked();
322+
});
323+
324+
test('visible when flag is true but no configs', async function (assert) {
325+
const { crate, user } = prepare(this);
326+
this.db.crate.update({ where: { id: { equals: crate.id } }, data: { trustpubOnly: true } });
327+
this.authenticateAs(user);
328+
329+
await visit(`/crates/${crate.name}/settings`);
330+
331+
assert.dom('[data-test-trustpub-only-checkbox]').exists();
332+
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isChecked();
333+
});
334+
335+
test('stays visible after disabling when no configs exist', async function (assert) {
336+
const { crate, user } = prepare(this);
337+
this.db.crate.update({ where: { id: { equals: crate.id } }, data: { trustpubOnly: true } });
338+
this.authenticateAs(user);
339+
340+
await visit(`/crates/${crate.name}/settings`);
341+
342+
assert.dom('[data-test-trustpub-only-checkbox]').exists();
343+
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isChecked();
344+
345+
await click('[data-test-trustpub-only-checkbox] [data-test-checkbox]');
346+
347+
// Checkbox stays visible after disabling (within same page visit)
348+
assert.dom('[data-test-trustpub-only-checkbox]').exists();
349+
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isNotChecked();
350+
351+
// After navigating away and back, checkbox should be hidden
352+
await visit(`/crates/${crate.name}`);
353+
await visit(`/crates/${crate.name}/settings`);
354+
355+
assert.dom('[data-test-trustpub-only-checkbox]').doesNotExist();
356+
});
357+
281358
test('enabling trustpub_only', async function (assert) {
282359
const { crate, user } = prepare(this);
283360
this.authenticateAs(user);
284361

362+
this.db.trustpubGithubConfig.create({
363+
crate,
364+
repository_owner: 'rust-lang',
365+
repository_name: 'crates.io',
366+
workflow_filename: 'ci.yml',
367+
});
368+
285369
await visit(`/crates/${crate.name}/settings`);
286370

287371
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isNotChecked();
@@ -298,6 +382,13 @@ module('Route | crate.settings', hooks => {
298382
this.db.crate.update({ where: { id: { equals: crate.id } }, data: { trustpubOnly: true } });
299383
this.authenticateAs(user);
300384

385+
this.db.trustpubGithubConfig.create({
386+
crate,
387+
repository_owner: 'rust-lang',
388+
repository_name: 'crates.io',
389+
workflow_filename: 'ci.yml',
390+
});
391+
301392
await visit(`/crates/${crate.name}/settings`);
302393

303394
assert.dom('[data-test-trustpub-only-checkbox] [data-test-checkbox]').isChecked();
@@ -313,6 +404,13 @@ module('Route | crate.settings', hooks => {
313404
const { crate, user } = prepare(this);
314405
this.authenticateAs(user);
315406

407+
this.db.trustpubGithubConfig.create({
408+
crate,
409+
repository_owner: 'rust-lang',
410+
repository_name: 'crates.io',
411+
workflow_filename: 'ci.yml',
412+
});
413+
316414
let deferred = defer();
317415
this.worker.use(http.patch('/api/v1/crates/:name', () => deferred.promise));
318416

0 commit comments

Comments
 (0)