import { Group } from '@visx/group'; import type { HierarchyPointNode } from '@visx/hierarchy/lib/types'; import { gt, isNil, pluck } from 'ramda'; import type { MouseEvent as ReactMouseEvent } from 'react'; import { useState } from 'react'; import type { BaseProp, Node, TreeProps } from './models'; interface Props extends Pick, 'children'> { descendants: Array>>; expandCollapseNode: (targetNode: Node) => void; getExpanded: (d: Node) => Array> | undefined; nodeSize: { height: number; width: number; }; } const DescendantNodes = ({ descendants, children, expandCollapseNode, getExpanded, nodeSize }: Props): Array => { const [pressEventTimeStamp, setPressEventTimeStamp] = useState( null ); const mouseDown = (e: ReactMouseEvent): void => { setPressEventTimeStamp(e.timeStamp); }; const mouseUp = (callback: () => void) => (e: ReactMouseEvent): void => { if (isNil(pressEventTimeStamp)) { callback(); return; } const diffTimeStamp = e.timeStamp - pressEventTimeStamp; if (gt(diffTimeStamp, 120)) { return; } callback(); }; return descendants.map((node) => { const top = node.x; const left = node.y; const ancestorIds = node .ancestors() .map((ancestor) => ancestor.data.data.id); const descendantIds = node .descendants() .map((ancestor) => ancestor.data.data.id); const key = `${node.data.data.id}-${node.data.data.name}-${ancestorIds.toString()}-${descendantIds.toString()}`; return ( {children({ ancestors: pluck('data', node.ancestors()), depth: node.depth, expandCollapseNode, isExpanded: !!getExpanded(node.data), node: node.data, nodeSize, onMouseDown: mouseDown, onMouseUp: mouseUp })} ); }); }; export default DescendantNodes;