import React from 'react' import { useIntl } from '@cultureamp/i18n-react-intl' import { Button, IconButton } from '~components/ButtonV1' import { Icon } from '~components/Icon' import { Menu, MenuList } from '~components/MenuV1' import styles from '../TitleBlock.module.scss' import { TITLE_BLOCK_ZEN_SECONDARY_MENU_HTML_ID } from '../constants' import { type SecondaryActionsProps, type TitleBlockMenuItemProps } from '../types' import { convertSecondaryActionsToMenuItems } from '../utils' import { TitleBlockMenuItem } from './TitleBlockMenuItem' import { Toolbar } from './Toolbar' type Props = { secondaryActions?: SecondaryActionsProps secondaryOverflowMenuItems?: TitleBlockMenuItemProps[] reversed?: boolean } const renderSecondaryOverflowMenu = ( menuAriaLabel: string, menuLabel: string, secondaryOverflowMenuItems?: TitleBlockMenuItemProps[], reversed?: boolean, ): JSX.Element | undefined => { if (!secondaryOverflowMenuItems || secondaryOverflowMenuItems.length === 0) return undefined return (
} iconPosition={'end'} /> } > {secondaryOverflowMenuItems.map((menuItem, i) => ( ))}
) } // New: combined overflow menu (secondary actions converted + overflow menu items) const renderCombinedSecondaryOverflowMenu = ( combinedMenuLabel: string, secondaryActions?: SecondaryActionsProps, secondaryOverflowMenuItems?: TitleBlockMenuItemProps[], reversed?: boolean, ): JSX.Element | undefined => { const secondaryConverted = secondaryActions ? convertSecondaryActionsToMenuItems(secondaryActions) : [] const overflowItems = secondaryOverflowMenuItems ?? [] if (secondaryConverted.length === 0 && overflowItems.length === 0) return undefined const combined = [...secondaryConverted, ...overflowItems] return (
} id={`${TITLE_BLOCK_ZEN_SECONDARY_MENU_HTML_ID}__combined`} /> } > {combined.map((menuItem, i) => ( ))}
) } // Unfortunately, you'll notice below, that I needed to use the array index, // against react best practices (https://reactjs.org/docs/lists-and-keys.html) // This is because the menu items have no unique identifier. export const SecondaryActions = ({ secondaryActions, secondaryOverflowMenuItems, reversed = false, }: Props): JSX.Element | null => { const { formatMessage } = useIntl() if (!secondaryActions && !secondaryOverflowMenuItems) return null const menuLabel = formatMessage({ id: 'kzTitleBlock.overflowMenuLabel', defaultMessage: 'Menu', description: 'Label for the dropdown menu which displays additional action items', }) const menuAriaLabel = formatMessage({ id: 'kzTitleBlock.overflowMenuAriaLabel', defaultMessage: 'Opens overflow menu', description: 'Aria Label for the dropdown menu which displays additional action items', }) const combinedMenuLabel = formatMessage({ id: 'kzTitleBlock.combinedOverflowMenuLabel', defaultMessage: 'Open combined secondary and overflow menu', description: 'Label for the dropdown menu which displays additional action items and secondary actions', }) const secondaryActionsAsToolbarItems = secondaryActions ? secondaryActions.map((action, i) => { if ('menuItems' in action) { return { key: `titleblock_secondary_action_${i}`, node: (
} iconPosition="end" /> } > {action.menuItems.map((menuItem, i2) => ( ))}
), } } else { if ('onClick' in action && 'href' in action) { // eslint-disable-next-line no-console console.warn( '\u001b[33m \nTITLE BLOCK WARNING:\nSecondary actions only support ' + 'either an href or an onClick, not both simultaneously.\n', ) } return { key: `${i}`, node: (
), } } }) : [] const overflowMenu = renderSecondaryOverflowMenu( menuAriaLabel, menuLabel, secondaryOverflowMenuItems, reversed, ) const combinedOverflowMenu = renderCombinedSecondaryOverflowMenu( combinedMenuLabel, secondaryActions, secondaryOverflowMenuItems, reversed, ) const toolbarItems = [ ...secondaryActionsAsToolbarItems, ...(overflowMenu ? [ { key: 'overflowMenu', node: overflowMenu, }, ] : []), ...(combinedOverflowMenu ? [ { key: 'combinedOverflowMenu', node: combinedOverflowMenu, }, ] : []), ] return (
) } SecondaryActions.displayName = 'SecondaryActions'