1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import * as React from 'react';
    
  11. import {memo, useCallback, useContext} from 'react';
    
  12. import {areEqual} from 'react-window';
    
  13. import {minBarWidth} from './constants';
    
  14. import {getGradientColor} from './utils';
    
  15. import ChartNode from './ChartNode';
    
  16. import {SettingsContext} from '../Settings/SettingsContext';
    
  17. 
    
  18. import type {ItemData} from './CommitRanked';
    
  19. 
    
  20. type Props = {
    
  21.   data: ItemData,
    
  22.   index: number,
    
  23.   style: Object,
    
  24.   ...
    
  25. };
    
  26. 
    
  27. function CommitRankedListItem({data, index, style}: Props) {
    
  28.   const {
    
  29.     chartData,
    
  30.     onElementMouseEnter,
    
  31.     onElementMouseLeave,
    
  32.     scaleX,
    
  33.     selectedFiberIndex,
    
  34.     selectFiber,
    
  35.     width,
    
  36.   } = data;
    
  37. 
    
  38.   const node = chartData.nodes[index];
    
  39. 
    
  40.   const {lineHeight} = useContext(SettingsContext);
    
  41. 
    
  42.   const handleClick = useCallback(
    
  43.     (event: $FlowFixMe) => {
    
  44.       event.stopPropagation();
    
  45.       const {id, name} = node;
    
  46.       selectFiber(id, name);
    
  47.     },
    
  48.     [node, selectFiber],
    
  49.   );
    
  50. 
    
  51.   const handleMouseEnter = () => {
    
  52.     const {id, name} = node;
    
  53.     onElementMouseEnter({id, name});
    
  54.   };
    
  55. 
    
  56.   const handleMouseLeave = () => {
    
  57.     onElementMouseLeave();
    
  58.   };
    
  59. 
    
  60.   // List items are absolutely positioned using the CSS "top" attribute.
    
  61.   // The "left" value will always be 0.
    
  62.   // Since height is fixed, and width is based on the node's duration,
    
  63.   // We can ignore those values as well.
    
  64.   const top = parseInt(style.top, 10);
    
  65. 
    
  66.   return (
    
  67.     <ChartNode
    
  68.       color={getGradientColor(node.value / chartData.maxValue)}
    
  69.       height={lineHeight}
    
  70.       isDimmed={index < selectedFiberIndex}
    
  71.       key={node.id}
    
  72.       label={node.label}
    
  73.       onClick={handleClick}
    
  74.       onMouseEnter={handleMouseEnter}
    
  75.       onMouseLeave={handleMouseLeave}
    
  76.       width={Math.max(minBarWidth, scaleX(node.value, width))}
    
  77.       x={0}
    
  78.       y={top}
    
  79.     />
    
  80.   );
    
  81. }
    
  82. 
    
  83. export default (memo(
    
  84.   CommitRankedListItem,
    
  85.   areEqual,
    
  86. ): React.ComponentType<Props>);