Skip to content

Commit

Permalink
fix: show shallow permissions followed by nested in alphabetical order (
Browse files Browse the repository at this point in the history
#2273)

* fix: show shallow permissions followed by nested in alphabetical order

* fix: correct the sorting of nested permissions top level keys

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
DeepaPrasanna and autofix-ci[bot] authored Oct 17, 2024
1 parent fb848c0 commit 771a76e
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ type Props = {
};
};

function sortNestedPermissions(nested: NestedPermissions) {
const shallowPermissions: NestedPermissions = {};
const nestedPermissions: NestedPermissions = {};

for (const [key, value] of Object.entries(nested)) {
if (Object.keys(value.permissions).length > 0) {
nestedPermissions[key] = value;
} else {
shallowPermissions[key] = value;
}
}

const sortedShallowKeys = Object.keys(shallowPermissions).sort();
const sortedNestedKeys = Object.keys(nestedPermissions).sort();

const sortedObject: NestedPermissions = {};

for (const key of sortedShallowKeys) {
sortedObject[key] = shallowPermissions[key];
}

for (const key of sortedNestedKeys) {
sortedObject[key] = nestedPermissions[key];
}

return sortedObject;
}

export default async function RolesPage(props: Props) {
const tenantId = getTenantId();

Expand Down Expand Up @@ -43,8 +71,19 @@ export default async function RolesPage(props: Props) {
return notFound();
}

const sortedPermissions = workspace.permissions.sort((a, b) => {
const aParts = a.name.split(".");
const bParts = b.name.split(".");

if (aParts.length !== bParts.length) {
return aParts.length - bParts.length;
}

return a.name.localeCompare(b.name);
});

const nested: NestedPermissions = {};
for (const permission of workspace.permissions) {
for (const permission of sortedPermissions) {
let n = nested;
const parts = permission.name.split(".");
for (let i = 0; i < parts.length; i++) {
Expand All @@ -64,6 +103,8 @@ export default async function RolesPage(props: Props) {
}
}

const sortedNestedPermissions = sortNestedPermissions(nested);

return (
<div className="flex flex-col min-h-screen gap-8">
<div className="flex items-center justify-between">
Expand All @@ -81,7 +122,7 @@ export default async function RolesPage(props: Props) {
</div>
</div>

<Tree nestedPermissions={nested} role={{ id: role.id }} />
<Tree nestedPermissions={sortedNestedPermissions} role={{ id: role.id }} />
</div>
);
}

0 comments on commit 771a76e

Please sign in to comment.