| 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 {AppMode, RepositoryError} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {ErrorBoundary, ErrorNotice} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {atom, useAtomValue, useSetAtom} from 'jotai'; |
| b69ab31 | | | 14 | import {Suspense, useEffect, useMemo} from 'react'; |
| b69ab31 | | | 15 | import {useThrottledEffect} from 'shared/hooks'; |
| b69ab31 | | | 16 | import {AllProviders} from './AppWrapper'; |
| b69ab31 | | | 17 | import {CommandHistoryAndProgress} from './CommandHistoryAndProgress'; |
| b69ab31 | | | 18 | import {CommitInfoSidebar} from './CommitInfoView/CommitInfoView'; |
| b69ab31 | | | 19 | import {CommitTreeList} from './CommitTreeList'; |
| b69ab31 | | | 20 | import {ComparisonViewApp, ComparisonViewModal} from './ComparisonView/ComparisonViewModal'; |
| b69ab31 | | | 21 | import {availableCwds, CwdSelections} from './CwdSelector'; |
| b69ab31 | | | 22 | import {Drawers} from './Drawers'; |
| b69ab31 | | | 23 | import {EmptyState} from './EmptyState'; |
| b69ab31 | | | 24 | import {useCommand} from './ISLShortcuts'; |
| b69ab31 | | | 25 | import {Internal} from './Internal'; |
| b69ab31 | | | 26 | import {TopBar} from './TopBar'; |
| b69ab31 | | | 27 | import {TopLevelAlerts} from './TopLevelAlert'; |
| 6c9fcae | | | 28 | import {CreateGroveRepoBanner} from './codeReview/grove/CreateGroveRepoBanner'; |
| b69ab31 | | | 29 | import {TopLevelErrors} from './TopLevelErrors'; |
| b69ab31 | | | 30 | import {tracker} from './analytics'; |
| b69ab31 | | | 31 | import {islDrawerState} from './drawerState'; |
| b69ab31 | | | 32 | import {t, T} from './i18n'; |
| b69ab31 | | | 33 | import platform from './platform'; |
| b69ab31 | | | 34 | import {useMainContentWidth} from './responsive'; |
| b69ab31 | | | 35 | import {repositoryInfoOrError} from './serverAPIState'; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | import clientToServerAPI from './ClientToServerAPI'; |
| b69ab31 | | | 38 | import './index.css'; |
| b69ab31 | | | 39 | import './stackEdit/ui/AISplitMessageHandlers'; |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | declare global { |
| b69ab31 | | | 42 | interface Window { |
| b69ab31 | | | 43 | /** |
| b69ab31 | | | 44 | * AppMode that determines what feature the App is rendering. |
| b69ab31 | | | 45 | * This is set at creation time (e.g. in HTML), and is not dynamic. |
| b69ab31 | | | 46 | */ |
| b69ab31 | | | 47 | islAppMode?: AppMode; |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | } |
| b69ab31 | | | 50 | let hasInitialized = false; |
| b69ab31 | | | 51 | export default function App() { |
| b69ab31 | | | 52 | const mode = window.islAppMode ?? {mode: 'isl'}; |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | useEffect(() => { |
| b69ab31 | | | 55 | if (!hasInitialized) { |
| b69ab31 | | | 56 | clientToServerAPI.postMessage({type: 'clientReady'}); |
| b69ab31 | | | 57 | hasInitialized = true; |
| b69ab31 | | | 58 | } |
| b69ab31 | | | 59 | }, []); |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | return ( |
| b69ab31 | | | 62 | <AllProviders> |
| b69ab31 | | | 63 | {mode.mode === 'isl' ? ( |
| b69ab31 | | | 64 | <> |
| b69ab31 | | | 65 | <NullStateOrDrawers /> |
| b69ab31 | | | 66 | <ComparisonViewModal /> |
| b69ab31 | | | 67 | </> |
| b69ab31 | | | 68 | ) : ( |
| b69ab31 | | | 69 | <ComparisonApp /> |
| b69ab31 | | | 70 | )} |
| b69ab31 | | | 71 | </AllProviders> |
| b69ab31 | | | 72 | ); |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | function ComparisonApp() { |
| b69ab31 | | | 76 | return ( |
| b69ab31 | | | 77 | <Suspense fallback={<Icon icon="loading" />}> |
| b69ab31 | | | 78 | <ComparisonViewApp /> |
| b69ab31 | | | 79 | </Suspense> |
| b69ab31 | | | 80 | ); |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | function NullStateOrDrawers() { |
| b69ab31 | | | 84 | const repoInfo = useAtomValue(repositoryInfoOrError); |
| b69ab31 | | | 85 | if (repoInfo != null && repoInfo.type !== 'success') { |
| b69ab31 | | | 86 | return <ISLNullState repoError={repoInfo} />; |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | return <ISLDrawers />; |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | function ISLDrawers() { |
| b69ab31 | | | 92 | const setDrawerState = useSetAtom(islDrawerState); |
| b69ab31 | | | 93 | useCommand('ToggleSidebar', () => { |
| b69ab31 | | | 94 | setDrawerState(state => ({ |
| b69ab31 | | | 95 | ...state, |
| b69ab31 | | | 96 | right: {...state.right, collapsed: !state.right.collapsed}, |
| b69ab31 | | | 97 | })); |
| b69ab31 | | | 98 | }); |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | return ( |
| b69ab31 | | | 101 | <Drawers |
| b69ab31 | | | 102 | rightLabel={ |
| b69ab31 | | | 103 | <> |
| b69ab31 | | | 104 | <Icon icon="edit" /> |
| b69ab31 | | | 105 | <T>Commit Info</T> |
| b69ab31 | | | 106 | </> |
| b69ab31 | | | 107 | } |
| b69ab31 | | | 108 | right={<CommitInfoSidebar />} |
| b69ab31 | | | 109 | errorBoundary={ErrorBoundary}> |
| b69ab31 | | | 110 | <MainContent /> |
| b69ab31 | | | 111 | <CommandHistoryAndProgress /> |
| b69ab31 | | | 112 | </Drawers> |
| b69ab31 | | | 113 | ); |
| b69ab31 | | | 114 | } |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | function MainContent() { |
| b69ab31 | | | 117 | const ref = useMainContentWidth(); |
| b69ab31 | | | 118 | return ( |
| b69ab31 | | | 119 | <div className="main-content-area" ref={ref}> |
| b69ab31 | | | 120 | <TopBar /> |
| b69ab31 | | | 121 | <TopLevelErrors /> |
| b69ab31 | | | 122 | <TopLevelAlerts /> |
| 6c9fcae | | | 123 | <CreateGroveRepoBanner /> |
| b69ab31 | | | 124 | <CommitTreeList /> |
| b69ab31 | | | 125 | </div> |
| b69ab31 | | | 126 | ); |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | function ISLNullState({repoError}: {repoError: RepositoryError}) { |
| b69ab31 | | | 130 | const emptyCwds = useAtomValue(useMemo(() => atom(get => get(availableCwds).length === 0), [])); |
| b69ab31 | | | 131 | useThrottledEffect( |
| b69ab31 | | | 132 | () => { |
| b69ab31 | | | 133 | if (repoError != null) { |
| b69ab31 | | | 134 | switch (repoError.type) { |
| b69ab31 | | | 135 | case 'cwdNotARepository': |
| b69ab31 | | | 136 | tracker.track('UIEmptyState', {extras: {cwd: repoError.cwd}, errorName: 'InvalidCwd'}); |
| b69ab31 | | | 137 | break; |
| b69ab31 | | | 138 | case 'edenFsUnhealthy': |
| b69ab31 | | | 139 | tracker.track('UIEmptyState', { |
| b69ab31 | | | 140 | extras: {cwd: repoError.cwd}, |
| b69ab31 | | | 141 | errorName: 'EdenFsUnhealthy', |
| b69ab31 | | | 142 | }); |
| b69ab31 | | | 143 | break; |
| b69ab31 | | | 144 | case 'invalidCommand': |
| b69ab31 | | | 145 | tracker.track('UIEmptyState', { |
| b69ab31 | | | 146 | extras: {command: repoError.command}, |
| b69ab31 | | | 147 | errorName: 'InvalidCommand', |
| b69ab31 | | | 148 | }); |
| b69ab31 | | | 149 | break; |
| b69ab31 | | | 150 | case 'unknownError': |
| b69ab31 | | | 151 | tracker.error('UIEmptyState', 'RepositoryError', repoError.error); |
| b69ab31 | | | 152 | break; |
| b69ab31 | | | 153 | } |
| b69ab31 | | | 154 | } |
| b69ab31 | | | 155 | }, |
| b69ab31 | | | 156 | 1_000, |
| b69ab31 | | | 157 | [repoError], |
| b69ab31 | | | 158 | ); |
| b69ab31 | | | 159 | let content; |
| b69ab31 | | | 160 | if (repoError != null) { |
| b69ab31 | | | 161 | if (repoError.type === 'cwdNotARepository') { |
| b69ab31 | | | 162 | if (platform.platformName === 'vscode' && emptyCwds) { |
| b69ab31 | | | 163 | content = ( |
| b69ab31 | | | 164 | <> |
| b69ab31 | | | 165 | <EmptyState> |
| b69ab31 | | | 166 | <div> |
| b69ab31 | | | 167 | <T>No folder opened</T> |
| b69ab31 | | | 168 | </div> |
| b69ab31 | | | 169 | <p> |
| b69ab31 | | | 170 | <T>Open a folder to get started.</T> |
| b69ab31 | | | 171 | </p> |
| b69ab31 | | | 172 | </EmptyState> |
| b69ab31 | | | 173 | </> |
| b69ab31 | | | 174 | ); |
| b69ab31 | | | 175 | } else { |
| b69ab31 | | | 176 | content = ( |
| b69ab31 | | | 177 | <> |
| b69ab31 | | | 178 | <EmptyState> |
| b69ab31 | | | 179 | <div> |
| b69ab31 | | | 180 | <T>Not a valid repository</T> |
| b69ab31 | | | 181 | </div> |
| b69ab31 | | | 182 | <p> |
| b69ab31 | | | 183 | <T replace={{$cwd: <code>{repoError.cwd}</code>}}> |
| b69ab31 | | | 184 | $cwd is not a valid Sapling repository. Clone or init a repository to use ISL. |
| b69ab31 | | | 185 | </T> |
| b69ab31 | | | 186 | </p> |
| b69ab31 | | | 187 | </EmptyState> |
| b69ab31 | | | 188 | <CwdSelections dismiss={() => null} /> |
| b69ab31 | | | 189 | </> |
| b69ab31 | | | 190 | ); |
| b69ab31 | | | 191 | } |
| b69ab31 | | | 192 | } else if (repoError.type === 'cwdDoesNotExist') { |
| b69ab31 | | | 193 | content = ( |
| b69ab31 | | | 194 | <> |
| b69ab31 | | | 195 | {Internal.InternalInstallationDocs ? ( |
| b69ab31 | | | 196 | <Internal.InternalInstallationDocs repoError={repoError} /> |
| b69ab31 | | | 197 | ) : ( |
| b69ab31 | | | 198 | <ErrorNotice |
| b69ab31 | | | 199 | title={ |
| b69ab31 | | | 200 | <T replace={{$cwd: repoError.cwd}}> |
| b69ab31 | | | 201 | cwd $cwd does not exist. Make sure the folder exists. |
| b69ab31 | | | 202 | </T> |
| b69ab31 | | | 203 | } |
| b69ab31 | | | 204 | error={ |
| b69ab31 | | | 205 | new Error( |
| b69ab31 | | | 206 | t('$cwd not found', { |
| b69ab31 | | | 207 | replace: {$cwd: repoError.cwd}, |
| b69ab31 | | | 208 | }), |
| b69ab31 | | | 209 | ) |
| b69ab31 | | | 210 | } |
| b69ab31 | | | 211 | buttons={[ |
| b69ab31 | | | 212 | <Button |
| b69ab31 | | | 213 | key="help-button" |
| b69ab31 | | | 214 | onClick={e => { |
| b69ab31 | | | 215 | platform.openExternalLink( |
| b69ab31 | | | 216 | 'https://sapling-scm.com/docs/introduction/installation', |
| b69ab31 | | | 217 | ); |
| b69ab31 | | | 218 | e.preventDefault(); |
| b69ab31 | | | 219 | e.stopPropagation(); |
| b69ab31 | | | 220 | }}> |
| b69ab31 | | | 221 | <T>See installation docs</T> |
| b69ab31 | | | 222 | </Button>, |
| b69ab31 | | | 223 | ]} |
| b69ab31 | | | 224 | /> |
| b69ab31 | | | 225 | )} |
| b69ab31 | | | 226 | <CwdSelections dismiss={() => null} /> |
| b69ab31 | | | 227 | </> |
| b69ab31 | | | 228 | ); |
| b69ab31 | | | 229 | } else if (repoError.type === 'edenFsUnhealthy') { |
| b69ab31 | | | 230 | content = ( |
| b69ab31 | | | 231 | <> |
| b69ab31 | | | 232 | <ErrorNotice |
| b69ab31 | | | 233 | title={<T replace={{$cwd: repoError.cwd}}>EdenFS is not running properly in $cwd</T>} |
| b69ab31 | | | 234 | description={ |
| b69ab31 | | | 235 | <T replace={{$edenDoctor: <code>eden doctor</code>}}> |
| b69ab31 | | | 236 | Try running $edenDoctor and reloading the ISL window |
| b69ab31 | | | 237 | </T> |
| b69ab31 | | | 238 | } |
| b69ab31 | | | 239 | error={ |
| b69ab31 | | | 240 | new Error( |
| b69ab31 | | | 241 | t('README_EDEN.txt found in $cwd', { |
| b69ab31 | | | 242 | replace: {$cwd: repoError.cwd}, |
| b69ab31 | | | 243 | }), |
| b69ab31 | | | 244 | ) |
| b69ab31 | | | 245 | } |
| b69ab31 | | | 246 | /> |
| b69ab31 | | | 247 | <CwdSelections dismiss={() => null} /> |
| b69ab31 | | | 248 | </> |
| b69ab31 | | | 249 | ); |
| b69ab31 | | | 250 | } else if (repoError.type === 'invalidCommand') { |
| b69ab31 | | | 251 | if (Internal.InvalidSlCommand) { |
| b69ab31 | | | 252 | content = <Internal.InvalidSlCommand repoError={repoError} />; |
| b69ab31 | | | 253 | } else { |
| b69ab31 | | | 254 | content = ( |
| b69ab31 | | | 255 | <ErrorNotice |
| b69ab31 | | | 256 | startExpanded |
| b69ab31 | | | 257 | title={<T>Invalid Sapling command. Is Sapling installed correctly?</T>} |
| b69ab31 | | | 258 | description={ |
| b69ab31 | | | 259 | <T replace={{$cmd: repoError.command}}>Command "$cmd" was not found in PATH</T> |
| b69ab31 | | | 260 | } |
| b69ab31 | | | 261 | details={<T replace={{$path: repoError.path ?? '(no path found)'}}>PATH: $path'</T>} |
| b69ab31 | | | 262 | buttons={[ |
| b69ab31 | | | 263 | <Button |
| b69ab31 | | | 264 | key="help-button" |
| b69ab31 | | | 265 | onClick={e => { |
| b69ab31 | | | 266 | platform.openExternalLink( |
| b69ab31 | | | 267 | 'https://sapling-scm.com/docs/introduction/installation', |
| b69ab31 | | | 268 | ); |
| b69ab31 | | | 269 | e.preventDefault(); |
| b69ab31 | | | 270 | e.stopPropagation(); |
| b69ab31 | | | 271 | }}> |
| b69ab31 | | | 272 | <T>See installation docs</T> |
| b69ab31 | | | 273 | </Button>, |
| b69ab31 | | | 274 | ]} |
| b69ab31 | | | 275 | /> |
| b69ab31 | | | 276 | ); |
| b69ab31 | | | 277 | } |
| b69ab31 | | | 278 | } else { |
| b69ab31 | | | 279 | content = <ErrorNotice title={<T>Something went wrong</T>} error={repoError.error} />; |
| b69ab31 | | | 280 | } |
| b69ab31 | | | 281 | } |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | return <div className="empty-app-state">{content}</div>; |
| b69ab31 | | | 284 | } |