| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {ErrorBoundary} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 11 | import {ThemedComponentsRoot} from 'isl-components/ThemedComponentsRoot'; |
| b69ab31 | | | 12 | import {ViewportOverlayRoot} from 'isl-components/ViewportOverlay'; |
| b69ab31 | | | 13 | import {Provider, useAtomValue, useStore} from 'jotai'; |
| b69ab31 | | | 14 | import React from 'react'; |
| b69ab31 | | | 15 | import {ContextMenus} from 'shared/ContextMenu'; |
| b69ab31 | | | 16 | import {ISLCommandContext} from './ISLShortcuts'; |
| b69ab31 | | | 17 | import {SuspenseBoundary} from './SuspenseBoundary'; |
| b69ab31 | | | 18 | import {TopLevelToast} from './TopLevelToast'; |
| b69ab31 | | | 19 | import {enableReactTools, enableReduxTools} from './atoms/debugToolAtoms'; |
| b69ab31 | | | 20 | import {I18nSupport} from './i18n'; |
| b69ab31 | | | 21 | import {setJotaiStore} from './jotaiUtils'; |
| b69ab31 | | | 22 | import platform from './platform'; |
| b69ab31 | | | 23 | import {DEFAULT_RESET_CSS} from './resetStyle'; |
| b69ab31 | | | 24 | import {zoomUISettingAtom} from './responsive'; |
| b69ab31 | | | 25 | import {themeState} from './theme'; |
| b69ab31 | | | 26 | import {ModalContainer} from './useModal'; |
| b69ab31 | | | 27 | import {usePromise} from './usePromise'; |
| b69ab31 | | | 28 | import {isDev, isTest} from './utils'; |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | export function AllProviders({children}: {children: ReactNode}) { |
| b69ab31 | | | 31 | return ( |
| b69ab31 | | | 32 | <React.StrictMode> |
| b69ab31 | | | 33 | <ResetStyle /> |
| b69ab31 | | | 34 | <I18nSupport> |
| b69ab31 | | | 35 | <MaybeWithJotaiRoot> |
| b69ab31 | | | 36 | <ISLRoot> |
| b69ab31 | | | 37 | <ISLCommandContext> |
| b69ab31 | | | 38 | <ErrorBoundary> |
| b69ab31 | | | 39 | {children} |
| b69ab31 | | | 40 | <ViewportOverlayRoot /> |
| b69ab31 | | | 41 | <ModalContainer /> |
| b69ab31 | | | 42 | <ContextMenus /> |
| b69ab31 | | | 43 | <TopLevelToast /> |
| b69ab31 | | | 44 | </ErrorBoundary> |
| b69ab31 | | | 45 | </ISLCommandContext> |
| b69ab31 | | | 46 | </ISLRoot> |
| b69ab31 | | | 47 | </MaybeWithJotaiRoot> |
| b69ab31 | | | 48 | </I18nSupport> |
| b69ab31 | | | 49 | </React.StrictMode> |
| b69ab31 | | | 50 | ); |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | function ResetStyle() { |
| b69ab31 | | | 54 | const resetCSS = platform.theme?.resetCSS ?? DEFAULT_RESET_CSS; |
| b69ab31 | | | 55 | return resetCSS.length > 0 ? <style>{resetCSS}</style> : null; |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | function ISLRoot({children}: {children: ReactNode}) { |
| b69ab31 | | | 59 | const theme = useAtomValue(themeState); |
| b69ab31 | | | 60 | useAtomValue(zoomUISettingAtom); |
| b69ab31 | | | 61 | return ( |
| b69ab31 | | | 62 | <div onDragEnter={handleDragAndDrop} onDragOver={handleDragAndDrop}> |
| b69ab31 | | | 63 | <ThemedComponentsRoot className="isl-root" theme={theme}> |
| b69ab31 | | | 64 | {children} |
| b69ab31 | | | 65 | </ThemedComponentsRoot> |
| b69ab31 | | | 66 | </div> |
| b69ab31 | | | 67 | ); |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | function handleDragAndDrop(e: React.DragEvent<HTMLDivElement>) { |
| b69ab31 | | | 71 | // VS Code tries to capture drag & drop events to open files. But if you're dragging |
| b69ab31 | | | 72 | // on ISL, you probably want to do an ImageUpload. Prevent this event from propagating to vscode. |
| b69ab31 | | | 73 | if (e.dataTransfer?.types?.some(t => t === 'Files')) { |
| b69ab31 | | | 74 | e.stopPropagation(); |
| b69ab31 | | | 75 | e.preventDefault(); |
| b69ab31 | | | 76 | e.dataTransfer.dropEffect = 'copy'; |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | } |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | function MaybeWithJotaiRoot({children}: {children: JSX.Element}) { |
| b69ab31 | | | 81 | if (isTest) { |
| b69ab31 | | | 82 | // Use a new store when re-mounting so each test (that calls `render(<App />)`) |
| b69ab31 | | | 83 | // starts with a clean state. |
| b69ab31 | | | 84 | return ( |
| b69ab31 | | | 85 | <Provider> |
| b69ab31 | | | 86 | <AccessJotaiRoot /> |
| b69ab31 | | | 87 | {children} |
| b69ab31 | | | 88 | </Provider> |
| b69ab31 | | | 89 | ); |
| b69ab31 | | | 90 | } else if (isDev) { |
| b69ab31 | | | 91 | return <MaybeJotaiDebugTools>{children}</MaybeJotaiDebugTools>; |
| b69ab31 | | | 92 | } else { |
| b69ab31 | | | 93 | // Such scoped Provider or store complexity is not needed outside tests or dev. |
| b69ab31 | | | 94 | return children; |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | const jotaiDevtools = import('./third-party/jotai-devtools/utils'); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | function MaybeJotaiDebugTools({children}: {children: JSX.Element}) { |
| b69ab31 | | | 100 | const enabledRedux = useAtomValue(enableReduxTools); |
| b69ab31 | | | 101 | const enabledReact = useAtomValue(enableReactTools); |
| b69ab31 | | | 102 | return enabledRedux || enabledReact ? ( |
| b69ab31 | | | 103 | <SuspenseBoundary> |
| b69ab31 | | | 104 | {enabledRedux ? <AtomsDevtools>{children}</AtomsDevtools> : children} |
| b69ab31 | | | 105 | {enabledReact && <DebugAtoms />} |
| b69ab31 | | | 106 | </SuspenseBoundary> |
| b69ab31 | | | 107 | ) : ( |
| b69ab31 | | | 108 | children |
| b69ab31 | | | 109 | ); |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | function AtomsDevtools({children}: {children: JSX.Element}) { |
| b69ab31 | | | 113 | const {useAtomsDevtools} = usePromise(jotaiDevtools); |
| b69ab31 | | | 114 | useAtomsDevtools('jotai'); |
| b69ab31 | | | 115 | return children; |
| b69ab31 | | | 116 | } |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | function DebugAtoms() { |
| b69ab31 | | | 119 | const {useAtomsDebugValue} = usePromise(jotaiDevtools); |
| b69ab31 | | | 120 | useAtomsDebugValue(); |
| b69ab31 | | | 121 | return null; |
| b69ab31 | | | 122 | } |
| b69ab31 | | | 123 | |
| b69ab31 | | | 124 | function AccessJotaiRoot() { |
| b69ab31 | | | 125 | const store = useStore(); |
| b69ab31 | | | 126 | setJotaiStore(store); |
| b69ab31 | | | 127 | return null; |
| b69ab31 | | | 128 | } |