|
@@ -1,43 +1,237 @@
|
|
import React, {useState, useEffect, useRef} from 'react'
|
|
import React, {useState, useEffect, useRef} from 'react'
|
|
|
|
|
|
import { connect } from 'react-redux'
|
|
import { connect } from 'react-redux'
|
|
|
|
+import { dateFromStamp, stringColor, backURL } from '../helpers'
|
|
|
|
+
|
|
|
|
+import { UserAvatar } from '.'
|
|
|
|
|
|
-import { actionFullMsgsByChat } from '../actions'
|
|
|
|
|
|
|
|
const msgsWrapper = {
|
|
const msgsWrapper = {
|
|
display: "flex",
|
|
display: "flex",
|
|
flexDirection: "column-reverse",
|
|
flexDirection: "column-reverse",
|
|
justifyContent: "flex-start",
|
|
justifyContent: "flex-start",
|
|
alignItems: "stretch",
|
|
alignItems: "stretch",
|
|
|
|
+ paddingTop: "10px",
|
|
|
|
+ paddingBottom: "5px"
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const msgBlock = {
|
|
|
|
+ alignSelf: "start",
|
|
|
|
+ display: "flex",
|
|
|
|
+ justifyContent: "flex-start",
|
|
|
|
+ alignItems: "start",
|
|
|
|
+ margin: "2px",
|
|
|
|
+ marginRight: "5%",
|
|
|
|
+ maxWidth: "calc(95% - 2px)",
|
|
|
|
+ minWidth: "40%",
|
|
|
|
+ wordWrap: "break-word",
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const myMsgBlock = {
|
|
|
|
+ alignSelf: "end",
|
|
|
|
+ display: "flex",
|
|
|
|
+ justifyContent: "flex-start",
|
|
|
|
+ alignItems: "start",
|
|
|
|
+ margin: "2px",
|
|
|
|
+ marginLeft: "5%",
|
|
|
|
+ maxWidth: "calc(95% - 2px)",
|
|
|
|
+ minWidth: "40%",
|
|
|
|
+ wordWrap: "break-word",
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const avBlock = {
|
|
|
|
+ alignSelf: "end",
|
|
|
|
+ width: "40px",
|
|
|
|
+ marginRight: "10px"
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const bodyBlock = {
|
|
|
|
+ display: "flex",
|
|
|
|
+ flexDirection: "column",
|
|
|
|
+ justifyContent: "flex-start",
|
|
|
|
+ borderRadius: "5px",
|
|
|
|
+ backgroundColor: "#fff",
|
|
|
|
+ width: "calc(100% - 50px)",
|
|
|
|
+ padding: "10px"
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const myBodyBlock = {
|
|
|
|
+ display: "flex",
|
|
|
|
+ flexDirection: "column",
|
|
|
|
+ justifyContent: "flex-start",
|
|
|
|
+ borderRadius: "5px",
|
|
|
|
+ backgroundColor: "#1976d255",
|
|
|
|
+ width: "calc(100% - 50px)",
|
|
|
|
+ padding: "10px"
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const nameBlock = {
|
|
|
|
+ fontWeight: 600,
|
|
|
|
+ fontSize: "12px",
|
|
|
|
+ alignSelf: "start",
|
|
|
|
+ maxWidth: "100%",
|
|
|
|
+ marginBottom: "5px",
|
|
}
|
|
}
|
|
|
|
+const contentBlock = {
|
|
|
|
+ alignSelf: "stretch",
|
|
|
|
+ maxWidth: "100%",
|
|
|
|
+}
|
|
|
|
+const dateBlock = {
|
|
|
|
+ alignSelf: "end",
|
|
|
|
+ maxWidth: "100%",
|
|
|
|
+ fontSize: "10px",
|
|
|
|
+ color: "#555"
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
-const Msg = ({msg}) => {
|
|
|
|
|
|
|
|
- const {_id, text} = msg
|
|
|
|
|
|
+const Msg = ({ msg, prevOwner, prevTime, myId }) => {
|
|
|
|
|
|
|
|
+ const { _id, text, owner, media, createdAt } = msg
|
|
|
|
+ const {nick, login, avatar} = owner
|
|
|
|
+
|
|
|
|
+ const allMedia = {}
|
|
|
|
+ if (media) for (const file of media) {
|
|
|
|
+ if (file.type) {
|
|
|
|
+
|
|
|
|
+ const objName = file.type.split('/')[0]
|
|
|
|
+ if (allMedia.hasOwnProperty(objName)) {
|
|
|
|
+
|
|
|
|
+ allMedia[objName].push(file)
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ allMedia[objName] = [file]
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // console.log(allMedia)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ const prOwner = prevOwner.current
|
|
|
|
+ prevOwner.current = owner._id
|
|
|
|
+
|
|
|
|
+ const prTime = prevTime.current
|
|
|
|
+ prevTime.current = createdAt
|
|
|
|
+
|
|
|
|
+ // console.log( prTime - createdAt, text)
|
|
|
|
+ const nameBlockNew = {...nameBlock, color: stringColor.stringToColor(nick || login)}
|
|
return (
|
|
return (
|
|
<>
|
|
<>
|
|
- <p>
|
|
|
|
- {text}
|
|
|
|
- </p>
|
|
|
|
|
|
+ <div
|
|
|
|
+ style={(myId === owner._id) ?
|
|
|
|
+ ( (prOwner === owner._id) ?
|
|
|
|
+ {...myMsgBlock, marginBottom: "2px"} : {...myMsgBlock, marginBottom: "15px"}) :
|
|
|
|
+ ( (prOwner === owner._id) ?
|
|
|
|
+ {...msgBlock, marginBottom: "2px"} : {...msgBlock, marginBottom: "15px"})
|
|
|
|
+ }
|
|
|
|
+ >
|
|
|
|
+ <div style={avBlock} >
|
|
|
|
+ { (prOwner === owner._id &&
|
|
|
|
+ prTime - createdAt < 600000) ?
|
|
|
|
+ <></> : <UserAvatar profile={owner} /> }
|
|
|
|
+ </div>
|
|
|
|
+ <div style={(myId === owner._id) ? myBodyBlock : bodyBlock} >
|
|
|
|
+
|
|
|
|
+ <div style={(myId === owner._id) ? nameBlock : nameBlockNew} >
|
|
|
|
+ {nick || login}
|
|
|
|
+ </div>
|
|
|
|
+
|
|
|
|
+ <div style={contentBlock} >
|
|
|
|
+
|
|
|
|
+ {
|
|
|
|
+ (media && media.length !== 0) &&
|
|
|
|
+ <div>
|
|
|
|
+ {media.map((mediaObj) => <img key={mediaObj._id}
|
|
|
|
+ // type={mediaObj.type}
|
|
|
|
+ style={{maxWidth: "400px"}}
|
|
|
|
+ src={backURL + mediaObj.url } />
|
|
|
|
+ )}
|
|
|
|
+ </div>
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ <pre>
|
|
|
|
+ {text}
|
|
|
|
+ </pre>
|
|
|
|
+
|
|
|
|
+ </div>
|
|
|
|
+
|
|
|
|
+ <div style={dateBlock} >
|
|
|
|
+ {dateFromStamp(createdAt)}
|
|
|
|
+ </div>
|
|
|
|
+ </div>
|
|
|
|
+ </div>
|
|
</>
|
|
</>
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-const MsgList = ({chats, chatId}) => {
|
|
|
|
|
|
+const MsgList = ({chats={}, chatId, myId }) => {
|
|
|
|
|
|
- const msgArr = chats[chatId]?.messages
|
|
|
|
|
|
+ const prevOwner = useRef('')
|
|
|
|
+ const prevTime = useRef('')
|
|
|
|
+
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ prevOwner.current = ''
|
|
|
|
+ prevTime.current = ''
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ // надо ли писать реф?
|
|
|
|
+ const msgArr = chats[chatId]?.messages
|
|
|
|
|
|
return (
|
|
return (
|
|
- <div style={msgsWrapper}>
|
|
|
|
|
|
+ <div
|
|
|
|
+ style={msgsWrapper}
|
|
|
|
+ >
|
|
{ msgArr ?
|
|
{ msgArr ?
|
|
- msgArr.map(msg => <Msg key={msg._id} msg={msg}/>) :
|
|
|
|
- <div>сообщений нема</div>
|
|
|
|
|
|
+
|
|
|
|
+ msgArr.map(msg => <Msg
|
|
|
|
+ key={msg._id}
|
|
|
|
+ msg={msg}
|
|
|
|
+ prevOwner={prevOwner}
|
|
|
|
+ prevTime={prevTime}
|
|
|
|
+ myId={myId}
|
|
|
|
+ />) :
|
|
|
|
+
|
|
|
|
+ <div>сообщений нема</div>
|
|
}
|
|
}
|
|
|
|
|
|
</div>
|
|
</div>
|
|
)
|
|
)
|
|
}
|
|
}
|
|
-export const CMsgList = connect(state => ({chats: state.chats}))(MsgList)
|
|
|
|
|
|
+export const CMsgList = connect(state => ( { chats: state.chats,
|
|
|
|
+ myId: state.auth.payload?.sub?.id || null} ))(MsgList)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//tg
|
|
|
|
+
|
|
|
|
+{/* <div class="media-inner dark interactive" style="">
|
|
|
|
+ <canvas class="thumbnail" width="1304" height="720" style="width: 480px; height: 265px;">
|
|
|
|
+ </canvas>
|
|
|
|
+ <img class="thumbnail opacity-transition slow" alt=""
|
|
|
|
+ draggable="true" style="width: 480px; height: 265px;">
|
|
|
|
+ <video class="full-media" width="480" height="265" autoplay="" loop="" playsinline="" draggable="true">
|
|
|
|
+ <source src="blob:https://web.telegram.org/464cc156-4e7f-415e-bec5-5fdf5b6c2d98">
|
|
|
|
+ </video>
|
|
|
|
+ <div class="message-media-duration">5:27</div>
|
|
|
|
+</div> */}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+{/* <div class="File interactive">
|
|
|
|
+ <div class="file-icon-container">
|
|
|
|
+ <div class="file-icon orange">
|
|
|
|
+ <span class="file-ext" dir="auto">
|
|
|
|
+ zip
|
|
|
|
+ </span>
|
|
|
|
+ </div>
|
|
|
|
+ <i class="action-icon icon-download"></i>
|
|
|
|
+ </div>
|
|
|
|
+ <div class="file-info">
|
|
|
|
+ <div class="file-title" dir="auto">2.1 01-02.zip</div>
|
|
|
|
+ <div class="file-subtitle" dir="auto">
|
|
|
|
+ <span>0.6 KB</span>
|
|
|
|
+ </div>
|
|
|
|
+ </div>
|
|
|
|
+ </div> */}
|