|
|
@@ -49,6 +49,8 @@ import { UserId } from '../../../types/user'
|
|
|
import { ProjectCompiler } from '../../../types/project-settings'
|
|
|
import { ReferencesContext } from '@/features/ide-react/context/references-context'
|
|
|
import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
|
|
|
+import { DetachCompileContext } from '@/shared/context/detach-compile-context'
|
|
|
+import { type CompileContext } from '@/shared/context/local-compile-context'
|
|
|
|
|
|
// these constants can be imported in tests instead of
|
|
|
// using magic strings
|
|
|
@@ -91,6 +93,7 @@ export type EditorProvidersProps = {
|
|
|
layoutContext?: Partial<LayoutContextValue>
|
|
|
userSettings?: Record<string, any>
|
|
|
providers?: Record<string, React.FC<React.PropsWithChildren<any>>>
|
|
|
+ mockCompileOnLoad?: boolean
|
|
|
}
|
|
|
|
|
|
export const projectDefaults: ProjectMetadata = {
|
|
|
@@ -174,6 +177,7 @@ export function EditorProviders({
|
|
|
layoutContext = layoutContextDefault,
|
|
|
userSettings = {},
|
|
|
providers = {},
|
|
|
+ mockCompileOnLoad = false,
|
|
|
}: EditorProvidersProps) {
|
|
|
window.metaAttributesCache.set(
|
|
|
'ol-gitBridgePublicBaseUrl',
|
|
|
@@ -234,27 +238,32 @@ export function EditorProviders({
|
|
|
window.metaAttributesCache.set('ol-user', { ...user, features })
|
|
|
window.metaAttributesCache.set('ol-project_id', projectId)
|
|
|
|
|
|
+ const customProviders: Record<string, FC<PropsWithChildren>> = {
|
|
|
+ ConnectionProvider: makeConnectionProvider(socket),
|
|
|
+ IdeReactProvider: makeIdeReactProvider(scope, socket),
|
|
|
+ EditorOpenDocProvider: makeEditorOpenDocProvider({
|
|
|
+ currentDocumentId: scope.editor.currentDocumentId,
|
|
|
+ openDocName: scope.editor.openDocName,
|
|
|
+ currentDocument: scope.editor.sharejs_doc,
|
|
|
+ }),
|
|
|
+ EditorPropertiesProvider: makeEditorPropertiesProvider({
|
|
|
+ wantTrackChanges: scope.editor.wantTrackChanges,
|
|
|
+ }),
|
|
|
+ LayoutProvider: makeLayoutProvider(layoutContext),
|
|
|
+ ProjectProvider: makeProjectProvider(project),
|
|
|
+ ReferencesProvider: makeReferencesProvider(),
|
|
|
+ ...providers,
|
|
|
+ }
|
|
|
+
|
|
|
+ // Only override DetachCompileProvider when we need the mock
|
|
|
+ if (mockCompileOnLoad) {
|
|
|
+ customProviders.DetachCompileProvider =
|
|
|
+ makeDetachCompileProvider(mockCompileOnLoad)
|
|
|
+ }
|
|
|
+ // Otherwise, let ReactContextRoot use the real DetachCompileProvider from production
|
|
|
+
|
|
|
return (
|
|
|
- <ReactContextRoot
|
|
|
- providers={{
|
|
|
- ConnectionProvider: makeConnectionProvider(socket),
|
|
|
- IdeReactProvider: makeIdeReactProvider(scope, socket),
|
|
|
- EditorOpenDocProvider: makeEditorOpenDocProvider({
|
|
|
- currentDocumentId: scope.editor.currentDocumentId,
|
|
|
- openDocName: scope.editor.openDocName,
|
|
|
- currentDocument: scope.editor.sharejs_doc,
|
|
|
- }),
|
|
|
- EditorPropertiesProvider: makeEditorPropertiesProvider({
|
|
|
- wantTrackChanges: scope.editor.wantTrackChanges,
|
|
|
- }),
|
|
|
- LayoutProvider: makeLayoutProvider(layoutContext),
|
|
|
- ProjectProvider: makeProjectProvider(project),
|
|
|
- ReferencesProvider: makeReferencesProvider(),
|
|
|
- ...providers,
|
|
|
- }}
|
|
|
- >
|
|
|
- {children}
|
|
|
- </ReactContextRoot>
|
|
|
+ <ReactContextRoot providers={customProviders}>{children}</ReactContextRoot>
|
|
|
)
|
|
|
}
|
|
|
|
|
|
@@ -640,3 +649,127 @@ export function makeProjectProvider(initialProject: ProjectMetadata) {
|
|
|
|
|
|
return ProjectProvider
|
|
|
}
|
|
|
+
|
|
|
+const BASE_COMPILE_CONTEXT_MOCK = {
|
|
|
+ animateCompileDropdownArrow: false,
|
|
|
+ clearCache: () => {},
|
|
|
+ clearingCache: false,
|
|
|
+ clsiServerId: undefined,
|
|
|
+ codeCheckFailed: false,
|
|
|
+ deliveryLatencies: {},
|
|
|
+ editedSinceCompileStarted: false,
|
|
|
+ fileList: undefined,
|
|
|
+ hasChanges: false,
|
|
|
+ hasShortCompileTimeout: false,
|
|
|
+ highlights: undefined,
|
|
|
+ isProjectOwner: true,
|
|
|
+ lastCompileOptions: {},
|
|
|
+ logEntryAnnotations: undefined,
|
|
|
+ logEntries: undefined,
|
|
|
+ outputFilesArchive: undefined,
|
|
|
+ pdfViewer: 'pdfjs',
|
|
|
+ position: undefined,
|
|
|
+ rawLog: undefined,
|
|
|
+ recompileFromScratch: () => {},
|
|
|
+ setAnimateCompileDropdownArrow: () => {},
|
|
|
+ setHasLintingError: () => {},
|
|
|
+ setHighlights: () => {},
|
|
|
+ setPosition: () => {},
|
|
|
+ setShowCompileTimeWarning: () => {},
|
|
|
+ setShowLogs: () => {},
|
|
|
+ toggleLogs: () => {},
|
|
|
+ setStopOnValidationError: () => {},
|
|
|
+ showLogs: false,
|
|
|
+ showCompileTimeWarning: false,
|
|
|
+ stopCompile: () => {},
|
|
|
+ stopOnValidationError: true,
|
|
|
+ stoppedOnFirstError: false,
|
|
|
+ uncompiled: false,
|
|
|
+ validationIssues: undefined,
|
|
|
+ firstRenderDone: () => {},
|
|
|
+ setChangedAt: () => {},
|
|
|
+ cleanupCompileResult: undefined,
|
|
|
+ syncToEntry: () => {},
|
|
|
+ recordAction: () => {},
|
|
|
+ darkModePdf: false,
|
|
|
+ setDarkModePdf: () => {},
|
|
|
+ activeOverallTheme: 'light',
|
|
|
+} as const
|
|
|
+
|
|
|
+const makeDetachCompileProvider = (mockCompileOnLoad: boolean = false) => {
|
|
|
+ const DetachCompileProvider: FC<PropsWithChildren> = ({ children }) => {
|
|
|
+ const [pdfUrl, setPdfUrl] = useState<string | undefined>()
|
|
|
+ const [pdfDownloadUrl, setPdfDownloadUrl] = useState<string | undefined>()
|
|
|
+ const [pdfFile, setPdfFile] = useState<any>()
|
|
|
+ const [compiling, setCompiling] = useState(false)
|
|
|
+ const [error, setError] = useState<string | undefined>()
|
|
|
+ const [autoCompile, setAutoCompile] = useState(true)
|
|
|
+ const [draft, setDraft] = useState(false)
|
|
|
+ const [stopOnFirstError, setStopOnFirstError] = useState(false)
|
|
|
+
|
|
|
+ const startCompile = useCallback(async () => {
|
|
|
+ setCompiling(true)
|
|
|
+ try {
|
|
|
+ const response = await fetch('/project/123abc/compile', {
|
|
|
+ method: 'POST',
|
|
|
+ })
|
|
|
+ const data = await response.json()
|
|
|
+ const pdfFileData = data.outputFiles?.find(
|
|
|
+ (file: any) => file.type === 'pdf'
|
|
|
+ )
|
|
|
+ if (data.status === 'success' && pdfFileData) {
|
|
|
+ const newPdfUrl = `${data.pdfDownloadDomain || ''}${pdfFileData.url}`
|
|
|
+ const params = [
|
|
|
+ data.compileGroup && `compileGroup=${data.compileGroup}`,
|
|
|
+ data.clsiServerId && `clsiserverid=${data.clsiServerId}`,
|
|
|
+ 'popupDownload=true',
|
|
|
+ ]
|
|
|
+ .filter(Boolean)
|
|
|
+ .join('&')
|
|
|
+ const newPdfDownloadUrl = `/download/project/123abc/build/${pdfFileData.build}/output/${pdfFileData.path}?${params}`
|
|
|
+
|
|
|
+ setPdfUrl(newPdfUrl)
|
|
|
+ setPdfDownloadUrl(newPdfDownloadUrl)
|
|
|
+ setPdfFile({ pdfUrl: newPdfUrl, pdfDownloadUrl: newPdfDownloadUrl })
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ setError('Compile failed')
|
|
|
+ } finally {
|
|
|
+ setCompiling(false)
|
|
|
+ }
|
|
|
+ }, [])
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (mockCompileOnLoad) {
|
|
|
+ startCompile()
|
|
|
+ }
|
|
|
+ // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
+ }, [startCompile])
|
|
|
+
|
|
|
+ const value = {
|
|
|
+ ...BASE_COMPILE_CONTEXT_MOCK,
|
|
|
+ autoCompile,
|
|
|
+ compiling,
|
|
|
+ draft,
|
|
|
+ error,
|
|
|
+ pdfDownloadUrl,
|
|
|
+ pdfFile,
|
|
|
+ pdfUrl,
|
|
|
+ setAutoCompile,
|
|
|
+ setCompiling,
|
|
|
+ setDraft,
|
|
|
+ setError,
|
|
|
+ setStopOnFirstError,
|
|
|
+ startCompile,
|
|
|
+ stopOnFirstError,
|
|
|
+ } as CompileContext
|
|
|
+
|
|
|
+ return (
|
|
|
+ <DetachCompileContext.Provider value={value}>
|
|
|
+ {children}
|
|
|
+ </DetachCompileContext.Provider>
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return DetachCompileProvider
|
|
|
+}
|