mirror of
https://github.com/overleaf/overleaf.git
synced 2025-01-26 16:24:11 +00:00
Add types to IdeContext (#8107)
GitOrigin-RevId: 6b1df81ce588558adab0c31c0f65e68e5b0c25bf
This commit is contained in:
parent
309742c37b
commit
4ca3f9cabf
2 changed files with 25 additions and 33 deletions
|
@ -1,33 +0,0 @@
|
|||
import { createContext, useContext, useState } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { getMockIde } from './mock/mock-ide'
|
||||
|
||||
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 }) {
|
||||
const [value] = useState(() => ide || getMockIde())
|
||||
|
||||
return <IdeContext.Provider value={value}>{children}</IdeContext.Provider>
|
||||
}
|
||||
IdeProvider.propTypes = {
|
||||
children: PropTypes.any.isRequired,
|
||||
ide: PropTypes.shape({
|
||||
$scope: PropTypes.object.isRequired,
|
||||
}),
|
||||
}
|
25
services/web/frontend/js/shared/context/ide-context.tsx
Normal file
25
services/web/frontend/js/shared/context/ide-context.tsx
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { createContext, FC, useContext, useState } from 'react'
|
||||
import { getMockIde } from './mock/mock-ide'
|
||||
|
||||
type Ide = {
|
||||
[key: string]: any // TODO: define the rest of the `ide` and `$scope` properties
|
||||
$scope: Record<string, any>
|
||||
}
|
||||
|
||||
const IdeContext = createContext<Ide | null>(null)
|
||||
|
||||
export const IdeProvider: FC<{ ide: Ide }> = ({ ide, children }) => {
|
||||
const [value] = useState(() => ide || getMockIde())
|
||||
|
||||
return <IdeContext.Provider value={value}>{children}</IdeContext.Provider>
|
||||
}
|
||||
|
||||
export function useIdeContext(): Ide {
|
||||
const context = useContext(IdeContext)
|
||||
|
||||
if (!context) {
|
||||
throw new Error('useIdeContext is only available inside IdeProvider')
|
||||
}
|
||||
|
||||
return context
|
||||
}
|
Loading…
Reference in a new issue