Skip to content
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

ACM-2589 WIP: Convert AcmDropdown to use Menu #2820

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 2 additions & 4 deletions frontend/src/routes/Applications/Overview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* Copyright Contributors to the Open Cluster Management project */

import { PageSection, Text, TextContent, TextVariants, Tooltip } from '@patternfly/react-core'
import { DropdownPosition, PageSection, Text, TextContent, TextVariants, Tooltip } from '@patternfly/react-core'
import { ExternalLinkAltIcon, OutlinedQuestionCircleIcon } from '@patternfly/react-icons'
import { cellWidth } from '@patternfly/react-table'
import {
Expand Down Expand Up @@ -1023,8 +1022,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