aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/VisitorContext.js
blob: a95090e593210e8562abf64bed5d12fc8537e4c0 (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
import { createContext, useContext, useState } from 'react'

const Visitor = createContext()

/**
 * @param { import('react').PropsWithChildren<{}> & {
 *  auth?: import('../api').SecureUser
 * }} props
 */
export function VisitorProvider({ auth, children }) {
    const state = useState(auth)
    return <Visitor.Provider value={state}>{children}</Visitor.Provider>
}

/**
 * Visitor hook
 * @returns {[
 *  (import('../api').SecureUser|undefined), 
 *  import('react').Dispatch<import('react').SetStateAction<import('../api').SecureUser>>
 * ]} visitor hook
 */
export function useVisitor() {
    const visitor = useContext(Visitor)
    if (visitor === undefined) {
        throw new Error('useVisitor must be used within a VisitorProvider')
    }
    return visitor
}