diff --git a/frontend/web/components/pages/onboarding/bootstrap/__tests__/onboardingProject.test.ts b/frontend/web/components/pages/onboarding/bootstrap/__tests__/onboardingProject.test.ts new file mode 100644 index 000000000000..62cb7dc89817 --- /dev/null +++ b/frontend/web/components/pages/onboarding/bootstrap/__tests__/onboardingProject.test.ts @@ -0,0 +1,20 @@ +import { ProjectSummary } from 'common/types/responses' +import { newestProject } from 'components/pages/onboarding/bootstrap/onboardingProject' + +const project = (id: number): ProjectSummary => ({ id } as ProjectSummary) + +describe('newestProject', () => { + it('takes the project the user just created, not the first returned', () => { + const created = project(9) + expect(newestProject([project(2), created, project(5)])).toBe(created) + }) + + it('takes the only project there is', () => { + const only = project(3) + expect(newestProject([only])).toBe(only) + }) + + it('finds nothing in an organisation with no projects', () => { + expect(newestProject([])).toBeUndefined() + }) +}) diff --git a/frontend/web/components/pages/onboarding/bootstrap/bootstrapOnboarding.ts b/frontend/web/components/pages/onboarding/bootstrap/bootstrapOnboarding.ts index b5bf917e2cc2..74595fe1d518 100644 --- a/frontend/web/components/pages/onboarding/bootstrap/bootstrapOnboarding.ts +++ b/frontend/web/components/pages/onboarding/bootstrap/bootstrapOnboarding.ts @@ -24,6 +24,7 @@ import { findOnboardingTag, shouldSeedDemoFlag, } from './demoFlag' +import { newestProject } from './onboardingProject' import { SmartDefaults } from 'components/pages/onboarding/hooks/useSmartDefaults' import { createOrganisationViaAccountStore } from './createOrganisationViaAccountStore' import API from 'project/api' @@ -81,7 +82,7 @@ async function ensureProject( const projects = await store .dispatch(projectService.endpoints.getProjects.initiate({ organisationId })) .unwrap() - const existing = projects?.[0] + const existing = newestProject(projects ?? []) if (existing) { return existing } diff --git a/frontend/web/components/pages/onboarding/bootstrap/onboardingProject.ts b/frontend/web/components/pages/onboarding/bootstrap/onboardingProject.ts new file mode 100644 index 000000000000..c96089fa7229 --- /dev/null +++ b/frontend/web/components/pages/onboarding/bootstrap/onboardingProject.ts @@ -0,0 +1,12 @@ +import { ProjectSummary } from 'common/types/responses' + +// The project the user just made: creating one and clicking Getting Started is +// how you run onboarding again. The list comes back in no useful order, so go +// by id rather than position. +export const newestProject = ( + projects: ProjectSummary[], +): ProjectSummary | undefined => + projects.reduce( + (newest, project) => (!newest || project.id > newest.id ? project : newest), + undefined, + )