Skip to content

Commit 59ca719

Browse files
committed
ref(ui): Allow ordering of groups in SentryProjectSelectorField
1 parent 201efc1 commit 59ca719

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

static/app/components/forms/fields/sentryProjectSelectorField.spec.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ describe('SentryProjectSelectorField', () => {
4343
render(
4444
<SentryProjectSelectorField
4545
groupProjects={project => (project.slug === 'other-project' ? 'other' : 'main')}
46-
groupLabels={{
47-
main: 'Main projects',
48-
}}
46+
groups={[
47+
{key: 'main', label: 'Main projects'},
48+
{key: 'other', label: 'Other'},
49+
]}
4950
onChange={mock}
5051
name="project"
5152
projects={projects}
@@ -55,6 +56,6 @@ describe('SentryProjectSelectorField', () => {
5556
await selectEvent.openMenu(screen.getByText(/choose sentry project/i));
5657

5758
expect(screen.getByText('Main projects')).toBeInTheDocument();
58-
expect(screen.getByText('other')).toBeInTheDocument();
59+
expect(screen.getByText('Other')).toBeInTheDocument();
5960
});
6061
});

static/app/components/forms/fields/sentryProjectSelectorField.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ type GroupProjects = (project: Project) => string;
1616
// projects can be passed as a direct prop as well
1717
export interface RenderFieldProps extends InputFieldProps {
1818
avatarSize?: number;
19-
/**
20-
* When using groupProjects you can specify the labels of the groups as a
21-
* mapping of the key returned for the groupProjects to the label
22-
*/
23-
groupLabels?: Record<string, React.ReactNode>;
2419
/**
2520
* Controls grouping of projects within the field. Useful to prioritize some
2621
* projects above others
2722
*/
2823
groupProjects?: GroupProjects;
24+
/**
25+
* When using groupProjects you must specify the labels of the groups as a
26+
* list of key and label. The ordering determines the order of the groups.
27+
*/
28+
groups?: Array<{key: string; label: React.ReactNode}>;
2929
projects?: Project[];
3030
/**
3131
* Use the slug as the select field value. Without setting this the numeric id
@@ -37,7 +37,7 @@ export interface RenderFieldProps extends InputFieldProps {
3737
function SentryProjectSelectorField({
3838
projects,
3939
groupProjects,
40-
groupLabels,
40+
groups,
4141
avatarSize = 20,
4242
placeholder = t('Choose Sentry project'),
4343
valueIsSlug,
@@ -61,9 +61,11 @@ function SentryProjectSelectorField({
6161
const projectOptions =
6262
projects && groupProjects
6363
? // Create project groups when groupProjects is in use
64-
Object.entries(groupBy(projects, groupProjects)).map(([key, projectsGroup]) => ({
65-
label: groupLabels?.[key] ?? key,
66-
options: projectsGroup.map(projectToOption),
64+
groups?.map(({key, label}) => ({
65+
label,
66+
options: projects
67+
.filter(project => groupProjects(project) === key)
68+
.map(projectToOption),
6769
}))
6870
: // Otherwise just map projects to the options
6971
projects?.map(projectToOption);

static/app/views/monitors/components/monitorForm.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ function MonitorForm({
291291
groupProjects={project =>
292292
platformsWithGuides.includes(project.platform) ? 'suggested' : 'other'
293293
}
294-
groupLabels={{
295-
suggested: t('Suggested Projects'),
296-
other: t('Other Projects'),
297-
}}
294+
groups={[
295+
{key: 'suggested', label: t('Suggested Projects')},
296+
{key: 'other', label: t('Other Projects')},
297+
]}
298298
projects={filteredProjects}
299299
placeholder={t('Choose Project')}
300300
disabled={!!monitor}

0 commit comments

Comments
 (0)