diff options
Diffstat (limited to 'vnext/src/ui/VisitorContext.js')
-rw-r--r-- | vnext/src/ui/VisitorContext.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/vnext/src/ui/VisitorContext.js b/vnext/src/ui/VisitorContext.js new file mode 100644 index 00000000..8c5364e7 --- /dev/null +++ b/vnext/src/ui/VisitorContext.js @@ -0,0 +1,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; +}
\ No newline at end of file |