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 Agent from 'react-devtools-shared/src/backend/agent';
    
  11. 
    
  12. import Overlay from './Overlay';
    
  13. 
    
  14. const SHOW_DURATION = 2000;
    
  15. 
    
  16. let timeoutID: TimeoutID | null = null;
    
  17. let overlay: Overlay | null = null;
    
  18. 
    
  19. export function hideOverlay(agent: Agent) {
    
  20.   if (window.document == null) {
    
  21.     agent.emit('hideNativeHighlight');
    
  22.     return;
    
  23.   }
    
  24.   timeoutID = null;
    
  25. 
    
  26.   if (overlay !== null) {
    
  27.     overlay.remove();
    
  28.     overlay = null;
    
  29.   }
    
  30. }
    
  31. 
    
  32. export function showOverlay(
    
  33.   elements: Array<HTMLElement> | null,
    
  34.   componentName: string | null,
    
  35.   agent: Agent,
    
  36.   hideAfterTimeout: boolean,
    
  37. ) {
    
  38.   if (window.document == null) {
    
  39.     if (elements != null && elements[0] != null) {
    
  40.       agent.emit('showNativeHighlight', elements[0]);
    
  41.     }
    
  42.     return;
    
  43.   }
    
  44. 
    
  45.   if (timeoutID !== null) {
    
  46.     clearTimeout(timeoutID);
    
  47.   }
    
  48. 
    
  49.   if (elements == null) {
    
  50.     return;
    
  51.   }
    
  52. 
    
  53.   if (overlay === null) {
    
  54.     overlay = new Overlay(agent);
    
  55.   }
    
  56. 
    
  57.   overlay.inspect(elements, componentName);
    
  58. 
    
  59.   if (hideAfterTimeout) {
    
  60.     timeoutID = setTimeout(() => hideOverlay(agent), SHOW_DURATION);
    
  61.   }
    
  62. }