about summary refs log tree commit diff
path: root/src/components/PolicyUpdateOverlay/context.tsx
blob: 68ae7bbd8694ad216f1a0ac34ba0ba6762d22138 (plain) (blame)
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
import {createContext, type ReactNode, useContext} from 'react'

import {Provider as PortalProvider} from '#/components/PolicyUpdateOverlay/Portal'
import {
  type PolicyUpdateState,
  usePolicyUpdateState,
} from '#/components/PolicyUpdateOverlay/usePolicyUpdateState'

const Context = createContext<PolicyUpdateState>({
  completed: true,
  complete: () => {},
})

export function usePolicyUpdateStateContext() {
  const context = useContext(Context)
  if (!context) {
    throw new Error(
      'usePolicyUpdateStateContext must be used within a PolicyUpdateProvider',
    )
  }
  return context
}

export function Provider({children}: {children?: ReactNode}) {
  const state = usePolicyUpdateState()

  return (
    <PortalProvider>
      <Context.Provider value={state}>{children}</Context.Provider>
    </PortalProvider>
  )
}