Skip to content

Commit 30d7c64

Browse files
authored
Merge pull request #11928 from Turbo87/pattern-component
Extract `PatternDescription` component
2 parents 6b1b5e7 + 459efff commit 30d7c64

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

app/components/settings/api-tokens.gjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ import or from 'ember-truth-helpers/helpers/or';
1414

1515
import CopyButton from 'crates-io/components/copy-button';
1616
import LoadingSpinner from 'crates-io/components/loading-spinner';
17+
import PatternDescription from 'crates-io/components/token-scopes/pattern-description';
1718
import Tooltip from 'crates-io/components/tooltip';
1819
import dateFormatDistanceToNow from 'crates-io/helpers/date-format-distance-to-now';
1920
import isClipboardSupported from 'crates-io/helpers/is-clipboard-supported';
2021

21-
import { patternDescription, scopeDescription } from '../../utils/token-scopes';
22+
import { scopeDescription } from '../../utils/token-scopes';
2223

2324
export default class ApiTokens extends Component {
2425
@service store;
2526
@service notifications;
2627
@service router;
2728

2829
scopeDescription = scopeDescription;
29-
patternDescription = patternDescription;
3030

3131
get sortedTokens() {
3232
return this.args.tokens
@@ -136,7 +136,7 @@ export default class ApiTokens extends Component {
136136

137137
{{#each (this.listToParts token.crate_scopes) as |part|~}}
138138
{{#if (eq part.type 'element')}}
139-
<strong>{{part.value}}<Tooltip @text={{this.patternDescription part.value}} /></strong>
139+
<strong>{{part.value}}<Tooltip><PatternDescription @pattern={{part.value}} /></Tooltip></strong>
140140
{{~else~}}
141141
{{part.value}}
142142
{{/if}}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Component from '@glimmer/component';
2+
3+
import { eq } from 'ember-truth-helpers';
4+
5+
export default class PatternDescription extends Component {
6+
get prefix() {
7+
if (this.args.pattern.endsWith('*')) {
8+
return this.args.pattern.slice(0, -1);
9+
}
10+
}
11+
12+
<template>
13+
{{#if (eq @pattern '*')}}
14+
Matches all crates on crates.io
15+
{{else if this.prefix}}
16+
Matches all crates starting with
17+
<strong>{{this.prefix}}</strong>
18+
{{else}}
19+
Matches only the
20+
<strong>{{@pattern}}</strong>
21+
crate
22+
{{/if}}
23+
</template>
24+
}

app/controllers/settings/tokens/new.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { tracked } from '@glimmer/tracking';
66
import { task } from 'ember-concurrency';
77
import { TrackedArray } from 'tracked-built-ins';
88

9-
import { patternDescription, scopeDescription } from '../../../utils/token-scopes';
9+
import { scopeDescription } from '../../../utils/token-scopes';
1010

1111
export default class NewTokenController extends Controller {
1212
@service notifications;
@@ -157,16 +157,6 @@ class CratePattern {
157157
return this.pattern.endsWith('*');
158158
}
159159

160-
get description() {
161-
if (!this.pattern) {
162-
return 'Please enter a crate name pattern';
163-
} else if (this.isValid) {
164-
return patternDescription(this.pattern);
165-
} else {
166-
return 'Invalid crate name pattern';
167-
}
168-
}
169-
170160
@action resetValidation() {
171161
this.showAsInvalid = false;
172162
}

app/templates/settings/tokens/new.gjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import autoFocus from '@zestia/ember-auto-focus/modifiers/auto-focus';
77
import perform from 'ember-concurrency/helpers/perform';
88
import preventDefault from 'ember-event-helpers/helpers/prevent-default';
99
import svgJar from 'ember-svg-jar/helpers/svg-jar';
10-
import eq from 'ember-truth-helpers/helpers/eq';
10+
import { eq, not } from 'ember-truth-helpers';
1111

1212
import LoadingSpinner from 'crates-io/components/loading-spinner';
13+
import PatternDescription from 'crates-io/components/token-scopes/pattern-description';
1314

1415
<template>
1516
<h2>New API Token</h2>
@@ -152,7 +153,13 @@ import LoadingSpinner from 'crates-io/components/loading-spinner';
152153
/>
153154

154155
<span class='pattern-description' data-test-description>
155-
{{pattern.description}}
156+
{{#if (not pattern.pattern)}}
157+
Please enter a crate name pattern
158+
{{else if pattern.isValid}}
159+
<PatternDescription @pattern={{pattern.pattern}} />
160+
{{else}}
161+
Invalid crate name pattern
162+
{{/if}}
156163
</span>
157164
</div>
158165

app/utils/token-scopes.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { htmlSafe } from '@ember/template';
2-
31
const DESCRIPTIONS = {
42
'change-owners': 'Invite new crate owners or remove existing ones',
53
'publish-new': 'Publish new crates',
@@ -10,13 +8,3 @@ const DESCRIPTIONS = {
108
export function scopeDescription(scope) {
119
return DESCRIPTIONS[scope];
1210
}
13-
14-
export function patternDescription(pattern) {
15-
if (pattern === '*') {
16-
return 'Matches all crates on crates.io';
17-
} else if (pattern.endsWith('*')) {
18-
return htmlSafe(`Matches all crates starting with <strong>${pattern.slice(0, -1)}</strong>`);
19-
} else {
20-
return htmlSafe(`Matches only the <strong>${pattern}</strong> crate`);
21-
}
22-
}

0 commit comments

Comments
 (0)