Skip to content

Commit

Permalink
WIP: Convert AcmDropdown to use Menu
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Cormier <[email protected]>
  • Loading branch information
KevinFCormier committed May 15, 2023
1 parent e4dcbb0 commit 4f2e29a
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 261 deletions.
42 changes: 30 additions & 12 deletions frontend/src/components/Rbac.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import { makeStyles } from '@mui/styles'
import { createSubjectAccessReview, ResourceAttributes } from '../resources'
import { AcmButton, AcmDropdown } from '../ui-components'
import { useEffect, useState } from 'react'
import { AcmButton, AcmDropdown, AcmDropdownItems } from '../ui-components'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useTranslation } from '../lib/acm-i18next'
import { Actions } from '../routes/Infrastructure/Clusters/ManagedClusters/CreateCluster/components/assisted-installer/hypershift/common/common'
import { DropdownPosition } from '@patternfly/react-core'

type RbacDropdownProps<T = unknown> = {
actions: Actions<T>[]
Expand All @@ -14,17 +16,23 @@ type RbacDropdownProps<T = unknown> = {
id: string
isDisabled?: boolean
tooltip?: string
dropdownPosition?: DropdownPosition
}

type Actions<T = unknown> = {
id: string
text: React.ReactNode
isAriaDisabled?: boolean
tooltip?: string
click: (item: T) => void
type Actions<T = unknown> = AcmDropdownItems & {
flyoutMenu?: Actions<T>[]
click?: (item: T) => void
rbac?: ResourceAttributes[] | Promise<ResourceAttributes>[]
}

function flattenAction<T>(action: Actions<T>): Actions<T> | Actions<T>[] {
return action.flyoutMenu ? flattenActions(action.flyoutMenu) : action
}

function flattenActions<T>(actions: Actions<T>[]): Actions<T>[] {
return actions.flatMap(flattenAction)
}

export function RbacDropdown<T = unknown>(props: RbacDropdownProps<T>) {
const { t } = useTranslation()
const [actions, setActions] = useState<Actions<T>[]>([])
Expand All @@ -36,10 +44,19 @@ export function RbacDropdown<T = unknown>(props: RbacDropdownProps<T>) {
}
}, [actions, props.actions])

const onSelect = (id: string) => {
const action = props.actions.find((a) => a.id === id)
return action?.click(props.item)
}
const actionsWithFlyoutActions = useMemo(() => {
return flattenActions<T>(actions)
}, [actions])

const onSelect = useCallback(
(id: string) => {
const action = actionsWithFlyoutActions.find((a) => a.id === id)
if (action?.click) {
action.click(props.item)
}
},
[actionsWithFlyoutActions, props.item]
)

const onToggle = async (isOpen?: boolean) => {
if (isOpen) {
Expand Down Expand Up @@ -75,6 +92,7 @@ export function RbacDropdown<T = unknown>(props: RbacDropdownProps<T>) {
id={props.id}
onSelect={onSelect}
dropdownItems={actions}
dropdownPosition={props.dropdownPosition}
isKebab={props.isKebab}
isPlain={true}
text={props.text}
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/routes/Applications/Overview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright Contributors to the Open Cluster Management project */

import { PageSection, Text, TextContent, TextVariants } from '@patternfly/react-core'
import { DropdownPosition, PageSection, Text, TextContent, TextVariants } from '@patternfly/react-core'
import { ExternalLinkAltIcon } from '@patternfly/react-icons'
import { cellWidth } from '@patternfly/react-table'
import {
Expand Down Expand Up @@ -989,8 +989,7 @@ export default function ApplicationsOverview() {
isKebab={false}
isPlain={false}
isPrimary={true}
// tooltipPosition={tableDropdown.tooltipPosition}
// dropdownPosition={DropdownPosition.left}
dropdownPosition={DropdownPosition.left}
/>
),
[canCreateApplication, history, t]
Expand Down
Loading

0 comments on commit 4f2e29a

Please sign in to comment.