import React, { useState, useRef, useEffect, useCallback } from "react";
import {
Card,
CloseButton,
Nav,
OverlayTrigger,
Button,
Col,
Row,
} from "react-bootstrap";
import StorageImage from "./StorageImage";
import ProfileLink from "./ProfileLink";
import { FiEdit } from "react-icons/fi";
import { HiOutlinePhotograph } from "react-icons/hi";
import { MdSend } from "react-icons/md";
import StyledTextarea from "./StyledTextarea";
import UploadPhoto from "./UploadPhoto";
import Conversation from "./Conversation";
import {
addPhoto,
handleTextareaChange,
delPhoto,
handleKeyPress,
} from "./helper";
import "./Contacts.css";
import { useSelector } from "react-redux";
import {
subscribeMessages,
updateToBeRead,
uploadMessage,
} from "../backend/backend";
const Contacts = () => {
const [showOverlay, setShowOverlay] = useState(false);
const [recipient, setRecipient] = useState(null);
const [showPhotoDlg, setShowPhotoDlg] = useState(null);
const user = useSelector((state) => state.currentUser);
const userID = useSelector((state) => state.user.id);
const users = useSelector((state) => state.users);
const WELCOME_TEXT = "Aa";
const INIT_MESSAGE = {
sender: `${userID}`,
recipient: "",
text: "",
isPhoto: false,
photoURL: "",
isRead: false,
};
const [message, setMessage] = useState(INIT_MESSAGE);
function handleClick(user) {
if (!user && recipient) return;
if (recipient && user.userID !== recipient.userID) return;
setShowOverlay(true);
let id;
if (user) id = user.userID;
else id = "";
const newMessage = { ...message };
newMessage.recipient = id;
setMessage(newMessage);
setRecipient(user);
}
const handleClickCallback = useCallback(handleClick, [user]);
async function handleClose() {
await updateReadStatusOfMessages(recipient);
removeSender();
setShowOverlay(false);
setRecipient(null);
}
function handleChange(e) {
handleTextareaChange({
e: e,
state: message,
setState: setMessage,
});
}
function addPhotoToMessage(file) {
addPhoto({
state: message,
setState: setMessage,
file: file,
userID: userID,
});
}
function deletePhoto() {
delPhoto({
state: message,
setState: setMessage,
user: user,
userID: userID,
});
}
const convRowRef = useRef(null);
const [scrollHeight, setScrollHeight] = useState("");
function saveMessage() {
uploadMessage(message).then(() => {
setMessage(INIT_MESSAGE);
setScrollHeight(convRowRef.current.scrollHeight);
});
}
useEffect(() => {
if (convRowRef.current) convRowRef.current.scrollTop = scrollHeight;
}, [scrollHeight]);
useEffect(() => {
const unsubscribeIncomingMsg = subscribeMessages("incoming");
const unsubscribeOutgoingMsg = subscribeMessages("outgoing");
return () => {
unsubscribeIncomingMsg();
unsubscribeOutgoingMsg();
};
}, []);
const [senders, setSenders] = useState([]);
const incomingMessages = useSelector((state) => state.incomingMessages);
const unread = incomingMessages.filter((message) => !message.isRead);
useEffect(() => {
const sendersWithUnreadMsg = [];
unread.forEach((msg) => {
const sender = msg.sender;
if (sendersWithUnreadMsg.indexOf(sender) === -1)
sendersWithUnreadMsg.push(sender);
});
if (senders.length !== sendersWithUnreadMsg.length)
setSenders(sendersWithUnreadMsg);
}, [senders, unread]);
useEffect(() => {
if (senders.length === 0) return;
const last = senders.length - 1;
const sender = senders[last];
handleClickCallback(users.find((usr) => usr.userID === sender));
}, [senders, users, handleClickCallback]);
function updateReadStatusOfMessages(sender) {
const messagesToUpdate = unread.filter(
(msg) => msg.sender === sender.userID
);
const updates = [];
messagesToUpdate.forEach((msg) => {
const messageID = msg.id;
updates.push(updateToBeRead(messageID));
});
return Promise.all(updates);
}
function removeSender() {
const newSenders = [...senders];
newSenders.pop();
setSenders(newSenders);
}
//We open the overlay card programmatically again, otherwise the user is unable to send
//more than one message.
if (recipient)
if (showOverlay && message.recipient === "")
//We only do this if we set back the INIT_MESSAGE after previous message had sent.
handleClick(recipient);
return (
<>
{!recipient && (
<>
New Message
To:
>
)}
{recipient && (
)}
{recipient && (
)}
{!recipient && (
{users.map((user, index) =>
user.userID === userID ? (
) : (
)
)}
)}
{recipient && (
{message.text === "" && (
)}
{message.isPhoto && (
)}
handleKeyPress(e, saveMessage)}
welcomeText={WELCOME_TEXT}
value={message.text}
className="w-100 mt-2"
/>
)}
}
>
>
);
};
export default Contacts;