2023-10-24 06:45:01 -04:00
|
|
|
import { createContext, FC, useContext, useState } from 'react'
|
|
|
|
import { getMockIde } from './mock/mock-ide'
|
2022-05-25 03:58:26 -04:00
|
|
|
|
2023-10-24 06:45:01 -04:00
|
|
|
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 06:45:01 -04:00
|
|
|
$scope: Record<string, any>
|
2022-05-25 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 06:45:01 -04:00
|
|
|
const IdeContext = createContext<Ide | null>(null)
|
2022-05-25 03:58:26 -04:00
|
|
|
|
2023-10-24 06:45:01 -04:00
|
|
|
export const IdeProvider: FC<{ ide: Ide }> = ({ ide, children }) => {
|
|
|
|
const [value] = useState(() => ide || getMockIde())
|
2022-05-25 03:58:26 -04:00
|
|
|
|
|
|
|
return <IdeContext.Provider value={value}>{children}</IdeContext.Provider>
|
|
|
|
}
|
|
|
|
|
2023-10-24 06:45:01 -04:00
|
|
|
export function useIdeContext(): Ide {
|
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
|
|
|
|
}
|