Skip to content

fix: show shallow permissions followed by nested in alphabetical order#2273

Merged
chronark merged 7 commits intounkeyed:mainfrom
DeepaPrasanna:fix/show-shallow-permissions-first
Oct 17, 2024
Merged

fix: show shallow permissions followed by nested in alphabetical order#2273
chronark merged 7 commits intounkeyed:mainfrom
DeepaPrasanna:fix/show-shallow-permissions-first

Conversation

@DeepaPrasanna
Copy link
Contributor

@DeepaPrasanna DeepaPrasanna commented Oct 8, 2024

What does this PR do?

Fixes #1011
Before:

Screenshot 2024-10-08 154904

After:

Screenshot 2024-10-08 155136

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • Create some shallow permissions and nested ones. Now, shallow permissions should be ordered first than nested in alphabetical order.

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features

    • Introduced a sorting mechanism for permissions in the RolesPage component, enhancing the organization and readability of permission listings.
  • Bug Fixes

    • Improved the handling of permission names by sorting them based on length and alphabetical order, ensuring a more intuitive user experience.

@changeset-bot
Copy link

changeset-bot bot commented Oct 8, 2024

⚠️ No Changeset found

Latest commit: aa7cdde

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Oct 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 17, 2024 0:59am
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 17, 2024 0:59am
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 17, 2024 0:59am
workflows ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 17, 2024 0:59am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 17, 2024 0:59am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes made in this pull request introduce a new sorting mechanism for permissions within the RolesPage component located in apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx. The permissions are now sorted first by the length of their names and then lexicographically, replacing the previous direct iteration over the permissions. This adjustment ensures that non-nested permissions appear before nested ones, aligning with the specified requirements.

Changes

File Path Change Summary
apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx Introduced sorting for permissions based on name length and lexicographical order. Added sortNestedPermissions function to categorize and sort permissions. Updated Tree component to use sorted permissions.

Assessment against linked issues

Objective Addressed Explanation
Permission ordering should show shallow permissions first (#1011)

Possibly related PRs

  • fix: Sort Permissions #2165: The changes in this PR also involve sorting permissions, specifically by their name property, which aligns with the sorting mechanism introduced in the main PR for permissions in the RolesPage component.

Suggested reviewers

  • chronark
  • mcstepp
  • perkinsjr

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel
Copy link

vercel bot commented Oct 8, 2024

@DeepaPrasanna is attempting to deploy a commit to the Unkey Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added 🕹️ 150 points Bug Something isn't working Good first issue Good for newcomers labels Oct 8, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2051528 and f41ed44.

📒 Files selected for processing (1)
  • apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (1 hunks)
🧰 Additional context used
🔇 Additional comments (3)
apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (3)

46-55: LGTM: Sorting logic implementation is correct and aligns with PR objectives.

The sorting mechanism effectively ensures that shallow permissions (those with fewer parts in their names) appear before nested ones, and then sorts them alphabetically. This implementation correctly addresses the issue #1011 and meets the PR objectives.


58-58: LGTM: Correct usage of sorted permissions in the loop.

The for loop now correctly iterates over the sortedPermissions array, ensuring that the nested object is constructed in the desired order (shallow permissions first, then nested, all alphabetically sorted within their groups).


46-58: Summary: Changes successfully implement required permission sorting.

The introduced changes effectively address the issue #1011 and meet the PR objectives. The sorting mechanism ensures that shallow permissions are displayed before nested ones, with both groups sorted alphabetically. The implementation is well-integrated into the existing component structure and doesn't introduce unnecessary complexity.

Key points:

  1. Correct sorting logic implementation (lines 46-55).
  2. Proper usage of sorted permissions in the nested object construction (line 58).
  3. Minimal impact on the overall component structure.

The changes look good to merge, with the optional consideration of the previously suggested performance optimization if needed in the future.

Copy link
Collaborator

@chronark chronark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your "after" screenshot, shouldn't the nested a be above api?

CleanShot 2024-10-08 at 15 46 15@2x

I think we want single permissions above nested, but then sort alphabetically

@DeepaPrasanna
Copy link
Contributor Author

@DeepaPrasanna
Copy link
Contributor Author

Is this good?
image

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (2)

17-43: Excellent implementation of permission sorting!

The sortNestedPermissions function effectively separates shallow and nested permissions, sorts them alphabetically, and combines them in the required order. This aligns perfectly with the PR objectives and the requirements from the linked issue #1011.

Consider a minor optimization:

function sortNestedPermissions(nested: NestedPermissions) {
  const [shallowPermissions, nestedPermissions] = Object.entries(nested).reduce(
    ([shallow, nested], [key, value]) => {
      (Object.keys(value.permissions).length > 0 ? nested : shallow)[key] = value;
      return [shallow, nested];
    },
    [{} as NestedPermissions, {} as NestedPermissions]
  );

  return {
    ...Object.fromEntries(Object.entries(shallowPermissions).sort(([a], [b]) => a.localeCompare(b))),
    ...Object.fromEntries(Object.entries(nestedPermissions).sort(([a], [b]) => a.localeCompare(b)))
  };
}

This version reduces the number of iterations and uses more functional programming concepts, which might be more concise and potentially more performant for larger permission sets.


74-83: Well-implemented sorting logic for permissions!

The sorting mechanism effectively ensures that shallow permissions appear before nested ones, with both categories sorted alphabetically. This implementation aligns perfectly with the PR objectives and the requirements from the linked issue #1011.

For improved clarity, consider extracting the sorting logic into a separate function:

const sortPermissions = (a: Permission, b: Permission) => {
  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 sortedPermissions = workspace.permissions.sort(sortPermissions);

This separation of concerns makes the code more readable and easier to maintain.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f41ed44 and 91a19e9.

📒 Files selected for processing (1)
  • apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (4 hunks)
🧰 Additional context used
🔇 Additional comments (1)
apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (1)

86-86: Excellent integration of the new sorting logic!

The changes effectively incorporate the new sorting mechanisms into the existing component structure:

  1. Using sortedPermissions to build the nested structure ensures that permissions are initially in the correct order.
  2. Applying sortNestedPermissions to the nested object further refines the ordering.
  3. Passing sortedNestedPermissions to the Tree component ensures that the UI reflects the desired permission order.

These changes successfully implement the requirements from the linked issue #1011 and meet the PR objectives.

Also applies to: 106-107, 125-125

@chronark chronark enabled auto-merge October 10, 2024 09:58
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (2)

17-43: LGTM! Consider a minor optimization for object creation.

The sortNestedPermissions function effectively implements the requirement to show shallow permissions before nested ones, both sorted alphabetically. The logic is clear and well-structured.

For a minor optimization, consider using Object.fromEntries() when creating the final sorted object:

const sortedObject: NestedPermissions = Object.fromEntries([
  ...sortedShallowKeys.map(key => [key, shallowPermissions[key]]),
  ...sortedNestedKeys.map(key => [key, nestedPermissions[key]])
]);

This approach creates the object in a single step, potentially improving performance for large permission sets.


74-83: LGTM! Consider extracting the comparison logic for better readability.

The sorting logic correctly implements the requirement to show shallow permissions before nested ones, with alphabetical ordering within each group. The approach is effective and locale-aware.

For improved readability, consider extracting the comparison logic into a separate function:

const comparePermissions = (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 sortedPermissions = workspace.permissions.sort(comparePermissions);

This extraction would make the sorting logic more self-documenting and easier to maintain.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 91a19e9 and d99209b.

📒 Files selected for processing (1)
  • apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (4 hunks)
🧰 Additional context used
🔇 Additional comments (1)
apps/dashboard/app/(app)/authorization/roles/[roleId]/page.tsx (1)

86-86: LGTM! The changes correctly implement the new sorting requirements.

The modifications effectively integrate the new sorting logic into the component:

  1. Using sortedPermissions to build the nested object ensures correct initial ordering.
  2. Applying sortNestedPermissions to nested further refines the order.
  3. Passing sortedNestedPermissions to the Tree component ensures the UI reflects the new sorting.

These changes successfully fulfill the PR objectives of showing shallow permissions before nested ones, both in alphabetical order.

To ensure the changes are correctly applied throughout the codebase, let's verify the usage of the Tree component:

This will help confirm that the sorting changes are consistently applied wherever the Tree component is used.

Also applies to: 106-106, 125-125

@DeepaPrasanna
Copy link
Contributor Author

Hello @chronark, it seems even if you enabled auto merge few days ago, this wasn't merged. Is there anything else I could do with respect to this issue?

@chronark chronark disabled auto-merge October 17, 2024 12:50
@chronark chronark merged commit 771a76e into unkeyed:main Oct 17, 2024
@oss-gg
Copy link

oss-gg bot commented Oct 17, 2024

Awarding DeepaPrasanna: 150 points 🕹️ Well done! Check out your new contribution on oss.gg/DeepaPrasanna

@DeepaPrasanna DeepaPrasanna deleted the fix/show-shallow-permissions-first branch October 17, 2024 12:54
Flo4604 pushed a commit to Flo4604/unkey that referenced this pull request Oct 19, 2024
unkeyed#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>
github-merge-queue bot pushed a commit that referenced this pull request Oct 20, 2024
* fix: Missing plan check and ip whitelist parsing

* fix: adjust tests for ipwhitelist

* fix: Rename error code

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix: ipwhitelist via features vs enterprise plan

* fix: invert condition

* chore: add cache log

* fix: ensure workspace is loaded (#2470)

* chore: more logging and retries (#2475)

* Update 7_create_a_template.md (#2471)

* increase override limit

* Update create-new-override.tsx

* fix: Enhance API Key Detail Page: Change Permissions Visualization to Tree Format (#2238)

* fix:changed the permission view

* fixed issue comments

* [autofix.ci] apply automated fixes

* removed font

---------

Co-authored-by: Andreas Thomas <dev@chronark.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

* fix: show shallow permissions followed by nested in alphabetical order (#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>

* Add Template to Markdown (#2362)

Co-authored-by: Andreas Thomas <dev@chronark.com>

* fix: retry on any error with disabled cache

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: chronark <dev@chronark.com>
Co-authored-by: Chirag Arora <chirag8023@gmail.com>
Co-authored-by: RajuGangitla <gangitlaraju8520@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Anne Deepa Prasanna <anneraj73@gmail.com>
Co-authored-by: djnovin <95838226+djnovin@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working Good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Permission ordering should show shallow permissions first

2 participants