Home / File/ useOpenResource.js — react Source File

useOpenResource.js — react Source File

Architecture documentation for useOpenResource.js, a javascript file in the react codebase. 9 imports, 4 dependents.

File javascript BabelCompiler 9 imports 4 dependents

Entity Profile

Dependency Diagram

graph LR
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe["useOpenResource.js"]
  10e77c43_8f60_bbc4_f7e7_41da67a4fac9["ViewElementSourceContext.js"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> 10e77c43_8f60_bbc4_f7e7_41da67a4fac9
  0de29888_ca49_eb3c_33f7_ab4fff46de0b["utils.js"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> 0de29888_ca49_eb3c_33f7_ab4fff46de0b
  92c8d128_26b7_05c1_1f27_a9a5ab236fc6["useEditorURL.js"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> 92c8d128_26b7_05c1_1f27_a9a5ab236fc6
  19776675_fb50_d3e9_2e67_e3dcc7805e1e["useEditorURL"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> 19776675_fb50_d3e9_2e67_e3dcc7805e1e
  bcb71f5e_1ecf_5620_9a9e_55d6ab61dd1f["constants.js"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> bcb71f5e_1ecf_5620_9a9e_55d6ab61dd1f
  df34d6bc_88ee_9d70_cb1b_fbe686dca6da["utils.js"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> df34d6bc_88ee_9d70_cb1b_fbe686dca6da
  3c81519b_a855_b45e_b91b_c1dcaaf5d5b9["checkConditions"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> 3c81519b_a855_b45e_b91b_c1dcaaf5d5b9
  d8f20c67_f5fa_0f0a_c967_c41fd9ffce07["ReactTypes"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> d8f20c67_f5fa_0f0a_c967_c41fd9ffce07
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  50c39210_7831_dde4_3a12_fc8ea644bc38["InspectedElementSourcePanel.js"]
  50c39210_7831_dde4_3a12_fc8ea644bc38 --> 4d6a124e_fdc4_9da9_8ac7_85f75cd106fe
  e1b4c141_a4b7_37b7_6892_efa866707492["InspectedElementViewSourceButton.js"]
  e1b4c141_a4b7_37b7_6892_efa866707492 --> 4d6a124e_fdc4_9da9_8ac7_85f75cd106fe
  7db6a3fc_5457_73ae_9df6_4bd78494320f["StackTraceView.js"]
  7db6a3fc_5457_73ae_9df6_4bd78494320f --> 4d6a124e_fdc4_9da9_8ac7_85f75cd106fe
  8a53e3b1_7d98_d6cb_d776_c4171cd9ac5f["SidebarEventInfo.js"]
  8a53e3b1_7d98_d6cb_d776_c4171cd9ac5f --> 4d6a124e_fdc4_9da9_8ac7_85f75cd106fe
  style 4d6a124e_fdc4_9da9_8ac7_85f75cd106fe fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */

import type {ReactFunctionLocation, ReactCallSite} from 'shared/ReactTypes';

import {useCallback, useContext, useSyncExternalStore} from 'react';

import ViewElementSourceContext from './Components/ViewElementSourceContext';

import {getAlwaysOpenInEditor} from '../../utils';
import useEditorURL from './useEditorURL';
import {LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR} from '../../constants';

import {checkConditions} from './Editor/utils';

const useOpenResource = (
  source: null | ReactFunctionLocation | ReactCallSite,
  symbolicatedSource: null | ReactFunctionLocation | ReactCallSite,
): [
  boolean, // isEnabled
  () => void, // Open Resource
] => {
  const {canViewElementSourceFunction, viewElementSourceFunction} = useContext(
    ViewElementSourceContext,
  );

  const editorURL = useEditorURL();

  const alwaysOpenInEditor = useSyncExternalStore(
    useCallback(function subscribe(callback) {
      window.addEventListener(LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR, callback);
      return function unsubscribe() {
        window.removeEventListener(
          LOCAL_STORAGE_ALWAYS_OPEN_IN_EDITOR,
          callback,
        );
      };
    }, []),
    getAlwaysOpenInEditor,
  );

  // First check if this link is eligible for being open directly in the configured editor.
  const openInEditor =
    alwaysOpenInEditor && source !== null
      ? checkConditions(editorURL, symbolicatedSource || source)
      : null;
  // In some cases (e.g. FB internal usage) the standalone shell might not be able to view the source.
  // To detect this case, we defer to an injected helper function (if present).
  const linkIsEnabled =
    (openInEditor !== null && !openInEditor.shouldDisableButton) ||
    (viewElementSourceFunction != null &&
      source != null &&
      (canViewElementSourceFunction == null ||
        canViewElementSourceFunction(source, symbolicatedSource)));

  const viewSource = useCallback(() => {
    if (openInEditor !== null && !openInEditor.shouldDisableButton) {
      // If we have configured to always open in the code editor, we do so if we can.
      // Otherwise, we fallback to open in the local editor if possible (e.g. non-file urls).
      window.open(openInEditor.url);
    } else if (viewElementSourceFunction != null && source != null) {
      viewElementSourceFunction(source, symbolicatedSource);
    }
  }, [openInEditor, source, symbolicatedSource]);

  return [linkIsEnabled, viewSource];
};

export default useOpenResource;

Domain

Frequently Asked Questions

What does useOpenResource.js do?
useOpenResource.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain.
What does useOpenResource.js depend on?
useOpenResource.js imports 9 module(s): ReactTypes, ViewElementSourceContext.js, checkConditions, constants.js, react, useEditorURL, useEditorURL.js, utils.js, and 1 more.
What files import useOpenResource.js?
useOpenResource.js is imported by 4 file(s): InspectedElementSourcePanel.js, InspectedElementViewSourceButton.js, SidebarEventInfo.js, StackTraceView.js.
Where is useOpenResource.js in the architecture?
useOpenResource.js is located at packages/react-devtools-shared/src/devtools/views/useOpenResource.js (domain: BabelCompiler, directory: packages/react-devtools-shared/src/devtools/views).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free