From 8887e1b51565b992f34c955c459125eb85b28483 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Mon, 31 Oct 2022 22:48:30 +0300
Subject: `useVisitor` hook
---
vnext/src/ui/VisitorContext.js | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 vnext/src/ui/VisitorContext.js
(limited to 'vnext/src/ui/VisitorContext.js')
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 {children};
+}
+
+/**
+ * Visitor hook
+ *
+ * @returns {[
+ * import('../api').SecureUser,
+ * import('react').Dispatch>
+ * ]} 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
--
cgit v1.2.3