mirror of
https://github.com/harness/drone.git
synced 2025-05-12 15:10:09 +08:00
33 lines
848 B
TypeScript
33 lines
848 B
TypeScript
import React, { useState, useContext } from 'react'
|
|
import { noop } from 'lodash-es'
|
|
import type { AppProps } from 'AppProps'
|
|
|
|
interface AppContextProps extends AppProps {
|
|
setAppContext: (value: Partial<AppProps>) => void
|
|
}
|
|
|
|
const AppContext = React.createContext<AppContextProps>({
|
|
standalone: true,
|
|
setAppContext: noop,
|
|
hooks: {},
|
|
components: {}
|
|
})
|
|
|
|
export const AppContextProvider: React.FC<{ value: AppProps }> = ({ value: initialValue, children }) => {
|
|
const [appStates, setAppStates] = useState<AppProps>(initialValue)
|
|
|
|
return (
|
|
<AppContext.Provider
|
|
value={{
|
|
...appStates,
|
|
setAppContext: props => {
|
|
setAppStates({ ...appStates, ...props })
|
|
}
|
|
}}>
|
|
{children}
|
|
</AppContext.Provider>
|
|
)
|
|
}
|
|
|
|
export const useAppContext: () => AppContextProps = () => useContext(AppContext)
|