-
Notifications
You must be signed in to change notification settings - Fork 610
refactor: optimize roles #3324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
refactor: optimize roles #3324
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a86e64c
refactor: optimize roles table
ogzhanolguncu b25a049
refactor: how we update and display connected keys and perms
ogzhanolguncu 6cb400d
fix: type names
ogzhanolguncu 978a2c7
fix: use same attach limit for related componetns
ogzhanolguncu 686489a
fix: typo
ogzhanolguncu 8bcae0f
refactor: handle edge maximum keys and permissions case
ogzhanolguncu 90694c3
chore: fmt
ogzhanolguncu f387098
Merge branch 'main' into refactor-optimize-roles
ogzhanolguncu 250dc10
fix: pr comments
ogzhanolguncu 84be909
fix: coderabbit issues
ogzhanolguncu 7550841
fix: merge
ogzhanolguncu 5694260
fix: conflicts
ogzhanolguncu 156a952
refactor: make invalidation and icons consistent
ogzhanolguncu 2538a9f
fix: wording
ogzhanolguncu e0cd6cc
fix: wording
ogzhanolguncu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 83 additions & 27 deletions
110
...d/app/(app)/authorization/permissions/components/table/components/assigned-items-cell.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,115 @@ | ||
| import { cn } from "@/lib/utils"; | ||
| import { Page2, Tag } from "@unkey/icons"; | ||
| import { Key2, Page2, Tag } from "@unkey/icons"; | ||
|
|
||
| export const AssignedItemsCell = ({ | ||
| items, | ||
| totalCount, | ||
| value, | ||
| isSelected = false, | ||
| type, | ||
| kind, | ||
| }: { | ||
| items: string[]; | ||
| totalCount?: number; | ||
| value?: string; | ||
| isSelected?: boolean; | ||
| type: "roles" | "slug"; | ||
| kind: "roles" | "keys" | "permissions" | "slug"; | ||
| }) => { | ||
| const hasMore = totalCount && totalCount > items.length; | ||
| const icon = | ||
| type === "roles" ? <Tag size="md-regular" /> : <Page2 className="size-3" size="md-regular" />; | ||
| const getIcon = () => { | ||
| switch (kind) { | ||
| case "roles": | ||
| return <Tag size="md-regular" className="opacity-50" />; | ||
| case "keys": | ||
| return <Key2 size="md-regular" className="opacity-50" />; | ||
| case "slug": | ||
| return <Page2 size="md-regular" className="opacity-50" />; | ||
| default: | ||
| throw new Error(`Invalid type: ${kind}`); | ||
| } | ||
| }; | ||
|
|
||
| const getDisplayText = (count: number) => { | ||
| if (count === 1) { | ||
| switch (kind) { | ||
| case "roles": | ||
| return "Role"; | ||
| case "keys": | ||
| return "Key"; | ||
| case "permissions": | ||
| return "Permission"; | ||
| default: | ||
| throw new Error(`Invalid type: ${kind}`); | ||
| } | ||
| } | ||
|
|
||
| switch (kind) { | ||
| case "roles": | ||
| return "Roles"; | ||
| case "keys": | ||
| return "Keys"; | ||
| case "permissions": | ||
| return "Permissions"; | ||
| default: | ||
| throw new Error(`Invalid type: ${kind}`); | ||
| } | ||
| }; | ||
|
|
||
| const itemClassName = cn( | ||
| "font-mono rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 transition-all duration-100 border border-dashed text-grayA-12", | ||
| isSelected ? "bg-grayA-4 border-grayA-7" : "bg-grayA-3 border-grayA-6 group-hover:bg-grayA-4", | ||
| ); | ||
|
|
||
| const emptyClassName = cn( | ||
| "rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 transition-all duration-100 border border-dashed bg-grayA-2 ", | ||
| "rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 transition-all duration-100 border border-dashed bg-grayA-2", | ||
| isSelected ? "border-grayA-7 text-grayA-9" : "border-grayA-6 text-grayA-8", | ||
| ); | ||
|
|
||
| if (items.length === 0) { | ||
| if (kind === "slug") { | ||
| if (!value) { | ||
| return ( | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px] animate-in fade-in slide-in-from-top-2 duration-300"> | ||
| <div className={emptyClassName}> | ||
| {getIcon()} | ||
| <span className="text-grayA-9 text-xs">No slug</span> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px] animate-in fade-in slide-in-from-top-2 duration-300"> | ||
| <div | ||
| className={cn(itemClassName, "animate-in fade-in slide-in-from-left-2")} | ||
| style={{ animationDelay: "50ms" }} | ||
| > | ||
| {getIcon()} | ||
| <div className="text-grayA-11 text-xs max-w-[150px] truncate" title={value}> | ||
| {value} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| if (!totalCount) { | ||
| return ( | ||
| <div className="flex flex-col gap-1 py-1 max-w-[200px]"> | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px] animate-in fade-in slide-in-from-top-2 duration-300"> | ||
| <div className={emptyClassName}> | ||
| {icon} | ||
| {getIcon()} | ||
| <span className="text-grayA-9 text-xs">None assigned</span> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px]"> | ||
| {items.map((item) => ( | ||
| <div className={itemClassName} key={item}> | ||
| {icon} | ||
| <span className="text-grayA-11 text-xs max-w-[150px] truncate" title={item}> | ||
| {item} | ||
| </span> | ||
| </div> | ||
| ))} | ||
| {hasMore && ( | ||
| <div className={itemClassName}> | ||
| <span className="text-grayA-9 text-xs max-w-[150px] truncate"> | ||
| {totalCount - items.length} more permissions... | ||
| </span> | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px] animate-in fade-in slide-in-from-top-2 duration-300"> | ||
| <div | ||
| className={cn(itemClassName, "animate-in fade-in slide-in-from-left-2")} | ||
| style={{ animationDelay: "50ms" }} | ||
| > | ||
| {getIcon()} | ||
| <div className="text-grayA-11 text-xs max-w-[150px] truncate"> | ||
| {totalCount} {getDisplayText(totalCount)} | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 18 additions & 16 deletions
34
...omponents/table/components/actions/components/hooks/use-fetch-connected-keys-and-perms.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,24 @@ | ||
| "use client"; | ||
| import { useRoleLimits } from "@/app/(app)/authorization/roles/components/table/hooks/use-role-limits"; | ||
| import { trpc } from "@/lib/trpc/client"; | ||
|
|
||
| export const useFetchConnectedKeysAndPerms = (roleId: string) => { | ||
| const { data, isLoading, error, refetch } = | ||
| trpc.authorization.roles.connectedKeysAndPerms.useQuery( | ||
| { | ||
| roleId, | ||
| }, | ||
| { | ||
| enabled: Boolean(roleId), | ||
| }, | ||
| ); | ||
| export const useFetchConnectedKeysAndPermsData = (roleId: string) => { | ||
| const { calculateLimits } = useRoleLimits(roleId); | ||
| const { shouldPrefetch } = calculateLimits(); | ||
|
|
||
| const query = trpc.authorization.roles.connectedKeysAndPerms.useQuery( | ||
| { roleId }, | ||
| { | ||
| enabled: shouldPrefetch && Boolean(roleId), | ||
| staleTime: 5 * 60 * 1000, | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| keys: data?.keys || [], | ||
| permissions: data?.permissions || [], | ||
| isLoading, | ||
| error, | ||
| refetch, | ||
| keys: query.data?.keys || [], | ||
| permissions: query.data?.permissions || [], | ||
| hasData: Boolean(query.data), | ||
| isLoading: query.isLoading, | ||
| isError: query.isError, | ||
| error: query.error, | ||
| }; | ||
ogzhanolguncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
4 changes: 2 additions & 2 deletions
4
...pp/(app)/authorization/roles/components/table/components/actions/components/role-info.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.