2023-10-26 04:57:00 -04:00
|
|
|
import React, {
|
2023-10-24 10:57:17 -04:00
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useState,
|
|
|
|
FC,
|
|
|
|
useMemo,
|
|
|
|
useEffect,
|
2023-10-26 04:57:00 -04:00
|
|
|
useCallback,
|
2023-10-24 10:57:17 -04:00
|
|
|
} from 'react'
|
|
|
|
import { ReactScopeValueStore } from '@/features/ide-react/scope-value-store/react-scope-value-store'
|
2023-10-26 04:57:00 -04:00
|
|
|
import populateLayoutScope from '@/features/ide-react/scope-adapters/layout-context-adapter'
|
2023-11-02 07:18:12 -04:00
|
|
|
import populateReviewPanelScope from '@/features/ide-react/scope-adapters/review-panel-context-adapter'
|
2023-10-24 10:57:17 -04:00
|
|
|
import { IdeProvider } from '@/shared/context/ide-context'
|
|
|
|
import {
|
|
|
|
createIdeEventEmitter,
|
|
|
|
IdeEventEmitter,
|
|
|
|
} from '@/features/ide-react/create-ide-event-emitter'
|
|
|
|
import { JoinProjectPayload } from '@/features/ide-react/connection/join-project-payload'
|
|
|
|
import { useConnectionContext } from '@/features/ide-react/context/connection-context'
|
|
|
|
import { getMockIde } from '@/shared/context/mock/mock-ide'
|
2023-10-26 04:57:00 -04:00
|
|
|
import { populateEditorScope } from '@/features/ide-react/context/editor-manager-context'
|
|
|
|
import { postJSON } from '@/infrastructure/fetch-json'
|
|
|
|
import { EventLog } from '@/features/ide-react/editor/event-log'
|
|
|
|
import { populateSettingsScope } from '@/features/ide-react/scope-adapters/settings-adapter'
|
|
|
|
import { populateOnlineUsersScope } from '@/features/ide-react/context/online-users-context'
|
|
|
|
import { populateReferenceScope } from '@/features/ide-react/context/references-context'
|
2023-11-02 07:18:12 -04:00
|
|
|
import { ReactScopeEventEmitter } from '@/features/ide-react/scope-event-emitter/react-scope-event-emitter'
|
2023-11-02 07:36:04 -04:00
|
|
|
import getMeta from '@/utils/meta'
|
2023-10-24 10:57:17 -04:00
|
|
|
|
|
|
|
type IdeReactContextValue = {
|
|
|
|
projectId: string
|
|
|
|
eventEmitter: IdeEventEmitter
|
2023-10-26 04:57:00 -04:00
|
|
|
eventLog: EventLog
|
|
|
|
startedFreeTrial: boolean
|
|
|
|
setStartedFreeTrial: React.Dispatch<
|
|
|
|
React.SetStateAction<IdeReactContextValue['startedFreeTrial']>
|
|
|
|
>
|
|
|
|
reportError: (error: any, meta?: Record<string, any>) => void
|
2023-10-24 10:57:17 -04:00
|
|
|
}
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
const IdeReactContext = createContext<IdeReactContextValue | undefined>(
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
function populateIdeReactScope(store: ReactScopeValueStore) {
|
|
|
|
store.set('sync_tex_error', false)
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateProjectScope(store: ReactScopeValueStore) {
|
|
|
|
store.allowNonExistentPath('project', true)
|
|
|
|
store.set('permissionsLevel', 'readOnly')
|
|
|
|
}
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
function populatePdfScope(store: ReactScopeValueStore) {
|
|
|
|
store.allowNonExistentPath('pdf', true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateFileTreeScope(store: ReactScopeValueStore) {
|
|
|
|
store.set('docs', [])
|
|
|
|
}
|
|
|
|
|
2023-11-02 07:36:04 -04:00
|
|
|
function createReactScopeValueStore(projectId: string) {
|
2023-10-24 10:57:17 -04:00
|
|
|
const scopeStore = new ReactScopeValueStore()
|
|
|
|
|
|
|
|
// Populate the scope value store with default values that will be used by
|
|
|
|
// nested contexts that refer to scope values. The ideal would be to leave
|
|
|
|
// initialization of store values up to the nested context, which would keep
|
|
|
|
// initialization code together with the context and would only populate
|
|
|
|
// necessary values in the store, but this is simpler for now
|
|
|
|
populateIdeReactScope(scopeStore)
|
2023-11-02 07:36:04 -04:00
|
|
|
populateEditorScope(scopeStore, projectId)
|
2023-10-26 04:57:00 -04:00
|
|
|
populateLayoutScope(scopeStore)
|
2023-10-24 10:57:17 -04:00
|
|
|
populateProjectScope(scopeStore)
|
2023-10-26 04:57:00 -04:00
|
|
|
populatePdfScope(scopeStore)
|
|
|
|
populateSettingsScope(scopeStore)
|
|
|
|
populateOnlineUsersScope(scopeStore)
|
|
|
|
populateReferenceScope(scopeStore)
|
|
|
|
populateFileTreeScope(scopeStore)
|
2023-11-02 07:18:12 -04:00
|
|
|
populateReviewPanelScope(scopeStore)
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
scopeStore.allowNonExistentPath('hasLintingError')
|
|
|
|
scopeStore.allowNonExistentPath('loadingThreads')
|
2023-10-24 10:57:17 -04:00
|
|
|
|
|
|
|
return scopeStore
|
|
|
|
}
|
|
|
|
|
|
|
|
const projectId = window.project_id
|
|
|
|
|
|
|
|
export const IdeReactProvider: FC = ({ children }) => {
|
2023-11-02 07:36:04 -04:00
|
|
|
const [scopeStore] = useState(() => createReactScopeValueStore(projectId))
|
2023-10-24 10:57:17 -04:00
|
|
|
const [eventEmitter] = useState(createIdeEventEmitter)
|
|
|
|
const [scopeEventEmitter] = useState(
|
|
|
|
() => new ReactScopeEventEmitter(eventEmitter)
|
|
|
|
)
|
2023-10-26 04:57:00 -04:00
|
|
|
const [eventLog] = useState(() => new EventLog())
|
|
|
|
const [startedFreeTrial, setStartedFreeTrial] = useState(false)
|
2023-10-24 10:57:17 -04:00
|
|
|
|
|
|
|
const { socket } = useConnectionContext()
|
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
const reportError = useCallback(
|
|
|
|
(error: any, meta?: Record<string, any>) => {
|
|
|
|
const metadata = {
|
|
|
|
...meta,
|
2023-11-02 07:36:04 -04:00
|
|
|
user_id: getMeta('ol-user_id'),
|
2023-10-26 04:57:00 -04:00
|
|
|
project_id: projectId,
|
|
|
|
// @ts-ignore
|
|
|
|
client_id: socket.socket.sessionid,
|
|
|
|
// @ts-ignore
|
|
|
|
transport: socket.socket.transport,
|
|
|
|
client_now: new Date(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const errorObj: Record<string, any> = {}
|
|
|
|
if (typeof error === 'object') {
|
|
|
|
for (const key of Object.getOwnPropertyNames(error)) {
|
|
|
|
errorObj[key] = error[key]
|
|
|
|
}
|
|
|
|
} else if (typeof error === 'string') {
|
|
|
|
errorObj.message = error
|
|
|
|
}
|
|
|
|
return postJSON('/error/client', {
|
|
|
|
body: {
|
|
|
|
error: errorObj,
|
|
|
|
meta: metadata,
|
|
|
|
_csrf: window.csrfToken,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[socket.socket]
|
|
|
|
)
|
2023-10-24 10:57:17 -04:00
|
|
|
|
|
|
|
// Populate scope values when joining project, then fire project:joined event
|
|
|
|
useEffect(() => {
|
|
|
|
function handleJoinProjectResponse({
|
|
|
|
project,
|
|
|
|
permissionsLevel,
|
|
|
|
}: JoinProjectPayload) {
|
|
|
|
scopeStore.set('project', { rootDoc_id: null, ...project })
|
|
|
|
scopeStore.set('permissionsLevel', permissionsLevel)
|
|
|
|
// Make watchers update immediately
|
|
|
|
scopeStore.flushUpdates()
|
|
|
|
eventEmitter.emit('project:joined', { project, permissionsLevel })
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('joinProjectResponse', handleJoinProjectResponse)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
socket.removeListener('joinProjectResponse', handleJoinProjectResponse)
|
|
|
|
}
|
|
|
|
}, [socket, eventEmitter, scopeStore])
|
|
|
|
|
|
|
|
const ide = useMemo(() => {
|
|
|
|
return {
|
|
|
|
...getMockIde(),
|
|
|
|
socket,
|
2023-10-26 04:57:00 -04:00
|
|
|
reportError,
|
|
|
|
// TODO: MIGRATION: Remove this once it's no longer used
|
|
|
|
fileTreeManager: {
|
|
|
|
findEntityByPath: () => null,
|
|
|
|
selectEntity: () => {},
|
|
|
|
getPreviewByPath: () => null,
|
|
|
|
getRootDocDirname: () => '',
|
|
|
|
},
|
2023-10-24 10:57:17 -04:00
|
|
|
}
|
2023-10-26 04:57:00 -04:00
|
|
|
}, [socket, reportError])
|
2023-10-24 10:57:17 -04:00
|
|
|
|
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
|
|
|
eventEmitter,
|
2023-10-26 04:57:00 -04:00
|
|
|
eventLog,
|
|
|
|
startedFreeTrial,
|
|
|
|
setStartedFreeTrial,
|
2023-10-24 10:57:17 -04:00
|
|
|
projectId,
|
2023-10-26 04:57:00 -04:00
|
|
|
reportError,
|
2023-10-24 10:57:17 -04:00
|
|
|
}),
|
2023-10-26 04:57:00 -04:00
|
|
|
[eventEmitter, eventLog, reportError, startedFreeTrial]
|
2023-10-24 10:57:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<IdeReactContext.Provider value={value}>
|
|
|
|
<IdeProvider
|
|
|
|
ide={ide}
|
|
|
|
scopeStore={scopeStore}
|
|
|
|
scopeEventEmitter={scopeEventEmitter}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</IdeProvider>
|
|
|
|
</IdeReactContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useIdeReactContext(): IdeReactContextValue {
|
|
|
|
const context = useContext(IdeReactContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useIdeReactContext is only available inside IdeReactProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|