import React, { useRef, useState, useEffect, FC } from "react"; import dayjs from "dayjs"; import useInView from "./useInView"; import Tippy from "@tippyjs/react"; import Reaction from "./Reaction"; import Reply from "./Reply"; import Profile from "../Profile"; import Avatar from "../Avatar"; import StyledWrapper from "./styled"; import Commands from "./Commands"; import EditMessage from "./EditMessage"; import renderContent from "./renderContent"; import Tooltip from "../Tooltip"; import ContextMenu from "./ContextMenu"; import useContextMenu from "../../hook/useContextMenu"; import usePinMessage from "../../hook/usePinMessage"; import { useAppSelector } from "../../../app/store"; interface IProps { readOnly?: boolean; contextId: number; context?: "user" | "channel"; read?: boolean; mid: number; updateReadIndex?: (param: any) => void; } const Message: FC = ({ readOnly = false, contextId, mid, context = "user", updateReadIndex, read = true }) => { const { visible: contextMenuVisible, handleContextMenuEvent, hideContextMenu } = useContextMenu(); const inviewRef = useInView(); const [edit, setEdit] = useState(false); const avatarRef = useRef(null); const { getPinInfo } = usePinMessage(context == "channel" ? contextId : 0); const { message, reactionMessageData, usersData } = useAppSelector((store) => { return { reactionMessageData: store.reactionMessage, message: store.message[mid], usersData: store.users.byId }; }); const toggleEditMessage = () => { setEdit((prev) => !prev); }; const { reply_mid, from_uid: fromUid, created_at: time, sending = false, content, thumbnail, download, content_type = "text/plain", edited, properties } = message; useEffect(() => { if (!read) { const data = context == "user" ? { users: [{ uid: +contextId, mid }] } : { groups: [{ gid: +contextId, mid }] }; if (updateReadIndex) { updateReadIndex(data); } } }, [mid, read]); const reactions = reactionMessageData[mid]; const currUser = usersData[fromUid || 0]; // if (!message) return null; let timePrefix = null; const dayjsTime = dayjs(time); timePrefix = dayjsTime.isToday() ? "Today" : dayjsTime.isYesterday() ? "Yesterday" : null; const pinInfo = getPinInfo(mid); // return null; const _key = properties?.local_id || mid; return ( } >
{currUser?.name || "Deleted User"} {timePrefix ? `${timePrefix} ${dayjsTime.format("h:mm A")}` : dayjsTime.format("YYYY-MM-DD h:mm:ss A")}
{reply_mid && } {edit ? ( ) : ( renderContent({ context, to: contextId, from_uid: fromUid, created_at: time, content_type, properties, content, thumbnail, download, edited }) )} {reactions && }
{!edit && !readOnly && ( )}
); }; export default React.memo(Message, (prevs, nexts) => { return prevs.mid == nexts.mid; });