import React, { useEffect, useRef } from 'react'; import { ActionCellProps } from '../ActionCell'; import { useExtensionMenuItems } from '../../hooks/useExtensionMenuItems'; import { FiltersMap } from '../../exports/core'; import { KanbanBaseProps, KanbanState } from '../../exports/alpha'; import { ItemActionsState } from '../../state'; import { IconButton, PopoverMenu, PopoverMenuItemProps, } from '@wix/design-system'; import { getDefaultActionCellProps } from '../CollectionItemActions/getDefaultActionCellProps'; import { MoreSmall } from '@wix/wix-ui-icons-common'; import { DisabledActionCellMenu } from '../ActionCell/DisabledActionCellMenu'; import { createKanbanPrimaryActionCellPropsWithBI, createKanbanSecondaryActionCellPropsWithBI, reportOnKanbanPopoverMenuShow, } from './KanbanCardActionsHelper'; interface CardActionCell { actionCell: KanbanBaseProps['actionCell']; originalItem: T; itemIndex: number; itemActionsState: ItemActionsState; stageKey: string; cardKey: string; state: KanbanState; } export function evaluateActionCell({ actionCell, originalItem, itemIndex, itemActionsState, }: Omit< CardActionCell, 'stageKey' | 'cardKey' | 'state' >): ActionCellProps { if (!actionCell) { return {}; } if (typeof actionCell === 'function') { return actionCell(originalItem, itemIndex, itemActionsState.actionCellAPI); } return actionCell; } export function useCardActions({ actionCell, originalItem, itemIndex, itemActionsState, stageKey, cardKey, state, }: CardActionCell) { const actionCellConfig = evaluateActionCell({ actionCell, originalItem, itemIndex, itemActionsState, }); const extensionMenuItems = useExtensionMenuItems({ containerId: actionCellConfig.containerId, }); const secondaryActionCellPropsWithBI = createKanbanSecondaryActionCellPropsWithBI({ state, actionCellProps: actionCellConfig, keyedItem: itemActionsState.keyedItem, extensionMenuItems, stageId: stageKey, }); const primaryActionCellPropsWithBI = createKanbanPrimaryActionCellPropsWithBI( { state, actionCellProps: actionCellConfig, keyedItem: itemActionsState.keyedItem, stageId: stageKey, }, ); const popoverCloseRef = useRef< ((event: React.SyntheticEvent) => void) | null >(null); useEffect(() => { const closePopoverOnScroll = () => { if (popoverCloseRef.current) { const scrollEvent = new Event('scroll') as any; popoverCloseRef.current(scrollEvent); } }; const stageCardsContainer = document.querySelector( `[data-hook="kanban-stage-cards-container-${stageKey}"]`, ); if ( stageCardsContainer && typeof stageCardsContainer.addEventListener === 'function' ) { stageCardsContainer.addEventListener('scroll', closePopoverOnScroll, { passive: true, }); return () => { if (typeof stageCardsContainer.removeEventListener === 'function') { stageCardsContainer.removeEventListener( 'scroll', closePopoverOnScroll, ); } }; } return () => {}; }, [stageKey]); let secondaryActions: React.ReactNode | null = null; if (actionCellConfig.secondaryMenuDisabled) { secondaryActions = ( } ariaLabel={state.container.translate('cairo.moreActions')} /> ); } else if ( secondaryActionCellPropsWithBI && secondaryActionCellPropsWithBI.length > 0 ) { secondaryActions = ( { popoverCloseRef.current = close; return ( { e.stopPropagation(); toggle(); reportOnKanbanPopoverMenuShow({ state, keyedItem: itemActionsState.keyedItem, stageId: stageKey, }); }} priority="secondary" size="tiny" > ); }} > {secondaryActionCellPropsWithBI.map((action, index) => { if (action.divider) { return ; } const { icon: prefixIcon, skin, ...rest } = action; return ( ); })} ); } return { secondaryActions, primaryActionProps: primaryActionCellPropsWithBI, }; }