-
Notifications
You must be signed in to change notification settings - Fork 46
/
OperationalContext.tsx
40 lines (32 loc) · 1.03 KB
/
OperationalContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as React from "react"
export type MessageType = "info" | "success" | "error"
export interface IMessage {
body: string
type: MessageType
onClick?: () => void
}
export interface Context {
pushState?: (url: string) => void
replaceState?: (url: string) => void
pushMessage: (message: IMessage) => void
loading: boolean
setLoading: (isLoading: boolean) => void
clearMessages: () => void
}
/**
* Defining a default context value here, used below when instantiating
* the context consumer and provider below in order for context to be
* correctly detected throughout the application.
*/
const defaultContext: Context = {
pushState: undefined,
replaceState: undefined,
pushMessage: (_: IMessage) => undefined,
clearMessages: () => undefined,
loading: false,
setLoading: (_: boolean) => undefined,
}
const ctx = React.createContext(defaultContext)
export const { Consumer: OperationalContext, Provider } = ctx
export const useOperationalContext = () => React.useContext(ctx)
export default OperationalContext