-
Notifications
You must be signed in to change notification settings - Fork 943
feat: add resource-action pills to custom roles table #14354
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
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a61d3f
feat: add resource-action pills to custom roles table
jaaydenh 83c5b09
fix: remove permission from theme and change name to colorRoles
jaaydenh d78f9a7
fix: revert name from colorRoles to roles
jaaydenh a7b63ba
fix: format
jaaydenh 437bbd0
fix: custom role with no permissions
jaaydenh 9514f30
feat: extract permissions pull list component and add tests
jaaydenh 9497583
chore: undo color roles name change
jaaydenh fae3881
feat: add experimental pill colors
jaaydenh de5e171
fix: format
jaaydenh af142e3
chore: update experiment name
jaaydenh 1b10a76
chore: cleanup
jaaydenh 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
feat: extract permissions pull list component and add tests
- Loading branch information
commit 9514f3000772b3ed4e21776cdaf9c1466eab5d5c
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
site/src/pages/ManagementSettingsPage/CustomRolesPage/CustomRolesPageView.stories.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
52 changes: 52 additions & 0 deletions
52
site/src/pages/ManagementSettingsPage/CustomRolesPage/PermissionPillsList.stories.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { userEvent, within } from "@storybook/test"; | ||
import { MockRoleWithOrgPermissions } from "testHelpers/entities"; | ||
import { PermissionPillsList } from "./PermissionPillsList"; | ||
|
||
const meta: Meta<typeof PermissionPillsList> = { | ||
title: "pages/OrganizationCustomRolesPage/PermissionPillsList", | ||
component: PermissionPillsList, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof PermissionPillsList>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
permissions: MockRoleWithOrgPermissions.organization_permissions, | ||
}, | ||
}; | ||
|
||
export const SinglePermission: Story = { | ||
args: { | ||
permissions: [ | ||
{ | ||
negate: false, | ||
resource_type: "organization_member", | ||
action: "create", | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export const NoPermissions: Story = { | ||
args: { | ||
permissions: [], | ||
}, | ||
}; | ||
|
||
export const HoverOverflowPill: Story = { | ||
args: { | ||
permissions: MockRoleWithOrgPermissions.organization_permissions, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
await userEvent.hover(canvas.getByTestId("overflow-permissions-pill")); | ||
}, | ||
}; | ||
|
||
export const ShowAllResources: Story = { | ||
args: { | ||
permissions: MockRoleWithOrgPermissions.organization_permissions, | ||
}, | ||
}; |
137 changes: 137 additions & 0 deletions
137
site/src/pages/ManagementSettingsPage/CustomRolesPage/PermissionPillsList.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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { type Interpolation, type Theme, useTheme } from "@emotion/react"; | ||
import Stack from "@mui/material/Stack"; | ||
import type { Permission, Role } from "api/typesGenerated"; | ||
import { Pill } from "components/Pill/Pill"; | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from "components/Popover/Popover"; | ||
import type { FC } from "react"; | ||
|
||
function getUniqueResourceTypes(jsonObject: readonly Permission[]) { | ||
const resourceTypes = jsonObject.map((item) => item.resource_type); | ||
return [...new Set(resourceTypes)]; | ||
} | ||
|
||
interface PermissionPillsListProps { | ||
permissions: readonly Permission[]; | ||
} | ||
|
||
export const PermissionPillsList: FC<PermissionPillsListProps> = ({ | ||
permissions, | ||
}) => { | ||
const resourceTypes: string[] = getUniqueResourceTypes(permissions); | ||
|
||
return ( | ||
<Stack direction="row" spacing={1}> | ||
{permissions.length > 0 ? ( | ||
<PermissionsPill | ||
resource={resourceTypes[0]} | ||
permissions={permissions} | ||
/> | ||
) : ( | ||
<p>None</p> | ||
)} | ||
|
||
{resourceTypes.length > 1 && ( | ||
<OverflowPermissionPill | ||
resources={resourceTypes.slice(1)} | ||
permissions={permissions.slice(1)} | ||
/> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
interface PermissionPillProps { | ||
resource: string; | ||
permissions: readonly Permission[]; | ||
} | ||
|
||
const PermissionsPill: FC<PermissionPillProps> = ({ | ||
resource, | ||
permissions, | ||
}) => { | ||
const actions = permissions.filter((p) => { | ||
if (resource === p.resource_type) { | ||
return p.action; | ||
} | ||
}); | ||
jaaydenh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<Pill css={styles.permissionPill}> | ||
<b>{resource}</b>: {actions.map((p) => p.action).join(", ")} | ||
</Pill> | ||
); | ||
}; | ||
|
||
type OverflowPermissionPillProps = { | ||
resources: string[]; | ||
permissions: readonly Permission[]; | ||
}; | ||
|
||
const OverflowPermissionPill: FC<OverflowPermissionPillProps> = ({ | ||
resources, | ||
permissions, | ||
}) => { | ||
const theme = useTheme(); | ||
|
||
return ( | ||
<Popover mode="hover"> | ||
<PopoverTrigger> | ||
<Pill | ||
css={{ | ||
backgroundColor: theme.palette.background.paper, | ||
borderColor: theme.palette.divider, | ||
}} | ||
data-testid="overflow-permissions-pill" | ||
> | ||
+{resources.length} more | ||
</Pill> | ||
</PopoverTrigger> | ||
|
||
<PopoverContent | ||
disableRestoreFocus | ||
disableScrollLock | ||
css={{ | ||
".MuiPaper-root": { | ||
display: "flex", | ||
flexFlow: "column wrap", | ||
columnGap: 8, | ||
rowGap: 12, | ||
padding: "12px 16px", | ||
alignContent: "space-around", | ||
minWidth: "auto", | ||
backgroundColor: theme.palette.background.default, | ||
}, | ||
}} | ||
anchorOrigin={{ | ||
vertical: -4, | ||
horizontal: "center", | ||
}} | ||
transformOrigin={{ | ||
vertical: "bottom", | ||
horizontal: "center", | ||
}} | ||
> | ||
{resources.map((resource) => ( | ||
<PermissionsPill | ||
key={resource} | ||
resource={resource} | ||
permissions={permissions} | ||
/> | ||
))} | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
}; | ||
|
||
const styles = { | ||
permissionPill: (theme) => ({ | ||
backgroundColor: theme.roles.default.background, | ||
borderColor: theme.roles.default.outline, | ||
color: theme.roles.default.text, | ||
width: "fit-content", | ||
}), | ||
} satisfies Record<string, Interpolation<Theme>>; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this type annotation here still necessary? maybe if TypeScript is having trouble inferring the type here you could add
: string[]
to the function declarationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not necessary, just leftover from implementation work.