import React, { useMemo } from 'react'; import { CategoryTreeNode, getId, getChildren as getNodeChildren } from 'wdk-client/Utils/CategoryUtils'; import { pure } from 'wdk-client/Utils/ComponentUtils'; import Dialog from 'wdk-client/Components/Overlays/Dialog'; import CategoriesCheckboxTree from 'wdk-client/Components/CheckboxTree/CategoriesCheckboxTree'; import { Answer, Question } from 'wdk-client/Utils/WdkModel'; import { flatMap, concat } from 'wdk-client/Utils/IterableUtils'; import { ShowHideAddColumnsDialog, UpdateColumnsDialogSelection, UpdateColumnsDialogSearchString, UpdateColumnsDialogExpandedNodes, RequestColumnsChoiceUpdate } from 'wdk-client/Views/ResultTableSummaryView/Types'; import { LinksPosition, } from '@veupathdb/coreui/dist/components/inputs/checkboxes/CheckboxTree/CheckboxTree'; import { getLeaves } from 'wdk-client/Utils/TreeUtils'; import { differenceWith } from 'lodash'; import { Tooltip } from '@veupathdb/components/lib/components/widgets/Tooltip'; const MAX_COLUMNS_ALLOWED = 80; export interface Props { answer: Answer; viewId: string; question: Question; columnsDialogIsOpen: boolean; columnsDialogSelection?: string[]; columnsDialogExpandedNodes?: string[]; columnsDialogSearchString?: string; columnsTree: CategoryTreeNode; showHideAddColumnsDialog: ShowHideAddColumnsDialog; updateColumnsDialogSelection: UpdateColumnsDialogSelection; updateColumnsDialogSearchString: UpdateColumnsDialogSearchString; updateColumnsDialogExpandedNodes: UpdateColumnsDialogExpandedNodes; requestColumnsChoiceUpdate: RequestColumnsChoiceUpdate; } function ResultTableAddColumnsDialog({ answer, question, columnsDialogExpandedNodes, columnsDialogIsOpen, columnsDialogSelection, columnsDialogSearchString = '', columnsTree, showHideAddColumnsDialog, updateColumnsDialogSelection, updateColumnsDialogSearchString, updateColumnsDialogExpandedNodes, requestColumnsChoiceUpdate, }: Props) { if (!columnsDialogIsOpen) return null; /** * This logic returns an accurate count of the checkboxes selected in the Dialog component. * Since columnsDialogSelection includes ids for non-selectable columns, the ids in columnsDialogSelection * that do not correspond with a checkbox are filtered out. */ const numberOfColumnsSelected = useMemo(() => { const checkboxIds = getLeaves(columnsTree, getNodeChildren).map(node => getId(node)); const filteredCheckboxIds = differenceWith(checkboxIds, columnsDialogSelection ?? []); return Math.abs(checkboxIds.length - filteredCheckboxIds.length) }, [columnsDialogSelection, columnsTree]) const areMaxColumnsExceeded = columnsDialogSelection && columnsDialogSelection?.length > MAX_COLUMNS_ALLOWED; const selectedColumnsMessage = (