feature/IO-3000-messaging-sockets-migrations2 - Updated Polling Intervals is now socket based over FCMToken based
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { InfoCircleOutlined, MessageOutlined, ShrinkOutlined, SyncOutlined } from "@ant-design/icons";
|
import { InfoCircleOutlined, MessageOutlined, ShrinkOutlined, SyncOutlined } from "@ant-design/icons";
|
||||||
import { useLazyQuery, useQuery } from "@apollo/client";
|
import { useLazyQuery, useQuery } from "@apollo/client";
|
||||||
import { Badge, Card, Col, Row, Space, Tag, Tooltip, Typography } from "antd";
|
import { Badge, Card, Col, Row, Space, Tag, Tooltip, Typography } from "antd";
|
||||||
import React, { useCallback, useEffect, useState } from "react";
|
import React, { useCallback, useContext, useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import { createStructuredSelector } from "reselect";
|
import { createStructuredSelector } from "reselect";
|
||||||
@@ -13,18 +13,21 @@ import ChatConversationContainer from "../chat-conversation/chat-conversation.co
|
|||||||
import ChatNewConversation from "../chat-new-conversation/chat-new-conversation.component";
|
import ChatNewConversation from "../chat-new-conversation/chat-new-conversation.component";
|
||||||
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
|
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
|
||||||
import "./chat-popup.styles.scss";
|
import "./chat-popup.styles.scss";
|
||||||
|
import SocketContext from "../../contexts/SocketIO/socketContext.jsx";
|
||||||
|
|
||||||
const mapStateToProps = createStructuredSelector({
|
const mapStateToProps = createStructuredSelector({
|
||||||
selectedConversation: selectSelectedConversation,
|
selectedConversation: selectSelectedConversation,
|
||||||
chatVisible: selectChatVisible
|
chatVisible: selectChatVisible
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapDispatchToProps = (dispatch) => ({
|
const mapDispatchToProps = (dispatch) => ({
|
||||||
toggleChatVisible: () => dispatch(toggleChatVisible())
|
toggleChatVisible: () => dispatch(toggleChatVisible())
|
||||||
});
|
});
|
||||||
|
|
||||||
export function ChatPopupComponent({ chatVisible, selectedConversation, toggleChatVisible }) {
|
export function ChatPopupComponent({ chatVisible, selectedConversation, toggleChatVisible }) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [pollInterval, setpollInterval] = useState(0);
|
const [pollInterval, setPollInterval] = useState(0);
|
||||||
|
const { socket } = useContext(SocketContext);
|
||||||
|
|
||||||
const { data: unreadData } = useQuery(UNREAD_CONVERSATION_COUNT, {
|
const { data: unreadData } = useQuery(UNREAD_CONVERSATION_COUNT, {
|
||||||
fetchPolicy: "network-only",
|
fetchPolicy: "network-only",
|
||||||
@@ -39,16 +42,29 @@ export function ChatPopupComponent({ chatVisible, selectedConversation, toggleCh
|
|||||||
...(pollInterval > 0 ? { pollInterval } : {})
|
...(pollInterval > 0 ? { pollInterval } : {})
|
||||||
});
|
});
|
||||||
|
|
||||||
const fcmToken = sessionStorage.getItem("fcmtoken");
|
|
||||||
|
|
||||||
//TODO: Change to be a fallback incase sockets shit the bed
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (fcmToken) {
|
const handleSocketStatus = () => {
|
||||||
setpollInterval(0);
|
if (socket?.connected) {
|
||||||
} else {
|
setPollInterval(15 * 60 * 1000); // 15 minutes
|
||||||
setpollInterval(90000);
|
} else {
|
||||||
|
setPollInterval(60 * 1000); // 60 seconds
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
handleSocketStatus();
|
||||||
|
|
||||||
|
if (socket) {
|
||||||
|
socket.on("connect", handleSocketStatus);
|
||||||
|
socket.on("disconnect", handleSocketStatus);
|
||||||
}
|
}
|
||||||
}, [fcmToken]);
|
|
||||||
|
return () => {
|
||||||
|
if (socket) {
|
||||||
|
socket.off("connect", handleSocketStatus);
|
||||||
|
socket.off("disconnect", handleSocketStatus);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [socket]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (chatVisible)
|
if (chatVisible)
|
||||||
@@ -56,6 +72,8 @@ export function ChatPopupComponent({ chatVisible, selectedConversation, toggleCh
|
|||||||
variables: {
|
variables: {
|
||||||
offset: 0
|
offset: 0
|
||||||
}
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error(`Error fetching conversations: ${(err, err.message || "")}`);
|
||||||
});
|
});
|
||||||
}, [chatVisible, getConversations]);
|
}, [chatVisible, getConversations]);
|
||||||
|
|
||||||
@@ -65,10 +83,12 @@ export function ChatPopupComponent({ chatVisible, selectedConversation, toggleCh
|
|||||||
variables: {
|
variables: {
|
||||||
offset: data.conversations.length
|
offset: data.conversations.length
|
||||||
}
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error(`Error fetching more conversations: ${(err, err.message || "")}`);
|
||||||
});
|
});
|
||||||
}, [data, fetchMore]);
|
}, [data, fetchMore]);
|
||||||
|
|
||||||
const unreadCount = unreadData?.messages_aggregate.aggregate.count || 0;
|
const unreadCount = unreadData?.messages_aggregate?.aggregate?.count || 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Badge count={unreadCount}>
|
<Badge count={unreadCount}>
|
||||||
|
|||||||
Reference in New Issue
Block a user