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/Chat.js | 7 ++++---
vnext/src/ui/Comment.js | 5 +++--
vnext/src/ui/Contacts.js | 3 +++
vnext/src/ui/Feeds.js | 31 ++++++++++++++++---------------
vnext/src/ui/Header.js | 6 ++++--
vnext/src/ui/Login.js | 12 ++++++------
vnext/src/ui/Message.js | 5 +++--
vnext/src/ui/PM.js | 7 ++++---
vnext/src/ui/Post.js | 6 ++++--
vnext/src/ui/Settings.js | 14 +++++++++-----
vnext/src/ui/Thread.js | 9 +++++----
vnext/src/ui/Users.js | 1 +
vnext/src/ui/VisitorContext.js | 32 ++++++++++++++++++++++++++++++++
13 files changed, 94 insertions(+), 44 deletions(-)
create mode 100644 vnext/src/ui/VisitorContext.js
(limited to 'vnext/src/ui')
diff --git a/vnext/src/ui/Chat.js b/vnext/src/ui/Chat.js
index 5ecb9c0f..cdf3de1b 100644
--- a/vnext/src/ui/Chat.js
+++ b/vnext/src/ui/Chat.js
@@ -9,10 +9,10 @@ import UserInfo from './UserInfo';
import { getChat, pm } from '../api';
import './Chat.css';
+import { useVisitor } from './VisitorContext';
/**
* @typedef {Object} ChatProps
- * @property {import('../api').SecureUser} visitor
* @property {EventSource} connection
*/
@@ -21,11 +21,12 @@ import './Chat.css';
* @param {ChatProps} props
*/
export default function Chat(props) {
+ const [visitor] = useVisitor();
const [chats, setChats] = useState([]);
const params = useParams();
let loadChat = useCallback((uname) => {
- const { hash } = props.visitor;
+ const { hash } = visitor;
setChats([]);
if (hash && uname) {
getChat(uname)
@@ -33,7 +34,7 @@ export default function Chat(props) {
setChats(response.data);
});
}
- }, [props.visitor]);
+ }, []);
let onMessage = useCallback((json) => {
const msg = JSON.parse(json.data);
diff --git a/vnext/src/ui/Comment.js b/vnext/src/ui/Comment.js
index 756b3487..45c80187 100644
--- a/vnext/src/ui/Comment.js
+++ b/vnext/src/ui/Comment.js
@@ -9,6 +9,7 @@ import MessageInput from './MessageInput';
import { fetchUserUri } from '../api';
import { chatItemStyle } from './helpers/BubbleStyle';
import { format, embedUrls } from '../utils/embed';
+import { useVisitor } from './VisitorContext';
let isMounted;
@@ -16,16 +17,16 @@ let isMounted;
* @param {{
msg: import('../api').Message,
draft: string,
- visitor: import('../api').User,
active: number,
setActive: function,
onStartEditing: function,
postComment: function
}} props
*/
-export default function Comment({ msg, draft, visitor, active, setActive, onStartEditing, postComment }) {
+export default function Comment({ msg, draft, active, setActive, onStartEditing, postComment }) {
const embedRef = useRef();
const msgRef = useRef();
+ const [visitor] = useVisitor();
const [author, setAuthor] = useState(msg.user);
useEffect(() => {
if (msgRef.current) {
diff --git a/vnext/src/ui/Contacts.js b/vnext/src/ui/Contacts.js
index 2a727b82..0005da9a 100644
--- a/vnext/src/ui/Contacts.js
+++ b/vnext/src/ui/Contacts.js
@@ -6,6 +6,9 @@ import { getChats } from '../api';
import Contact from './Contact.js';
import { ChatSpinner } from './Spinner';
+/**
+ *
+ */
export default function Contacts(props) {
const [pms, setPms] = useState([]);
useEffect(() => {
diff --git a/vnext/src/ui/Feeds.js b/vnext/src/ui/Feeds.js
index 84319c28..75f1800d 100644
--- a/vnext/src/ui/Feeds.js
+++ b/vnext/src/ui/Feeds.js
@@ -10,6 +10,7 @@ import Spinner from './Spinner';
import UserInfo from './UserInfo';
import { getMessages } from '../api';
+import { useVisitor } from './VisitorContext';
/**
* @typedef {object} Query
@@ -21,12 +22,12 @@ import { getMessages } from '../api';
/**
* @typedef {object} PageProps
* @property {string=} search
- * @property {import('../api').SecureUser} visitor
* @property {import('../api').Message[]=} msgs
*/
-function RequireAuth({ visitor, children }) {
+function RequireAuth({ children }) {
let location = useLocation();
+ let [ visitor ] = useVisitor();
if (!visitor.hash) {
// Redirect them to the /login page, but save the current location they were
// trying to go to when they were redirected. This allows us to send them
@@ -41,7 +42,7 @@ function RequireAuth({ visitor, children }) {
/**
* @param {PageProps} props
*/
-export function Discover({ visitor }) {
+export function Discover() {
const location = useLocation();
let search = qs.parse(location.search.substring(1));
const query = {
@@ -49,24 +50,24 @@ export function Discover({ visitor }) {
search: search,
pageParam: search.search ? 'page' : 'before_mid'
};
- return (