aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/VisitorContext.js
blob: 8c5364e78fe1590da7a6d3a5419a899ad1d4b30e (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, 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;
}