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

const Visitor = createContext()

/** @type {import('../api').SecureUser} */
const unknownUser = {
    uid: -1
}

/**
 * @param { import('react').PropsWithChildren<{}> } props
 */
export function VisitorProvider({ children }) {
    const state = useState(unknownUser)
    return <Visitor.Provider value={state}>{children}</Visitor.Provider>
}

/**
 * Visitor hook
 * @returns {[
 *  import('../api').SecureUser, 
 *  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
}