2022-03-31 11:22:36 +00:00
|
|
|
import { createContext, useContext, useState } from 'react'
|
2021-06-16 09:32:38 +00:00
|
|
|
import PropTypes from 'prop-types'
|
2022-03-31 11:22:36 +00:00
|
|
|
import { getMockIde } from './mock/mock-ide'
|
2021-06-16 09:32:38 +00:00
|
|
|
|
|
|
|
const IdeContext = createContext()
|
|
|
|
|
|
|
|
IdeContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
$scope: PropTypes.object.isRequired,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useIdeContext() {
|
|
|
|
const context = useContext(IdeContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useIdeContext is only available inside IdeProvider')
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
|
|
|
export function IdeProvider({ ide, children }) {
|
2022-03-31 11:22:36 +00:00
|
|
|
const [value] = useState(() => ide || getMockIde())
|
|
|
|
|
|
|
|
return <IdeContext.Provider value={value}>{children}</IdeContext.Provider>
|
2021-06-16 09:32:38 +00:00
|
|
|
}
|
|
|
|
IdeProvider.propTypes = {
|
|
|
|
children: PropTypes.any.isRequired,
|
|
|
|
ide: PropTypes.shape({
|
|
|
|
$scope: PropTypes.object.isRequired,
|
2022-03-31 11:22:36 +00:00
|
|
|
}),
|
2021-06-16 09:32:38 +00:00
|
|
|
}
|