Browse Source

ref: Move sortProjects into utils/project (#71849)

Relates to https://github.com/getsentry/frontend-tsc/issues/13
Ryan Albrecht 9 months ago
parent
commit
35bd66eba1

+ 0 - 13
static/app/utils.tsx

@@ -1,7 +1,6 @@
 import type {Query} from 'history';
 
 import type {EventTag} from 'sentry/types/event';
-import type {Project} from 'sentry/types/project';
 import {formatNumberWithDynamicDecimalPoints} from 'sentry/utils/formatters';
 import {appendTagCondition} from 'sentry/utils/queryString';
 
@@ -208,18 +207,6 @@ export function convertMultilineFieldValue<T extends string | string[]>(
   return '';
 }
 
-function projectDisplayCompare(a: Project, b: Project): number {
-  if (a.isBookmarked !== b.isBookmarked) {
-    return a.isBookmarked ? -1 : 1;
-  }
-  return a.slug.localeCompare(b.slug);
-}
-
-// Sort a list of projects by bookmarkedness, then by id
-export function sortProjects(projects: Array<Project>): Array<Project> {
-  return projects.sort(projectDisplayCompare);
-}
-
 // build actorIds
 export const buildUserId = (id: string) => `user:${id}`;
 export const buildTeamId = (id: string) => `team:${id}`;

+ 29 - 0
static/app/utils/project/sortProjects.spec.tsx

@@ -0,0 +1,29 @@
+import {ProjectFixture} from 'sentry-fixture/project';
+
+import {sortProjects} from 'sentry/utils/project/sortProjects';
+
+describe('sortProjects', function () {
+  it('sorts by bookmark and project slug', function () {
+    const projects = [
+      ProjectFixture({isBookmarked: true, slug: 'm'}),
+      ProjectFixture({isBookmarked: false, slug: 'm'}),
+      ProjectFixture({isBookmarked: false, slug: 'a'}),
+      ProjectFixture({isBookmarked: true, slug: 'a'}),
+      ProjectFixture({isBookmarked: true, slug: 'z'}),
+      ProjectFixture({isBookmarked: false, slug: 'z'}),
+    ];
+
+    const expected = [
+      expect.objectContaining({isBookmarked: true, slug: 'a'}),
+      expect.objectContaining({isBookmarked: true, slug: 'm'}),
+      expect.objectContaining({isBookmarked: true, slug: 'z'}),
+      expect.objectContaining({isBookmarked: false, slug: 'a'}),
+      expect.objectContaining({isBookmarked: false, slug: 'm'}),
+      expect.objectContaining({isBookmarked: false, slug: 'z'}),
+    ];
+
+    const sortedProjects = sortProjects(projects);
+
+    expect(sortedProjects).toEqual(expected);
+  });
+});

+ 15 - 0
static/app/utils/project/sortProjects.tsx

@@ -0,0 +1,15 @@
+import type {Project} from 'sentry/types/project';
+
+function projectDisplayCompare(a: Project, b: Project): number {
+  if (a.isBookmarked !== b.isBookmarked) {
+    return a.isBookmarked ? -1 : 1;
+  }
+  return a.slug.localeCompare(b.slug);
+}
+
+/**
+ * Sort a list of projects by bookmarkedness, then by id
+ */
+export function sortProjects(projects: Array<Project>): Array<Project> {
+  return projects.sort(projectDisplayCompare);
+}

+ 0 - 29
static/app/utils/utils.spec.tsx

@@ -1,12 +1,9 @@
-import {ProjectFixture} from 'sentry-fixture/project';
-
 import {
   descopeFeatureName,
   escapeDoubleQuotes,
   explodeSlug,
   extractMultilineFields,
   parseRepo,
-  sortProjects,
   valueIsEqual,
 } from 'sentry/utils';
 
@@ -176,32 +173,6 @@ describe('utils.explodeSlug', function () {
   });
 });
 
-describe('utils.projectDisplayCompare', function () {
-  it('sorts by bookmark and project slug', function () {
-    const projects = [
-      ProjectFixture({isBookmarked: true, slug: 'm'}),
-      ProjectFixture({isBookmarked: false, slug: 'm'}),
-      ProjectFixture({isBookmarked: false, slug: 'a'}),
-      ProjectFixture({isBookmarked: true, slug: 'a'}),
-      ProjectFixture({isBookmarked: true, slug: 'z'}),
-      ProjectFixture({isBookmarked: false, slug: 'z'}),
-    ];
-
-    const expected = [
-      expect.objectContaining({isBookmarked: true, slug: 'a'}),
-      expect.objectContaining({isBookmarked: true, slug: 'm'}),
-      expect.objectContaining({isBookmarked: true, slug: 'z'}),
-      expect.objectContaining({isBookmarked: false, slug: 'a'}),
-      expect.objectContaining({isBookmarked: false, slug: 'm'}),
-      expect.objectContaining({isBookmarked: false, slug: 'z'}),
-    ];
-
-    const sortedProjects = sortProjects(projects);
-
-    expect(sortedProjects).toEqual(expected);
-  });
-});
-
 describe('utils.descopeFeatureName', function () {
   it('descopes the feature name', () => {
     [

+ 1 - 1
static/app/views/projectsDashboard/index.tsx

@@ -24,12 +24,12 @@ import ProjectsStatsStore from 'sentry/stores/projectsStatsStore';
 import {space} from 'sentry/styles/space';
 import type {Organization} from 'sentry/types/organization';
 import type {Project, TeamWithProjects} from 'sentry/types/project';
-import {sortProjects} from 'sentry/utils';
 import {
   onRenderCallback,
   Profiler,
   setGroupedEntityTag,
 } from 'sentry/utils/performanceForSentry';
+import {sortProjects} from 'sentry/utils/project/sortProjects';
 import useOrganization from 'sentry/utils/useOrganization';
 import withApi from 'sentry/utils/withApi';
 import withOrganization from 'sentry/utils/withOrganization';

+ 1 - 1
static/app/views/settings/organizationProjects/index.tsx

@@ -17,8 +17,8 @@ import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
 import {t} from 'sentry/locale';
 import {space} from 'sentry/styles/space';
 import type {Project} from 'sentry/types/project';
-import {sortProjects} from 'sentry/utils';
 import {browserHistory} from 'sentry/utils/browserHistory';
+import {sortProjects} from 'sentry/utils/project/sortProjects';
 import {useApiQuery} from 'sentry/utils/queryClient';
 import {decodeScalar} from 'sentry/utils/queryString';
 import routeTitleGen from 'sentry/utils/routeTitle';

+ 1 - 1
static/app/views/settings/organizationTeams/teamProjects.tsx

@@ -22,7 +22,7 @@ import ProjectsStore from 'sentry/stores/projectsStore';
 import {space} from 'sentry/styles/space';
 import type {Team} from 'sentry/types/organization';
 import type {Project} from 'sentry/types/project';
-import {sortProjects} from 'sentry/utils';
+import {sortProjects} from 'sentry/utils/project/sortProjects';
 import {useApiQuery} from 'sentry/utils/queryClient';
 import useApi from 'sentry/utils/useApi';
 import useOrganization from 'sentry/utils/useOrganization';