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 type {ThrownError, TimelineData} from '../types';
    
  11. import type {
    
  12.   Interaction,
    
  13.   MouseMoveInteraction,
    
  14.   Rect,
    
  15.   Size,
    
  16.   ViewRefs,
    
  17. } from '../view-base';
    
  18. 
    
  19. import {
    
  20.   positioningScaleFactor,
    
  21.   timestampToPosition,
    
  22.   positionToTimestamp,
    
  23.   widthToDuration,
    
  24. } from './utils/positioning';
    
  25. import {
    
  26.   View,
    
  27.   Surface,
    
  28.   rectContainsPoint,
    
  29.   rectIntersectsRect,
    
  30.   intersectionOfRects,
    
  31. } from '../view-base';
    
  32. import {
    
  33.   COLORS,
    
  34.   TOP_ROW_PADDING,
    
  35.   REACT_EVENT_DIAMETER,
    
  36.   BORDER_SIZE,
    
  37. } from './constants';
    
  38. 
    
  39. const EVENT_ROW_HEIGHT_FIXED =
    
  40.   TOP_ROW_PADDING + REACT_EVENT_DIAMETER + TOP_ROW_PADDING;
    
  41. 
    
  42. export class ThrownErrorsView extends View {
    
  43.   _profilerData: TimelineData;
    
  44.   _intrinsicSize: Size;
    
  45.   _hoveredEvent: ThrownError | null = null;
    
  46.   onHover: ((event: ThrownError | null) => void) | null = null;
    
  47. 
    
  48.   constructor(surface: Surface, frame: Rect, profilerData: TimelineData) {
    
  49.     super(surface, frame);
    
  50.     this._profilerData = profilerData;
    
  51. 
    
  52.     this._intrinsicSize = {
    
  53.       width: this._profilerData.duration,
    
  54.       height: EVENT_ROW_HEIGHT_FIXED,
    
  55.     };
    
  56.   }
    
  57. 
    
  58.   desiredSize(): Size {
    
  59.     return this._intrinsicSize;
    
  60.   }
    
  61. 
    
  62.   setHoveredEvent(hoveredEvent: ThrownError | null) {
    
  63.     if (this._hoveredEvent === hoveredEvent) {
    
  64.       return;
    
  65.     }
    
  66.     this._hoveredEvent = hoveredEvent;
    
  67.     this.setNeedsDisplay();
    
  68.   }
    
  69. 
    
  70.   /**
    
  71.    * Draw a single `ThrownError` as a circle in the canvas.
    
  72.    */
    
  73.   _drawSingleThrownError(
    
  74.     context: CanvasRenderingContext2D,
    
  75.     rect: Rect,
    
  76.     thrownError: ThrownError,
    
  77.     baseY: number,
    
  78.     scaleFactor: number,
    
  79.     showHoverHighlight: boolean,
    
  80.   ) {
    
  81.     const {frame} = this;
    
  82.     const {timestamp} = thrownError;
    
  83. 
    
  84.     const x = timestampToPosition(timestamp, scaleFactor, frame);
    
  85.     const radius = REACT_EVENT_DIAMETER / 2;
    
  86.     const eventRect: Rect = {
    
  87.       origin: {
    
  88.         x: x - radius,
    
  89.         y: baseY,
    
  90.       },
    
  91.       size: {width: REACT_EVENT_DIAMETER, height: REACT_EVENT_DIAMETER},
    
  92.     };
    
  93.     if (!rectIntersectsRect(eventRect, rect)) {
    
  94.       return; // Not in view
    
  95.     }
    
  96. 
    
  97.     const fillStyle = showHoverHighlight
    
  98.       ? COLORS.REACT_THROWN_ERROR_HOVER
    
  99.       : COLORS.REACT_THROWN_ERROR;
    
  100. 
    
  101.     const y = eventRect.origin.y + radius;
    
  102. 
    
  103.     context.beginPath();
    
  104.     context.fillStyle = fillStyle;
    
  105.     context.arc(x, y, radius, 0, 2 * Math.PI);
    
  106.     context.fill();
    
  107.   }
    
  108. 
    
  109.   draw(context: CanvasRenderingContext2D) {
    
  110.     const {
    
  111.       frame,
    
  112.       _profilerData: {thrownErrors},
    
  113.       _hoveredEvent,
    
  114.       visibleArea,
    
  115.     } = this;
    
  116. 
    
  117.     context.fillStyle = COLORS.BACKGROUND;
    
  118.     context.fillRect(
    
  119.       visibleArea.origin.x,
    
  120.       visibleArea.origin.y,
    
  121.       visibleArea.size.width,
    
  122.       visibleArea.size.height,
    
  123.     );
    
  124. 
    
  125.     // Draw events
    
  126.     const baseY = frame.origin.y + TOP_ROW_PADDING;
    
  127.     const scaleFactor = positioningScaleFactor(
    
  128.       this._intrinsicSize.width,
    
  129.       frame,
    
  130.     );
    
  131. 
    
  132.     const highlightedEvents: ThrownError[] = [];
    
  133. 
    
  134.     thrownErrors.forEach(thrownError => {
    
  135.       if (thrownError === _hoveredEvent) {
    
  136.         highlightedEvents.push(thrownError);
    
  137.         return;
    
  138.       }
    
  139.       this._drawSingleThrownError(
    
  140.         context,
    
  141.         visibleArea,
    
  142.         thrownError,
    
  143.         baseY,
    
  144.         scaleFactor,
    
  145.         false,
    
  146.       );
    
  147.     });
    
  148. 
    
  149.     // Draw the highlighted items on top so they stand out.
    
  150.     // This is helpful if there are multiple (overlapping) items close to each other.
    
  151.     highlightedEvents.forEach(thrownError => {
    
  152.       this._drawSingleThrownError(
    
  153.         context,
    
  154.         visibleArea,
    
  155.         thrownError,
    
  156.         baseY,
    
  157.         scaleFactor,
    
  158.         true,
    
  159.       );
    
  160.     });
    
  161. 
    
  162.     // Render bottom borders.
    
  163.     // Propose border rect, check if intersects with `rect`, draw intersection.
    
  164.     const borderFrame: Rect = {
    
  165.       origin: {
    
  166.         x: frame.origin.x,
    
  167.         y: frame.origin.y + EVENT_ROW_HEIGHT_FIXED - BORDER_SIZE,
    
  168.       },
    
  169.       size: {
    
  170.         width: frame.size.width,
    
  171.         height: BORDER_SIZE,
    
  172.       },
    
  173.     };
    
  174.     if (rectIntersectsRect(borderFrame, visibleArea)) {
    
  175.       const borderDrawableRect = intersectionOfRects(borderFrame, visibleArea);
    
  176.       context.fillStyle = COLORS.REACT_WORK_BORDER;
    
  177.       context.fillRect(
    
  178.         borderDrawableRect.origin.x,
    
  179.         borderDrawableRect.origin.y,
    
  180.         borderDrawableRect.size.width,
    
  181.         borderDrawableRect.size.height,
    
  182.       );
    
  183.     }
    
  184.   }
    
  185. 
    
  186.   /**
    
  187.    * @private
    
  188.    */
    
  189.   _handleMouseMove(interaction: MouseMoveInteraction, viewRefs: ViewRefs) {
    
  190.     const {frame, onHover, visibleArea} = this;
    
  191.     if (!onHover) {
    
  192.       return;
    
  193.     }
    
  194. 
    
  195.     const {location} = interaction.payload;
    
  196.     if (!rectContainsPoint(location, visibleArea)) {
    
  197.       onHover(null);
    
  198.       return;
    
  199.     }
    
  200. 
    
  201.     const {
    
  202.       _profilerData: {thrownErrors},
    
  203.     } = this;
    
  204.     const scaleFactor = positioningScaleFactor(
    
  205.       this._intrinsicSize.width,
    
  206.       frame,
    
  207.     );
    
  208.     const hoverTimestamp = positionToTimestamp(location.x, scaleFactor, frame);
    
  209.     const eventTimestampAllowance = widthToDuration(
    
  210.       REACT_EVENT_DIAMETER / 2,
    
  211.       scaleFactor,
    
  212.     );
    
  213. 
    
  214.     // Because data ranges may overlap, we want to find the last intersecting item.
    
  215.     // This will always be the one on "top" (the one the user is hovering over).
    
  216.     for (let index = thrownErrors.length - 1; index >= 0; index--) {
    
  217.       const event = thrownErrors[index];
    
  218.       const {timestamp} = event;
    
  219. 
    
  220.       if (
    
  221.         timestamp - eventTimestampAllowance <= hoverTimestamp &&
    
  222.         hoverTimestamp <= timestamp + eventTimestampAllowance
    
  223.       ) {
    
  224.         this.currentCursor = 'context-menu';
    
  225.         viewRefs.hoveredView = this;
    
  226.         onHover(event);
    
  227.         return;
    
  228.       }
    
  229.     }
    
  230. 
    
  231.     onHover(null);
    
  232.   }
    
  233. 
    
  234.   handleInteraction(interaction: Interaction, viewRefs: ViewRefs) {
    
  235.     switch (interaction.type) {
    
  236.       case 'mousemove':
    
  237.         this._handleMouseMove(interaction, viewRefs);
    
  238.         break;
    
  239.     }
    
  240.   }
    
  241. }