diff --git a/src/pages/commonCreation/components/ProjectCreation/components/ProjectCreationForm/ProjectCreationForm.tsx b/src/pages/commonCreation/components/ProjectCreation/components/ProjectCreationForm/ProjectCreationForm.tsx index 544c75d27..80d89eee9 100644 --- a/src/pages/commonCreation/components/ProjectCreation/components/ProjectCreationForm/ProjectCreationForm.tsx +++ b/src/pages/commonCreation/components/ProjectCreation/components/ProjectCreationForm/ProjectCreationForm.tsx @@ -33,6 +33,7 @@ import { UnsavedChangesPrompt } from "../UnsavedChangesPrompt"; import { getConfiguration } from "./configuration"; import { ProjectCreationFormValues } from "./types"; import styles from "./ProjectCreationForm.module.scss"; +import { sortByTierDesc } from "@/shared/utils"; const NOTION_INTEGRATION_TOKEN_MASK = "************"; @@ -186,39 +187,46 @@ const ProjectCreationForm: FC = (props) => { const advancedSettings: SpaceAdvancedSettingsIntermediate = useMemo(() => { const initialCircles = Object.values(governance?.circles || {}); + const sortedRootCommonRoles = sortByTierDesc(rootCommonRoles); + + const sortedParentCommonRoles = sortByTierDesc(parentCommonRoles); + + const circlesWithInheritRoles = sortedRootCommonRoles.map((rootCommonRole, index) => { + const initialCircle = initialCircles.find( + (circle) => circle.derivedFrom?.circleId === rootCommonRole.circleId, + ); + + const isSelected = Boolean(!isEditing || initialCircle); + const roleForInheritance = + sortedParentCommonRoles.slice(0,2).find( + (parentRole) => parentRole.circleId === initialCircle?.inheritFrom?.circleId, + ) || sortedParentCommonRoles[index]; + + + return { + circleId: rootCommonRole.circleId, + circleName: `${rootCommonRole.circleName}s`, + selected: isSelected, + synced: Boolean( + isEditing ? initialCircle?.inheritGovernanceId : roleForInheritance ? true : false, + ), + ...(parentGovernanceId && + roleForInheritance && { + inheritFrom: { + governanceId: parentGovernanceId, + circleId: roleForInheritance.circleId, + circleName: `${roleForInheritance.circleName}s`, + tier: roleForInheritance.tier, + }, + }), + }; + }); + return { permissionGovernanceId: isParentIsRoot ? parentGovernanceId : rootGovernance?.id, - circles: rootCommonRoles.map((rootCommonRole, index) => { - const initialCircle = initialCircles.find( - (circle) => circle.derivedFrom?.circleId === rootCommonRole.circleId, - ); - const isSelected = Boolean(!isEditing || initialCircle); - const roleForInheritance = - parentCommonRoles.find( - (parentRole) => - parentRole.circleId === initialCircle?.inheritFrom?.circleId, - ) || parentCommonRoles[index]; - - return { - circleId: rootCommonRole.circleId, - circleName: `${rootCommonRole.circleName}s`, - selected: isSelected, - synced: Boolean( - isEditing ? initialCircle?.inheritGovernanceId : index === 0, - ), - ...(parentGovernanceId && - roleForInheritance && { - inheritFrom: { - governanceId: parentGovernanceId, - circleId: roleForInheritance.circleId, - circleName: `${roleForInheritance.circleName}s`, - tier: roleForInheritance.tier, - }, - }), - }; - }), + circles: circlesWithInheritRoles.reverse(), }; }, [ rootGovernance?.id, diff --git a/src/shared/utils/index.tsx b/src/shared/utils/index.tsx index ef6d925ba..085e0370a 100755 --- a/src/shared/utils/index.tsx +++ b/src/shared/utils/index.tsx @@ -45,5 +45,6 @@ export * from "./joinWithLast"; export * from "./getResizedFileUrl"; export * from "./areTimestampsEqual"; export * from "./parseMessageLink"; +export * from "./sortByTierDesc"; export * from "./generateOptimisticFeedItem"; export * from "./generateFirstMessage"; diff --git a/src/shared/utils/sortByTierDesc.ts b/src/shared/utils/sortByTierDesc.ts new file mode 100644 index 000000000..c05e6af79 --- /dev/null +++ b/src/shared/utils/sortByTierDesc.ts @@ -0,0 +1,8 @@ +type TierSortable = { + tier?: number | null; + }; + +export function sortByTierDesc(array: T[]): T[] { + return [...array].sort((a, b) => (b.tier ?? 0) - (a.tier ?? 0)); + } + \ No newline at end of file