2023-10-24 10:57:17 -04:00
|
|
|
import { createContext, FC, useContext, useMemo } from 'react'
|
|
|
|
import { ScopeValueStore } from '../../../../types/ide/scope-value-store'
|
|
|
|
import { Scope } from '../../../../types/angular/scope'
|
|
|
|
import getMeta from '@/utils/meta'
|
|
|
|
import { ScopeEventEmitter } from '../../../../types/ide/scope-event-emitter'
|
2022-05-25 03:58:26 -04:00
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
export type Ide = {
|
2022-05-25 03:58:26 -04:00
|
|
|
[key: string]: any // TODO: define the rest of the `ide` and `$scope` properties
|
2023-10-24 10:57:17 -04:00
|
|
|
$scope: Scope
|
2022-05-25 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
type IdeContextValue = Ide & {
|
|
|
|
isReactIde: boolean
|
|
|
|
scopeStore: ScopeValueStore
|
|
|
|
scopeEventEmitter: ScopeEventEmitter
|
|
|
|
}
|
|
|
|
|
|
|
|
const IdeContext = createContext<IdeContextValue | undefined>(undefined)
|
|
|
|
const isReactIde: boolean = getMeta('ol-idePageReact')
|
2022-05-25 03:58:26 -04:00
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
export const IdeProvider: FC<{
|
|
|
|
ide: Ide
|
|
|
|
scopeStore: ScopeValueStore
|
|
|
|
scopeEventEmitter: ScopeEventEmitter
|
|
|
|
}> = ({ ide, scopeStore, scopeEventEmitter, children }) => {
|
|
|
|
const value = useMemo<IdeContextValue>(() => {
|
|
|
|
return {
|
|
|
|
...ide,
|
|
|
|
isReactIde,
|
|
|
|
scopeStore,
|
|
|
|
scopeEventEmitter,
|
|
|
|
}
|
|
|
|
}, [ide, scopeStore, scopeEventEmitter])
|
2022-05-25 03:58:26 -04:00
|
|
|
|
|
|
|
return <IdeContext.Provider value={value}>{children}</IdeContext.Provider>
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:57:17 -04:00
|
|
|
export function useIdeContext(): IdeContextValue {
|
2022-05-25 03:58:26 -04:00
|
|
|
const context = useContext(IdeContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useIdeContext is only available inside IdeProvider')
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|