|
| 1 | +import React from 'react' |
| 2 | +import { Substores, useEditorState } from './store/store-hook' |
| 3 | +import { getImportOperationTextAsJsx } from './import-wizard/import-wizard-helpers' |
| 4 | +import { getTotalImportStatusAndResult } from '../../core/shared/import/import-operation-service' |
| 5 | +import type { TotalImportResult } from '../../core/shared/import/import-operation-types' |
| 6 | +import type { Theme } from '../../uuiui' |
| 7 | +import { useColorTheme } from '../../uuiui' |
| 8 | +import { getCurrentTheme } from './store/editor-state' |
| 9 | +import ReactDOM from 'react-dom' |
| 10 | + |
| 11 | +export function LoadingEditorComponent() { |
| 12 | + const colorTheme = useColorTheme() |
| 13 | + |
| 14 | + const currentTheme: Theme = useEditorState( |
| 15 | + Substores.theme, |
| 16 | + (store) => getCurrentTheme(store.userState), |
| 17 | + 'currentTheme', |
| 18 | + ) |
| 19 | + |
| 20 | + const importState = useEditorState( |
| 21 | + Substores.restOfEditor, |
| 22 | + (store) => store.editor.importState, |
| 23 | + 'LoadingEditorComponent importState', |
| 24 | + ) |
| 25 | + |
| 26 | + const githubRepo = useEditorState( |
| 27 | + Substores.userState, |
| 28 | + (store) => store.userState.githubState.gitRepoToLoad, |
| 29 | + 'LoadingEditorComponent githubRepoToLoad', |
| 30 | + ) |
| 31 | + |
| 32 | + const totalImportResult: TotalImportResult = React.useMemo( |
| 33 | + () => getTotalImportStatusAndResult(importState), |
| 34 | + [importState], |
| 35 | + ) |
| 36 | + |
| 37 | + const projectId = useEditorState( |
| 38 | + Substores.restOfEditor, |
| 39 | + (store) => store.editor.id, |
| 40 | + 'LoadingEditorComponent projectId', |
| 41 | + ) |
| 42 | + |
| 43 | + const cleared = React.useRef(false) |
| 44 | + |
| 45 | + const currentOperationToShow: { |
| 46 | + text: React.ReactNode |
| 47 | + id: string |
| 48 | + timeDone: number | null | undefined |
| 49 | + timeStarted: number | null | undefined |
| 50 | + } | null = React.useMemo(() => { |
| 51 | + if (totalImportResult.importStatus.status == 'not-started') { |
| 52 | + if (projectId == null) { |
| 53 | + return { |
| 54 | + text: 'Loading Editor...', |
| 55 | + id: 'loading-editor', |
| 56 | + timeDone: null, |
| 57 | + timeStarted: null, |
| 58 | + } |
| 59 | + } else { |
| 60 | + return { |
| 61 | + text: `Parsing files`, |
| 62 | + id: 'parseFiles', |
| 63 | + timeDone: null, |
| 64 | + timeStarted: null, |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + for (const op of importState.importOperations) { |
| 69 | + if (op?.children?.length == 0 || op.type == 'refreshDependencies') { |
| 70 | + if (op.timeStarted != null && op.timeDone == null) { |
| 71 | + return { |
| 72 | + text: getImportOperationTextAsJsx(op), |
| 73 | + id: op.id ?? op.type, |
| 74 | + timeDone: op.timeDone, |
| 75 | + timeStarted: op.timeStarted, |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + if (op.type !== 'refreshDependencies') { |
| 80 | + for (const child of op.children ?? []) { |
| 81 | + if (child.timeStarted != null && child.timeDone == null) { |
| 82 | + return { |
| 83 | + text: getImportOperationTextAsJsx(child), |
| 84 | + id: child.id ?? child.type, |
| 85 | + timeDone: child.timeDone, |
| 86 | + timeStarted: child.timeStarted, |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return { |
| 93 | + text: 'Loading Editor...', |
| 94 | + id: 'loading-editor', |
| 95 | + timeDone: null, |
| 96 | + timeStarted: null, |
| 97 | + } |
| 98 | + }, [totalImportResult, importState.importOperations, projectId]) |
| 99 | + |
| 100 | + const shouldBeCleared = React.useMemo(() => { |
| 101 | + return ( |
| 102 | + cleared.current || |
| 103 | + (totalImportResult.importStatus.status == 'done' && |
| 104 | + (githubRepo == null || totalImportResult.result == 'criticalError')) || |
| 105 | + totalImportResult.importStatus.status == 'paused' |
| 106 | + ) |
| 107 | + }, [totalImportResult, githubRepo]) |
| 108 | + |
| 109 | + React.useEffect(() => { |
| 110 | + if (shouldBeCleared) { |
| 111 | + const loadingScreenWrapper = document.getElementById('loading-screen-wrapper') |
| 112 | + if (loadingScreenWrapper != null) { |
| 113 | + loadingScreenWrapper.remove() |
| 114 | + } |
| 115 | + } |
| 116 | + }, [shouldBeCleared]) |
| 117 | + |
| 118 | + const portal = React.useRef(document.getElementById('loading-screen-progress-bar-portal')).current |
| 119 | + const hasMounted = React.useRef(false) |
| 120 | + if (portal == null) { |
| 121 | + return null |
| 122 | + } |
| 123 | + |
| 124 | + if (shouldBeCleared) { |
| 125 | + cleared.current = true |
| 126 | + return null |
| 127 | + } |
| 128 | + |
| 129 | + if (!hasMounted.current) { |
| 130 | + portal.innerHTML = '' |
| 131 | + hasMounted.current = true |
| 132 | + } |
| 133 | + |
| 134 | + const themeStyle = |
| 135 | + currentTheme === 'dark' |
| 136 | + ? ` |
| 137 | + .editor-loading-screen { background-color: ${colorTheme.bg6.value} } |
| 138 | + .utopia-logo-pyramid.light { display: none; } |
| 139 | + .utopia-logo-pyramid.dark { display: block; } |
| 140 | + ` |
| 141 | + : '' |
| 142 | + |
| 143 | + return ReactDOM.createPortal( |
| 144 | + <React.Fragment> |
| 145 | + <style>{themeStyle}</style> |
| 146 | + <div className='progress-bar-shell' style={{ borderColor: colorTheme.fg0.value }}> |
| 147 | + <div |
| 148 | + className='progress-bar-progress animation-progress' |
| 149 | + style={{ |
| 150 | + transform: 'translateX(-180px)', |
| 151 | + animationName: 'animation-keyframes-2', |
| 152 | + backgroundColor: colorTheme.fg0.value, |
| 153 | + }} |
| 154 | + ></div> |
| 155 | + </div> |
| 156 | + <div> |
| 157 | + <ul className='loading-screen-import-operations'> |
| 158 | + {currentOperationToShow != null ? ( |
| 159 | + <li |
| 160 | + style={{ |
| 161 | + listStyle: 'none', |
| 162 | + color: colorTheme.fg0.value, |
| 163 | + }} |
| 164 | + key={currentOperationToShow.id} |
| 165 | + > |
| 166 | + {currentOperationToShow.text} |
| 167 | + </li> |
| 168 | + ) : null} |
| 169 | + </ul> |
| 170 | + </div> |
| 171 | + </React.Fragment>, |
| 172 | + portal, |
| 173 | + ) |
| 174 | +} |
0 commit comments