index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 { useState } from "react";
  10. import { useSelector } from "react-redux";
  11. import FilesMenu from "../FilesMenu";
  12. import NotDone from "../../../../reusableComponents/NotDone";
  13. import {
  14. sentMessageById, sentImgMessageById, sentAudioMessageById,
  15. sentVideoMessageById,sentFileMessageById
  16. } from '../../../../../api-data'
  17. import { getChat } from '../../../../../redux/chat/selector'
  18. import { playNotification } from "../../../../../helpers";
  19. import { typingChat } from "../../../../../api-data";
  20. const useStyles = makeStyles({
  21. container: {
  22. display: "flex",
  23. justifyContent: "space-between",
  24. alignContent: 'flex-end',
  25. alignItems:'flex-end',
  26. width: 700,
  27. position: 'fixed',
  28. bottom: 20,
  29. paddingTop:10,
  30. },
  31. inputContainer: {
  32. position:'relative',
  33. minWidth: 634,
  34. maxWidth: 634,
  35. maxHeight:56,
  36. display: 'flex',
  37. flexWrap: 'nowrap',
  38. alignContent: 'center',
  39. alignItems: 'center',
  40. color: '#6b6b6b',
  41. backgroundColor: '#ffffff',
  42. borderRadius: 8,
  43. padding: 10,
  44. marginRight: 10,
  45. "&:after": {
  46. content: "''",
  47. position: "absolute",
  48. width: "0",
  49. height: "0",
  50. borderBottom: "15px solid #ffffff",
  51. borderLeft: "15px solid transparent",
  52. borderRight: "15px solid transparent",
  53. bottom: "0",
  54. right: "-15px"
  55. },
  56. "&:before": {
  57. content: "''",
  58. position: "absolute",
  59. width: "0",
  60. height: "0",
  61. borderBottom: "17px solid #ffffff",
  62. borderLeft: "16px solid transparent",
  63. borderRight: "16px solid transparent",
  64. bottom: "0px",
  65. right: "-17px"
  66. }
  67. },
  68. textarea: {
  69. minWidth: 550,
  70. maxWidth: 550,
  71. outline: 'none',
  72. border:'none',
  73. padding: '0px 10px',
  74. marginLeft: 8,
  75. marginRight: 8,
  76. overflowY:'auto',
  77. resize: 'none',
  78. height: 'auto',
  79. },
  80. attachIcon: {
  81. transform:'rotate(30deg)',
  82. },
  83. filesMenu: {
  84. background: '#fdfdfd',
  85. position: 'absolute',
  86. width: 300,
  87. maxWidth: '100%',
  88. left: 406,
  89. bottom:64,
  90. zIndex: 10,
  91. visibility: 'visible',
  92. borderRadius: 10,
  93. padding: '4px 6px',
  94. },
  95. emoji: {
  96. background: '#fdfdfd',
  97. position: 'absolute',
  98. width: 350,
  99. height:350,
  100. left: 0,
  101. bottom:64,
  102. zIndex: 10,
  103. visibility: 'visible',
  104. borderRadius: 10,
  105. padding: '0px 6px',
  106. },
  107. avatar: {
  108. '&:hover': {
  109. backgroundColor: 'rgb(41, 139, 231)',
  110. color: '#ffffff',
  111. }
  112. },
  113. avatarArrow: {
  114. left: 120,
  115. bottom:200,
  116. '&:hover': {
  117. backgroundColor: 'rgb(41, 139, 231)',
  118. color: '#ffffff',
  119. }
  120. },
  121. iconCancel: {
  122. position: 'absolute',
  123. left:-45,
  124. display:'flex',
  125. backgroundColor: '#945353',
  126. width: 56,
  127. height: 56,
  128. color: '#6b6b6b',
  129. borderRadius: '50%',
  130. '&:hover': {
  131. backgroundColor: 'rgb(218, 18, 18)',
  132. color: '#ffffff',
  133. }
  134. },
  135. overlay: {
  136. position: 'fixed',
  137. top: 0,
  138. left: 0,
  139. width: '100vw',
  140. height: '100vh',
  141. zIndex:100
  142. },
  143. });
  144. interface ISendMessage{
  145. isArrow:boolean,
  146. handleScrollTo:() => void
  147. }
  148. const SendMessage = ({isArrow,handleScrollTo}:ISendMessage) => {
  149. const classes = useStyles();
  150. const { companionId } = useSelector(getChat)
  151. const [value, setValue] = useState<string>('')
  152. const [file, setFile] = useState<any>(null)
  153. const [isOpenMenu, setIsOpenMenu] = useState<boolean>(false)
  154. const [isOpenEmoji, setIsOpenEmoji] = useState<boolean>(false)
  155. const [type, setType] = useState<string>('')
  156. const sentMessage = async () => {
  157. setValue('')
  158. setFile(null)
  159. isOpenMenu&&setIsOpenMenu(false)
  160. isOpenEmoji && setIsOpenEmoji(false)
  161. if (value) sentMessageById(companionId, value)
  162. if (file && file.type&&type) {
  163. if (file.type.includes('image') && type === 'content') {
  164. const formData: any = new FormData()
  165. formData.append("image", file);
  166. await sentImgMessageById(companionId, formData)
  167. }
  168. if (file.type.includes('audio') && type === 'content') {
  169. const formData: any = new FormData()
  170. formData.append("audio", file);
  171. sentAudioMessageById(companionId, formData)
  172. }
  173. if (file.type.includes('video') && type === 'content') {
  174. const formData: any = new FormData()
  175. formData.append("video", file);
  176. sentVideoMessageById(companionId, formData)
  177. }
  178. if (file.type.includes('application') && type === 'application') {
  179. const formData: any = new FormData()
  180. formData.append("file", file);
  181. sentFileMessageById(companionId, formData)
  182. }
  183. }
  184. setTimeout(handleScrollTo, 3000);
  185. playNotification('http://localhost:3000/send.mp3')
  186. }
  187. const handleTextarea = (e: React.ChangeEvent<HTMLTextAreaElement>) => setValue(e.target.value)
  188. const handleFocusTextarea = async () => await typingChat(companionId,true)
  189. const handleBlurTextarea = async () => await typingChat(companionId,false)
  190. const handleKeyPres = (e: any) => {
  191. if(e.code === 'Enter'&& value) sentMessage()
  192. }
  193. const handleOpenFileMenu = () => !isOpenMenu&&setIsOpenMenu(true)
  194. const handleCloseFileMenu = (e:any) => {
  195. if (e.target.id === 'overlay'&&isOpenMenu) setIsOpenMenu(false)
  196. }
  197. const handleOpenEmoji = () => !isOpenEmoji&&setIsOpenEmoji(true)
  198. const handleCloseEmoji = (e: any) => {
  199. if (e.target.id === 'overlay'&&isOpenEmoji) setIsOpenEmoji(false)
  200. }
  201. const handleClearMessage = () => setFile(null)
  202. console.log(file)
  203. return (
  204. <div className={classes.container} style={{borderTop:isArrow?'solid 1px #ffffff':'none'}} >
  205. <div onKeyPress={handleKeyPres} className={classes.inputContainer}>
  206. <CloseIcon onClick={handleClearMessage} fontSize="small" className={classes.iconCancel}
  207. sx={{backgroundColor:'#ffffff',width:36,height:36,color:'#949393',display:file?'inline-block':'none'}}/>
  208. <SentimentSatisfiedAltIcon onClick={handleOpenEmoji}
  209. fontSize='medium' sx={{
  210. color: isOpenEmoji ? 'rgb(41, 139, 231)' : '#6b6b6b', cursor: 'pointer',
  211. '&:hover': { color: 'rgb(41, 139, 231)' }
  212. }} />
  213. <div onClick={handleCloseEmoji} className={classes.overlay} id='overlay'
  214. style={{ display: isOpenEmoji ? 'block':'none'}}>
  215. <div className={classes.emoji}>
  216. <NotDone name='Emoji Bar'/>
  217. </div>
  218. </div>
  219. <textarea disabled={file?true:false} value={value} onBlur={handleBlurTextarea} onFocus={handleFocusTextarea} onChange={handleTextarea}
  220. className={classes.textarea} placeholder={file?'Press onto Plane to send or Cross to remove':'Message'} rows={1}></textarea>
  221. <AttachFileIcon onClick={handleOpenFileMenu} className={classes.attachIcon}
  222. fontSize='medium' sx={{
  223. color: isOpenMenu ? 'rgb(41, 139, 231)' : '#6b6b6b', cursor: 'pointer',
  224. '&:hover': { color: 'rgb(41, 139, 231)' }
  225. }} />
  226. <div onClick={handleCloseFileMenu} className={classes.overlay} id='overlay'
  227. style={{ display: isOpenMenu ? 'block':'none'}}>
  228. <div className={classes.filesMenu}>
  229. <FilesMenu setFile={setFile} setValue={setValue} setIsOpenMenu={setIsOpenMenu} setType={setType}/>
  230. </div>
  231. </div>
  232. <Avatar onClick={handleScrollTo} className={classes.avatarArrow} sx={{
  233. backgroundColor: '#ffffff',
  234. width: 56, height: 56 ,color: '#6b6b6b',display:isArrow?'flex':'none'}}>
  235. <ArrowDownwardIcon fontSize="medium" />
  236. </Avatar>
  237. </div>
  238. {value || file ?
  239. <Avatar onClick={sentMessage} className={classes.avatar} sx={{
  240. backgroundColor: '#ffffff',
  241. width: 56, height: 56 ,color: 'rgb(41, 139, 231)'}}>
  242. <SendIcon fontSize="medium" />
  243. </Avatar> :
  244. <Avatar className={classes.avatar} sx={{
  245. backgroundColor: '#ffffff',
  246. width: 56, height: 56, color: '#6b6b6b'}}>
  247. <MicNoneIcon fontSize="medium" />
  248. </Avatar>}
  249. </div>
  250. )
  251. }
  252. export default SendMessage