// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React from 'react' import {FormattedMessage, useIntl} from 'react-intl' import {BoardGroup, IPropertyTemplate} from '../../blocks/board' import {BoardView} from '../../blocks/boardView' import mutator from '../../mutator' import Button from '../../widgets/buttons/button' import Menu from '../../widgets/menu' import MenuWrapper from '../../widgets/menuWrapper' import CheckIcon from '../../widgets/icons/check' import HideIcon from '../../widgets/icons/hide' import ShowIcon from '../../widgets/icons/show' import {useAppSelector} from '../../store/hooks' import {getCurrentViewCardsSortedFilteredAndGrouped} from '../../store/cards' import {getVisibleAndHiddenGroups} from '../../boardUtils' import propsRegistry from '../../properties' type Props = { properties: readonly IPropertyTemplate[] activeView: BoardView groupByProperty?: IPropertyTemplate } const ViewHeaderGroupByMenu = (props: Props) => { const {properties, activeView, groupByProperty} = props const intl = useIntl() const cards = useAppSelector(getCurrentViewCardsSortedFilteredAndGrouped) const {visible: visibleGroups, hidden: hiddenGroups} = getVisibleAndHiddenGroups(cards, activeView.fields.visibleOptionIds, activeView.fields.hiddenOptionIds, groupByProperty) const emptyVisibleGroups = visibleGroups.filter((g) => !g.cards.length) const emptyVisibleGroupsCount = emptyVisibleGroups.length const hiddenGroupsCount = hiddenGroups.length const handleToggleGroups = (show: boolean) => { const getColumnIds = (groups: BoardGroup[]) => groups.map((g) => g.option.id) if (show) { const columnsToShow = getColumnIds(hiddenGroups) mutator.unhideViewColumns(activeView.boardId, activeView, columnsToShow) } else { const columnsToHide = getColumnIds(emptyVisibleGroups) mutator.hideViewColumns(activeView.boardId, activeView, columnsToHide) } } return ( {activeView.fields.viewType === 'table' && activeView.fields.groupById && <> {emptyVisibleGroupsCount > 0 && } onClick={() => handleToggleGroups(false)} />} {hiddenGroupsCount > 0 && ) } export default React.memo(ViewHeaderGroupByMenu)