aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/VisitorContext.js
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-10-31 22:48:30 +0300
committerGravatar Vitaly Takmazov2023-01-13 10:37:58 +0300
commit8887e1b51565b992f34c955c459125eb85b28483 (patch)
tree7fc8130f523014864e2d60aa9628e7a7ee7e7dd5 /vnext/src/ui/VisitorContext.js
parentfc96a9a206a825171da87a7f23cc2ea16b1d645d (diff)
`useVisitor` hook
Diffstat (limited to 'vnext/src/ui/VisitorContext.js')
-rw-r--r--vnext/src/ui/VisitorContext.js32
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