index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { makeStyles } from "@material-ui/core/styles";
  2. import SendIcon from '@mui/icons-material/Send';
  3. import MicNoneIcon from '@mui/icons-material/MicNone';
  4. import AttachFileIcon from '@mui/icons-material/AttachFile';
  5. import SentimentSatisfiedAltIcon from '@mui/icons-material/SentimentSatisfiedAlt';
  6. import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward';
  7. import Avatar from '@mui/material/Avatar';
  8. import CloseIcon from '@mui/icons-material/Close';
  9. import { useReactMediaRecorder } from "react-media-recorder";
  10. import { useState } from "react";
  11. import { useSelector } from "react-redux";
  12. import FilesMenu from "../FilesMenu";
  13. import NotDone from "../../../../reusableComponents/NotDone";
  14. import {
  15. sentMessageById, sentImgMessageById, sentAudioMessageById,
  16. sentVideoMessageById,sentFileMessageById
  17. } from '../../../../../api-data'
  18. import { getChat } from '../../../../../redux/chat/selector'
  19. import { getIsOpen } from '../../../../../redux/control/selector'
  20. import { playNotification } from "../../../../../helpers";
  21. import { typingChat } from "../../../../../api-data";
  22. const useStyles = makeStyles({
  23. container: {
  24. display: "flex",
  25. justifyContent: "space-between",
  26. alignContent: 'flex-end',
  27. alignItems:'flex-end',
  28. width: 700,
  29. position: 'fixed',
  30. bottom: 20,
  31. paddingTop:10,
  32. },
  33. inputContainer: {
  34. position:'relative',
  35. minWidth: 634,
  36. maxWidth: 634,
  37. maxHeight:56,
  38. display: 'flex',
  39. flexWrap: 'nowrap',
  40. alignContent: 'center',
  41. alignItems: 'center',
  42. color: '#6b6b6b',
  43. backgroundColor: '#ffffff',
  44. borderRadius: 8,
  45. padding: 10,
  46. marginRight: 10,
  47. "&:after": {
  48. content: "''",
  49. position: "absolute",
  50. width: "0",
  51. height: "0",
  52. borderBottom: "15px solid #ffffff",
  53. borderLeft: "15px solid transparent",
  54. borderRight: "15px solid transparent",
  55. bottom: "0",
  56. right: "-15px"
  57. },
  58. "&:before": {
  59. content: "''",
  60. position: "absolute",
  61. width: "0",
  62. height: "0",
  63. borderBottom: "17px solid #ffffff",
  64. borderLeft: "16px solid transparent",
  65. borderRight: "16px solid transparent",
  66. bottom: "0px",
  67. right: "-17px"
  68. }
  69. },
  70. textarea: {
  71. minWidth: 550,
  72. maxWidth: 550,
  73. outline: 'none',
  74. border:'none',
  75. padding: '0px 10px',
  76. marginLeft: 8,
  77. marginRight: 8,
  78. overflowY:'auto',
  79. resize: 'none',
  80. height: 'auto',
  81. '&::placeholder': {
  82. color: 'rgb(82, 82, 82)',
  83. fontWeight: 600
  84. }
  85. },
  86. attachIcon: {
  87. transform:'rotate(30deg)',
  88. },
  89. filesMenu: {
  90. background: '#fdfdfd',
  91. position: 'absolute',
  92. width: 300,
  93. maxWidth: '100%',
  94. left: '61%',
  95. bottom:90,
  96. zIndex: 10,
  97. visibility: 'visible',
  98. borderRadius: 10,
  99. padding: '4px 6px',
  100. },
  101. emoji: {
  102. background: '#fdfdfd',
  103. position: 'absolute',
  104. width: 350,
  105. height:350,
  106. bottom:90,
  107. zIndex: 10,
  108. visibility: 'visible',
  109. borderRadius: 10,
  110. padding: '0px 6px',
  111. },
  112. avatar: {
  113. '&:hover': {
  114. backgroundColor: 'rgb(41, 139, 231)',
  115. color: '#ffffff',
  116. }
  117. },
  118. avatarArrow: {
  119. left: 120,
  120. bottom:200,
  121. '&:hover': {
  122. backgroundColor: 'rgb(41, 139, 231)',
  123. color: '#ffffff',
  124. }
  125. },
  126. iconCancel: {
  127. position: 'absolute',
  128. left:-45,
  129. display:'flex',
  130. backgroundColor: '#945353',
  131. width: 56,
  132. height: 56,
  133. color: '#6b6b6b',
  134. borderRadius: '50%',
  135. '&:hover': {
  136. backgroundColor: 'rgb(218, 18, 18)',
  137. color: '#ffffff',
  138. }
  139. },
  140. overlay: {
  141. position: 'fixed',
  142. top: 0,
  143. left: 0,
  144. width: '100vw',
  145. height: '100vh',
  146. zIndex:100
  147. },
  148. });
  149. interface ISendMessage{
  150. isArrow:boolean,
  151. handleScrollTo:() => void
  152. }
  153. const SendMessage = ({isArrow,handleScrollTo}:ISendMessage) => {
  154. const classes = useStyles();
  155. const { companionId } = useSelector(getChat)
  156. const isOpen = useSelector(getIsOpen)
  157. const [value, setValue] = useState<string>('')
  158. const [file, setFile] = useState<any>(null)
  159. const [isOpenMenu, setIsOpenMenu] = useState<boolean>(false)
  160. const [isOpenEmoji, setIsOpenEmoji] = useState<boolean>(false)
  161. const [isRecording, setIsRecording] = useState<boolean>(false)
  162. const [type, setType] = useState<string>('')
  163. const { status, startRecording, stopRecording, mediaBlobUrl,clearBlobUrl } = useReactMediaRecorder({ audio: true });
  164. const sentMessage = async () => {
  165. setFile(null)
  166. setIsRecording(false)
  167. setValue('')
  168. setType('')
  169. isOpenMenu&&setIsOpenMenu(false)
  170. isOpenEmoji && setIsOpenEmoji(false)
  171. if (value) sentMessageById(companionId, value)
  172. if (mediaBlobUrl && type) {
  173. if (type === 'recording') {
  174. const audio = new XMLHttpRequest();
  175. audio.open('GET', mediaBlobUrl, true);
  176. audio.responseType = 'blob';
  177. audio.onload = () => {
  178. if (audio.status === 200) {
  179. const name = `audioMessage${new Date().getSeconds()}.mp3`
  180. const blob = audio.response
  181. const file = new File([blob], name, {
  182. type: 'audio/mpeg'
  183. })
  184. const formData: any = new FormData()
  185. formData.append("audio", file)
  186. sentAudioMessageById(companionId, formData)
  187. clearBlobUrl()
  188. }
  189. }
  190. audio.send();
  191. }
  192. }
  193. if (file && type) {
  194. if (file.type.includes('image') && type === 'content') {
  195. const formData: any = new FormData()
  196. formData.append("image", file);
  197. await sentImgMessageById(companionId, formData)
  198. }
  199. if (file.type.includes('audio') && type === 'content') {
  200. const formData: any = new FormData()
  201. formData.append("audio", file);
  202. sentAudioMessageById(companionId, formData)
  203. }
  204. if (file.type.includes('video') && type === 'content') {
  205. const formData: any = new FormData()
  206. formData.append("video", file);
  207. sentVideoMessageById(companionId, formData)
  208. }
  209. if (file.type.includes('application') && type === 'application') {
  210. const formData: any = new FormData()
  211. formData.append("file", file);
  212. sentFileMessageById(companionId, formData)
  213. }
  214. }
  215. setTimeout(handleScrollTo, 3000);
  216. playNotification('http://localhost:3000/send.mp3')
  217. }
  218. const handleTextarea = (e: React.ChangeEvent<HTMLTextAreaElement>) => setValue(e.target.value)
  219. const handleFocusTextarea = async () => await typingChat(companionId,true)
  220. const handleBlurTextarea = async () => await typingChat(companionId,false)
  221. const handleKeyPres = (e: any) => {
  222. if(e.code === 'Enter'&& value) sentMessage()
  223. }
  224. const handleOpenFileMenu = () => !isOpenMenu&&setIsOpenMenu(true)
  225. const handleCloseFileMenu = (e:any) => {
  226. if (e.target.id === 'overlay'&&isOpenMenu) setIsOpenMenu(false)
  227. }
  228. const handleOpenEmoji = () => !isOpenEmoji&&setIsOpenEmoji(true)
  229. const handleCloseEmoji = (e: any) => {
  230. if (e.target.id === 'overlay'&&isOpenEmoji) setIsOpenEmoji(false)
  231. }
  232. const handleRecording = () => {
  233. if (isRecording) {
  234. stopRecording()
  235. } else {
  236. startRecording()
  237. setType('recording')
  238. setIsRecording(true)
  239. }
  240. }
  241. const handleClearMessage = () => {
  242. file&&setFile(null)
  243. value&&setValue('')
  244. type&&setType('')
  245. }
  246. return (
  247. <div className={classes.container} style={{borderTop:isArrow?'solid 1px #ffffff':'none'}} >
  248. <div onKeyPress={handleKeyPres} className={classes.inputContainer}>
  249. <CloseIcon onClick={handleClearMessage} fontSize="small" className={classes.iconCancel}
  250. sx={{backgroundColor: '#ffffff', width: 36, height: 36, color: '#949393',
  251. display: file || value ? 'inline-block' : 'none'}} />
  252. <SentimentSatisfiedAltIcon onClick={handleOpenEmoji}
  253. fontSize='medium' sx={{
  254. color: isOpenEmoji ? 'rgb(41, 139, 231)' : '#6b6b6b', cursor: 'pointer',
  255. '&:hover': { color: 'rgb(41, 139, 231)' }
  256. }} />
  257. <div onClick={handleCloseEmoji} className={classes.overlay} id='overlay'
  258. style={{ display: isOpenEmoji ? 'block':'none'}}>
  259. <div className={classes.emoji} style={{left: isOpen&&isOpen !== 'menu'?'31%':'44%'}}>
  260. <NotDone name='Emoji Bar'/>
  261. </div>
  262. </div>
  263. <textarea disabled={file ? true : false} value={value} onBlur={handleBlurTextarea}
  264. onFocus={handleFocusTextarea} onChange={handleTextarea} className={classes.textarea}
  265. placeholder={file ? 'The File is ready to send' : status === 'idle' ? 'Message' :
  266. `${status === 'recording'?'Recording':'Audio message Recorded'}`} rows={1}>
  267. </textarea>
  268. <AttachFileIcon onClick={handleOpenFileMenu} className={classes.attachIcon}
  269. fontSize='medium' sx={{
  270. color: isOpenMenu ? 'rgb(41, 139, 231)' : '#6b6b6b', cursor: 'pointer',
  271. '&:hover': { color: 'rgb(41, 139, 231)' }
  272. }} />
  273. <div onClick={handleCloseFileMenu} className={classes.overlay} id='overlay'
  274. style={{ display: isOpenMenu ? 'block':'none'}}>
  275. <div className={classes.filesMenu} style={{left: isOpen&&isOpen !== 'menu'?'48%':'61%'}}>
  276. <FilesMenu setFile={setFile} setValue={setValue} setIsOpenMenu={setIsOpenMenu} setType={setType}/>
  277. </div>
  278. </div>
  279. <Avatar onClick={handleScrollTo} className={classes.avatarArrow}
  280. sx={{backgroundColor: '#ffffff', width: 56, height: 56,
  281. color: '#6b6b6b', display: isArrow ? 'flex' : 'none'}}>
  282. <ArrowDownwardIcon fontSize="medium" />
  283. </Avatar>
  284. </div>
  285. {value || file || mediaBlobUrl ?
  286. <Avatar onClick={sentMessage} className={classes.avatar} sx={{
  287. backgroundColor: '#ffffff', width: 56, height: 56, color: 'rgb(41, 139, 231)',
  288. }}>
  289. <SendIcon fontSize="medium" />
  290. </Avatar> :
  291. <Avatar onClick={handleRecording} className={!isRecording ? classes.avatar : undefined}
  292. sx={{backgroundColor: isRecording ? 'rgb(41, 139, 231)' : '#ffffff',
  293. color: isRecording ? '#ffffff' : '#6b6b6b', width: 56, height: 56}}
  294. style={{animation: isRecording ? 'ripple 1.5s infinite ease-in-out' : 'none',
  295. border: isRecording?'solid 5px #ffffff':'none'}}>
  296. <MicNoneIcon fontSize="medium" />
  297. </Avatar>}
  298. </div>
  299. )
  300. }
  301. export default SendMessage