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