diff --git a/src/features/projects/data/useProjectSelection.ts b/src/features/projects/data/useProjectSelection.ts index 80b7327b..de8327b4 100644 --- a/src/features/projects/data/useProjectSelection.ts +++ b/src/features/projects/data/useProjectSelection.ts @@ -4,7 +4,13 @@ import NProgress from "nprogress" import { useRouter, usePathname } from "next/navigation" import { useContext } from "react" import { ProjectsContext } from "@/common" -import { Project, ProjectNavigator, getProjectSelectionFromPath } from "../domain" +import { + Project, + ProjectNavigator, + getProjectSelectionFromPath, + getDefaultSpecification + +} from "../domain" export default function useProjectSelection() { const router = useRouter() @@ -29,7 +35,7 @@ export default function useProjectSelection() { }, selectProject: (project: Project) => { const version = project.versions[0] - const specification = version.specifications.find(spec => spec.isDefault) || version.specifications[0] + const specification = getDefaultSpecification(version) NProgress.start() projectNavigator.navigate( project.owner, diff --git a/src/features/projects/domain/ProjectNavigator.ts b/src/features/projects/domain/ProjectNavigator.ts index f9094732..e3d03217 100644 --- a/src/features/projects/domain/ProjectNavigator.ts +++ b/src/features/projects/domain/ProjectNavigator.ts @@ -1,4 +1,5 @@ import Project from "./Project" +import { getDefaultSpecification } from "./Version" interface IPathnameReader { readonly pathname: string @@ -36,7 +37,7 @@ export default class ProjectNavigator { if (candidateSpecification) { this.router.push(`/${project.owner}/${project.name}/${newVersion.id}/${candidateSpecification.id}`) } else { - const defaultOrFirstSpecification = newVersion.specifications.find(spec => spec.isDefault) || newVersion.specifications[0] + const defaultOrFirstSpecification = getDefaultSpecification(newVersion) this.router.push(`/${project.owner}/${project.name}/${newVersion.id}/${defaultOrFirstSpecification.id}`) } } diff --git a/src/features/projects/domain/Version.ts b/src/features/projects/domain/Version.ts index 2eee9225..f6b69989 100644 --- a/src/features/projects/domain/Version.ts +++ b/src/features/projects/domain/Version.ts @@ -12,3 +12,7 @@ export const VersionSchema = z.object({ type Version = z.infer export default Version + +export function getDefaultSpecification(version: Version) { + return version.specifications.find((spec) => spec.isDefault) || version.specifications[0] +} diff --git a/src/features/projects/domain/getProjectSelectionFromPath.ts b/src/features/projects/domain/getProjectSelectionFromPath.ts index 7f56ba95..dfa5497e 100644 --- a/src/features/projects/domain/getProjectSelectionFromPath.ts +++ b/src/features/projects/domain/getProjectSelectionFromPath.ts @@ -1,5 +1,5 @@ import Project from "./Project" -import Version from "./Version" +import Version, { getDefaultSpecification } from "./Version" import OpenApiSpecification from "./OpenApiSpecification" export default function getProjectSelectionFromPath({ @@ -61,7 +61,7 @@ export default function getProjectSelectionFromPath({ if (specificationId && !didMoveSpecificationIdToVersionId) { specification = version.specifications.find(e => e.id == specificationId) } else if (version.specifications.length > 0) { - specification = version.specifications.find(spec => spec.isDefault) || version.specifications[0] + specification = getDefaultSpecification(version) } return { project, version, specification } } diff --git a/src/features/projects/domain/index.ts b/src/features/projects/domain/index.ts index 2b3f10cf..2bf08445 100644 --- a/src/features/projects/domain/index.ts +++ b/src/features/projects/domain/index.ts @@ -16,3 +16,4 @@ export { default as ProjectNavigator } from "./ProjectNavigator" export { default as ProjectRepository } from "./ProjectRepository" export { default as updateWindowTitle } from "./updateWindowTitle" export type { default as Version } from "./Version" +export { getDefaultSpecification } from "./Version"