From 10531d1badeec1a749ec774424881a55a55d2e36 Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Tue, 1 Oct 2024 12:59:30 +0300 Subject: [PATCH] CW-sync-all-roles Added sync all roles logic with protection for difference in parent/child roles amount --- .../ProjectCreationForm.tsx | 66 +++++++++++-------- src/shared/utils/index.tsx | 1 + src/shared/utils/sortByTierDesc.ts | 8 +++ 3 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 src/shared/utils/sortByTierDesc.ts 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 af54f66cf..ba64ec5cc 100755 --- a/src/shared/utils/index.tsx +++ b/src/shared/utils/index.tsx @@ -45,3 +45,4 @@ export * from "./joinWithLast"; export * from "./getResizedFileUrl"; export * from "./areTimestampsEqual"; export * from "./parseMessageLink"; +export * from "./sortByTierDesc"; \ No newline at end of file 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