import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import { alpha, TableCell, type TableCellBaseProps, type TableCellProps, type Theme, useTheme } from '@mui/material'; import type { ListingVariant } from '@centreon/ui-context'; import { useAtom } from 'jotai'; import { equals, includes, isNil, reject } from 'ramda'; import type { ElementType } from 'react'; import type { CSSObject } from 'tss-react'; import { IconButton } from '../..'; import { subItemsPivotsAtom } from '../tableAtoms'; import { getTextStyleByViewMode } from '../useStyleTable'; import type { Props as DataCellProps } from './DataCell'; interface GetBackgroundColorProps extends Omit { theme: Theme; } interface GetRowHighlightStyleProps { isRowHighlighted?: boolean; theme: Theme; disableRowCondition: (row: Record) => boolean; row?: Record; } const getBackgroundColor = ({ isRowHovered, row, rowColorConditions, disableRowCondition, theme }: GetBackgroundColorProps): string => { if (disableRowCondition(row as Record)) { return alpha(theme.palette.common.black, theme.palette.action.focusOpacity); } if (isRowHovered) { return alpha(theme.palette.primary.main, theme.palette.action.focusOpacity); } const foundCondition = rowColorConditions?.find(({ condition }) => condition(row as Record) ); if (!isNil(foundCondition)) { return foundCondition.color; } return 'unset'; }; const getRowTextColor = ({ isRowHighlighted, theme, disableRowCondition, row }: GetRowHighlightStyleProps): CSSObject | undefined => { if (isRowHighlighted) { return { color: theme.palette.text.primary }; } if (disableRowCondition(row as Record)) { return { color: alpha(theme.palette.text.secondary, 0.5) }; } return undefined; }; interface Props extends Pick< DataCellProps, 'isRowHovered' | 'rowColorConditions' | 'disableRowCondition' >, TableCellProps { displaySubItemsCaret?: boolean; isRowHighlighted?: boolean; labelCollapse?: string; labelExpand?: string; listingVariant?: ListingVariant; row?: Record; subItemsRowProperty?: string; } const isPivotExistInTheList = ( id: number | string ): ((list: Array) => boolean) => includes(id); const handleSubItems = ({ currentSubItemsPivots, id }: { currentSubItemsPivots: Array; id: number | string; }): Array => { if (isPivotExistInTheList(id)(currentSubItemsPivots)) { return reject(equals(id), currentSubItemsPivots); } return [...currentSubItemsPivots, id]; }; const Cell = ({ displaySubItemsCaret, subItemsRowProperty, labelCollapse, labelExpand, disableRowCondition, isRowHovered, isRowHighlighted, rowColorConditions, listingVariant, row, style, ...props }: Props): JSX.Element => { const theme = useTheme(); const [subItemsPivots, setSubItemsPivots] = useAtom(subItemsPivotsAtom); const { children } = props; const rowId = row?.id as number | string | undefined; const click = (e: React.MouseEvent): void => { e.preventDefault(); e.stopPropagation(); setSubItemsPivots((currentSubItemsPivots) => handleSubItems({ currentSubItemsPivots, id: rowId as number | string }) ); }; const isSubItemsExpanded = isPivotExistInTheList(rowId as number | string)( subItemsPivots ); const hasSubItems = Boolean( subItemsRowProperty && row?.[subItemsRowProperty] ); return ( } style={ { backgroundColor: getBackgroundColor({ disableRowCondition, isRowHovered, row, rowColorConditions, theme }), ...getTextStyleByViewMode({ listingVariant, theme }), ...getRowTextColor({ disableRowCondition, isRowHighlighted, row, theme }), ...style } as React.CSSProperties } {...props} > {displaySubItemsCaret && hasSubItems && ( )} {children} ); }; export default Cell;