Spaces:
Paused
Paused
File size: 4,171 Bytes
a0fda44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import { useEffect } from "react";
import useSocket from "./useSocket";
import { useSelector, useDispatch } from "react-redux";
import { contactsActions } from "../store/contactsSlice";
import useFetch from "./useFetch";
import { chatActions } from "../store/chatSlice";
import { modalActions } from "../store/modalSlice";
import useChatBot from "./useChatBot";
import { authActions } from "../store/authSlice";
const useInit = () => {
// useSocketHook
const { socketEmit, socketListen, userId, socket } = useSocket();
// Set app theme
useEffect(() => {
const initialMode = JSON.parse(localStorage.getItem("darkMode"));
document
.querySelector("html")
.setAttribute("class", initialMode ? "dark" : "null");
}, []);
const { respondAsChatBot } = useChatBot();
// Send default messages from bot
const sendDefaultMessagesFromBot = (chatRoomId) => {
respondAsChatBot({
chatRoomId,
message:
"Hi there, I'm Eddie <br /> <br /> A Chat bot to keep you busy while you are still new to the app (built by Adekola Thanni). <br /> <br /> You can test out some of the features of the app while talking with me but I'll strongly recommend you adding a friend to your contact list. <br /> You can hop on a call and video call with them, something more fun than talking to a robot <img class='w-[2.5rem] h-[2.5rem] inline-block' src='https://cdn.jsdelivr.net/npm/emoji-datasource-apple/img/apple/64/1f643.png'>",
});
};
// Get logged in state
const loggedIn = useSelector((state) => state.authReducer.loggedIn);
const isNew = useSelector((state) => state.authReducer.isNew);
const chatList = useSelector((state) => state.chatListReducer);
const dispatch = useDispatch();
// Fetch user contacts
const { reqFn: fetchContacts } = useFetch(
{ method: "GET", url: "/contacts" },
(data) => {
dispatch(contactsActions.setContacts(data.data.contacts));
}
);
// Moment user logs in, fetch contacts
useEffect(() => {
if (loggedIn) {
fetchContacts();
}
}, [loggedIn]);
// On getting user details
useEffect(() => {
if (userId) {
// Connect socket if disconnected
if (socket.disconnected) {
socket.connect();
}
// Announce logged in status
socketEmit("user:online", userId);
// Listen to online event from other users
socketListen("user:online", (userId) => {
const payload = {
id: userId,
status: {
online: true,
},
};
// Set contact online status
dispatch(contactsActions.setContactOnlineStatus(payload));
// Set chatroom online status
dispatch(chatActions.updateChatProfile({ payload }));
});
// Listen to offline event from other users
socketListen("user:offline", ({ userId, time }) => {
const payload = {
id: userId,
status: {
online: false,
lastSeen: time,
},
};
dispatch(contactsActions.setContactOnlineStatus(payload));
dispatch(chatActions.updateChatProfile({ payload }));
});
}
}, [userId]);
useEffect(() => {
// Listen to call request
socketListen(
"user:callRequest",
({ chatRoomId, signalData, userId, callType }, acknowledgeCall) => {
dispatch(
modalActions.openModal({
type: `${callType}CallModal`,
payload: {
partnerProfile: chatList.find(
(chat) => chat.chatRoomId === chatRoomId
).profile,
callDetail: {
caller: false,
chatRoomId,
callerSignal: signalData,
callerId: userId,
},
},
positions: {},
})
);
acknowledgeCall();
}
);
if (chatList.length && isNew.isNew) {
sendDefaultMessagesFromBot(isNew.payload.chatRoomId);
dispatch(authActions.setUserIsNew({}));
}
return () => {
socket.off("user:callRequest");
};
}, [chatList]);
return {
loggedIn,
};
};
export default useInit;
|