Skip to content

Commit ebb08ca

Browse files
ref(ui): Allow ordering of groups in SentryProjectSelectorField (#69064)
1 parent 5fe324c commit ebb08ca

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import groupBy from 'lodash/groupBy';
2-
31
import IdBadge from 'sentry/components/idBadge';
42
import {t} from 'sentry/locale';
53
import type {Project} from 'sentry/types';
@@ -16,16 +14,16 @@ type GroupProjects = (project: Project) => string;
1614
// projects can be passed as a direct prop as well
1715
export interface RenderFieldProps extends InputFieldProps {
1816
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>;
2417
/**
2518
* Controls grouping of projects within the field. Useful to prioritize some
2619
* projects above others
2720
*/
2821
groupProjects?: GroupProjects;
22+
/**
23+
* When using groupProjects you must specify the labels of the groups as a
24+
* list of key and label. The ordering determines the order of the groups.
25+
*/
26+
groups?: Array<{key: string; label: React.ReactNode}>;
2927
projects?: Project[];
3028
/**
3129
* Use the slug as the select field value. Without setting this the numeric id
@@ -37,7 +35,7 @@ export interface RenderFieldProps extends InputFieldProps {
3735
function SentryProjectSelectorField({
3836
projects,
3937
groupProjects,
40-
groupLabels,
38+
groups,
4139
avatarSize = 20,
4240
placeholder = t('Choose Sentry project'),
4341
valueIsSlug,
@@ -61,9 +59,11 @@ function SentryProjectSelectorField({
6159
const projectOptions =
6260
projects && groupProjects
6361
? // 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),
62+
groups?.map(({key, label}) => ({
63+
label,
64+
options: projects
65+
.filter(project => groupProjects(project) === key)
66+
.map(projectToOption),
6767
}))
6868
: // Otherwise just map projects to the options
6969
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)