| 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 { |
| 12 | useCallback, |
| 13 | useContext, |
| 14 | unstable_useCacheRefresh as useCacheRefresh, |
| 15 | } from 'react'; |
| 16 | import ErrorBoundary from '../ErrorBoundary'; |
| 17 | import {TreeStateContext} from './TreeContext'; |
| 18 | import {clearCacheBecauseOfError} from '../../../inspectedElementCache'; |
| 19 | import styles from './InspectedElementErrorBoundary.css'; |
| 20 | |
| 21 | type WrapperProps = { |
| 22 | children: React$Node, |
| 23 | }; |
| 24 | |
| 25 | export default function InspectedElementErrorBoundaryWrapper({ |
| 26 | children, |
| 27 | }: WrapperProps): React.Node { |
| 28 | // Key on the selected element ID so that changing the selected element automatically hides the boundary. |
| 29 | // This seems best since an error inspecting one element isn't likely to be relevant to another element. |
| 30 | const {inspectedElementID} = useContext(TreeStateContext); |
| 31 | |
| 32 | const refresh = useCacheRefresh(); |
| 33 | const handleDsmiss = useCallback(() => { |
| 34 | clearCacheBecauseOfError(refresh); |
| 35 | }, [refresh]); |
| 36 | |
| 37 | return ( |
| 38 | <div className={styles.Wrapper}> |
| 39 | <ErrorBoundary |
| 40 | key={inspectedElementID} |
| 41 | canDismiss={true} |
| 42 | onBeforeDismissCallback={handleDsmiss}> |
| 43 | {children} |
| 44 | </ErrorBoundary> |
| 45 | </div> |
| 46 | ); |
| 47 | } |