index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import Stack from '@mui/material/Stack';
  2. import IconButton from '@mui/material/IconButton';
  3. import SearchIcon from '@mui/icons-material/Search';
  4. import PhoneIcon from '@mui/icons-material/Phone';
  5. import { makeStyles, Typography } from '@material-ui/core'
  6. import { useState,useEffect,useCallback,useRef } from 'react';
  7. import { useSelector,useDispatch } from 'react-redux';
  8. import ListItemText from '@mui/material/ListItemText';
  9. import ListItemAvatar from '@mui/material/ListItemAvatar';
  10. import Avatar from '@mui/material/Avatar';
  11. import MinimizeIcon from '@mui/icons-material/Minimize';
  12. import CropLandscapeIcon from '@mui/icons-material/CropLandscape';
  13. import CloseIcon from '@mui/icons-material/Close';
  14. import ScreenShareIcon from '@mui/icons-material/ScreenShare';
  15. import StopScreenShareIcon from '@mui/icons-material/StopScreenShare';
  16. import VideocamIcon from '@mui/icons-material/Videocam';
  17. import VideocamOffIcon from '@mui/icons-material/VideocamOff';
  18. import MicIcon from '@mui/icons-material/Mic';
  19. import MicOffIcon from '@mui/icons-material/MicOff';
  20. import CallEndIcon from '@mui/icons-material/CallEnd';
  21. import { getChat } from '../../../redux/chat/selector';
  22. import { getAuthorizationState } from '../../../redux/authorization/selector';
  23. import { prodAwsS3, firstLetter, slicedWord } from '../../../helpers'
  24. import { socketIdChat } from '../../../api-data';
  25. const Peer = require('simple-peer')
  26. const useStyles = makeStyles({
  27. container: {
  28. position: 'absolute',
  29. left: 0,
  30. top: 0,
  31. width: '100vw',
  32. height:'100vh',
  33. overflow: 'hidden',
  34. zIndex:100,
  35. display: 'flex',
  36. justifyContent: 'center',
  37. alignItems: 'center',
  38. alignContent: "center",
  39. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  40. },
  41. shareScreenActive: {
  42. width: '30%',
  43. borderRadius: 7,
  44. margin: 'auto',
  45. border:'solid 2px #0084ff',
  46. },
  47. shareScreenDisabled: {
  48. width: 0,
  49. height:0,
  50. },
  51. modalCall: {
  52. background: 'rgb(36, 36, 36)',
  53. display: 'flex',
  54. flexDirection: 'column',
  55. justifyContent: 'start',
  56. alignItems: 'center',
  57. justifyItems:"center",
  58. width: '34vw',
  59. height:'90vh',
  60. borderRadius: 7,
  61. },
  62. rightIcons: {
  63. display: 'flex',
  64. justifyContent: 'end',
  65. alignContent: 'center',
  66. alignItems: 'center',
  67. width:'100%'
  68. },
  69. rightIconWrapper: {
  70. color: '#ffffff',
  71. cursor: 'pointer',
  72. padding:'3px 10px 3px 10px',
  73. '&:hover': {
  74. backgroundColor:'rgb(80, 80, 80)'
  75. }
  76. },
  77. rightIconWrapperClose: {
  78. color: '#ffffff',
  79. cursor: 'pointer',
  80. padding:'3px 10px 3px 10px',
  81. borderTopRightRadius:7,
  82. '&:hover': {
  83. backgroundColor:'#f02a2a'
  84. }
  85. },
  86. bottomWrapper: {
  87. display: 'flex',
  88. justifyContent: 'center',
  89. padding: 5
  90. },
  91. bottomItem: {
  92. display: 'flex',
  93. flexDirection: 'column',
  94. justifyContent: 'center',
  95. alignContent: 'center',
  96. alignItems: 'center',
  97. cursor:'pointer',
  98. width: 80,
  99. },
  100. bottomIcon: {
  101. cursor:'pointer',
  102. },
  103. bottomIconEndAccept: {
  104. cursor: 'pointer',
  105. '&:hover': {
  106. animation: `$shake 1s`,
  107. animationIterationCount:'infinite',
  108. }
  109. },
  110. ringPulsate: {
  111. backgroundColor:"rgb(80, 80, 80)",
  112. borderRadius: '50%',
  113. height: 60,
  114. width: 60,
  115. position: 'absolute',
  116. left: 10,
  117. top: -8,
  118. animation: `$pulsate 1.5s ease-out`,
  119. animationIterationCount: 'infinite',
  120. opacity: 0,
  121. },
  122. titleIconBottom: {
  123. color: '#ffffff',
  124. fontSize: 13,
  125. paddingTop:7
  126. },
  127. '@keyframes pulsate': {
  128. '0%': {transform: 'scale(1, 1)', opacity: 0},
  129. '50%': { opacity: 1},
  130. '100%': {transform: 'scale(1.2, 1.2)', opacity: 0},
  131. },
  132. '@keyframes shake': {
  133. '0%': { transform: 'rotate(0deg)'},
  134. '11%': { transform: 'rotate(10deg)'},
  135. '22%': { transform: 'rotate(20deg)'},
  136. '33%': { transform: 'rotate(30deg)'},
  137. '44%': { transform: 'rotate(20deg)'},
  138. '55%': { transform: 'rotate(10deg)'},
  139. '66%': { transform: 'rotate(0deg)'},
  140. '77%': { transform: 'rotate(-10deg)'},
  141. '88%': { transform: 'rotate(-20deg)'},
  142. '100%': { transform: 'rotate(-30deg)'},
  143. },
  144. })
  145. interface ICallBar {
  146. callStatus: string,
  147. setCallStatus: any,
  148. socket: any,
  149. myVideoRef :any,
  150. }
  151. const CallBar = ({callStatus,setCallStatus,socket,myVideoRef}:ICallBar) => {
  152. const classes = useStyles()
  153. const { _id } = useSelector(getAuthorizationState)
  154. const chat = useSelector(getChat)
  155. const myAudioRef = useRef<any>(null);
  156. const userAudioRef = useRef<any>(null);
  157. const userVideoRef = useRef<any>(null);
  158. const connectionRef = useRef<any>(null);
  159. const [mySocket, setMySocket] = useState<string>('')
  160. const [companionSocket, setCompanionSocket] = useState<string>('')
  161. const [companionSignal, setCompanionSignal] = useState<string>('')
  162. const [name, setName] = useState<string>('')
  163. const [lastName, setLastName] = useState<string>('')
  164. const [avatarUrl, setAvatarUrl] = useState<string>('')
  165. const [color, setColor] = useState<string>('')
  166. const [number,setNumber] = useState<string>('')
  167. const handleLeaveCall = () => {
  168. connectionRef.current.destroy();
  169. setCallStatus('')
  170. };
  171. // requesting, waiting ,ringing, hanging up,line busy
  172. const handleStartCall = useCallback(async () => {
  173. const stream = await navigator.mediaDevices.getUserMedia({
  174. video: true,
  175. audio: true
  176. })
  177. const peer = new Peer({
  178. initiator: true,
  179. trickle: false,
  180. stream
  181. });
  182. peer.on("signal", (data:any) => {
  183. socket.emit("callTo", {
  184. to: chat.socketId,
  185. signalData: data,
  186. from: mySocket,
  187. userId: _id,
  188. companionId:chat.companionId
  189. })
  190. setCallStatus('ringing')
  191. });
  192. peer.on("stream", (stream: any) => {
  193. console.log(stream,'user stream')
  194. });
  195. socket.on("acceptedCall", ({ signal }: any) => {
  196. peer.signal(signal)
  197. setCallStatus('accepted')
  198. console.log(signal,'signal accepted from companion')
  199. });
  200. connectionRef.current = peer;
  201. },[chat.socketId,chat.companionId,_id,socket,setCallStatus,mySocket])
  202. const handleAnswerCall = useCallback(async () => {
  203. const stream = await navigator.mediaDevices.getUserMedia({
  204. video: true,
  205. audio: true
  206. })
  207. const peer = new Peer({
  208. initiator: false,
  209. trickle: false,
  210. stream,
  211. });
  212. peer.on("signal", (data: any) => {
  213. socket.emit("answerCall", { signal: data, to: companionSocket });
  214. });
  215. peer.on("stream", (stream: any) => {
  216. userVideoRef.current.srcObject = stream;
  217. });
  218. peer.signal(companionSignal);
  219. connectionRef.current = peer;
  220. },[socket,companionSocket,companionSignal])
  221. useEffect(() => {
  222. socket.on("me", (id: string) => {
  223. setMySocket(id)
  224. socketIdChat(id)
  225. })
  226. socket.on('incomeCall', (data: any) => {
  227. setCallStatus('is calling you')
  228. setName(data.name)
  229. setLastName(data.lastName)
  230. setAvatarUrl(data.avatarUrl)
  231. setColor(data.color)
  232. setNumber(data.number)
  233. setCompanionSocket(data.from)
  234. setCompanionSignal(data.signal)
  235. })
  236. },[socket,setCallStatus,setName,setLastName,setCompanionSocket,setCompanionSignal])
  237. useEffect(() => {
  238. if(callStatus === 'requesting') handleStartCall()
  239. }, [callStatus, handleStartCall])
  240. useEffect(() => {
  241. if (callStatus === '') {
  242. setName(chat.name)
  243. setLastName(chat.lastName)
  244. setAvatarUrl(chat.avatarUrl)
  245. setColor(chat.color)
  246. setNumber(chat.number)
  247. }
  248. }, [callStatus,chat])
  249. return (
  250. <div className={classes.container} style={{top: callStatus?0:'-100%'}}>
  251. <div className={classes.modalCall}>
  252. <div className={classes.rightIcons} style={{marginBottom: true?0:40,}}>
  253. <div className={classes.rightIconWrapper}>
  254. <MinimizeIcon fontSize='small' />
  255. </div>
  256. <div className={classes.rightIconWrapper}>
  257. <CropLandscapeIcon fontSize='small' />
  258. </div>
  259. <div className={classes.rightIconWrapperClose} onClick={handleLeaveCall}>
  260. <CloseIcon fontSize='small' />
  261. </div>
  262. </div>
  263. {<ListItemAvatar style={{marginBottom:5}}>
  264. <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
  265. sx={{ background: color, width: 120, height: 120, marginRight: 2, fontSize:30,zIndex:0}}>
  266. {`${firstLetter(name)}${firstLetter(lastName)}`}
  267. </Avatar>
  268. </ListItemAvatar>}
  269. {<div style={{marginBottom:'auto'}}>
  270. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  271. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  272. primaryTypographyProps={{ color: '#dfdfdf', fontSize: 20, fontWeight: 500 }}/>
  273. <ListItemText primary={number} primaryTypographyProps={{ color: '#ffffff', fontSize: 15, fontWeight: 500,textAlign:"center" }}/>
  274. <ListItemText secondary={callStatus+'...'} secondaryTypographyProps={{ color: "#dfdfdf",textAlign: "center" }} />
  275. </div>}
  276. <video className={true ? classes.shareScreenActive : classes.shareScreenDisabled} ref={userVideoRef} playsInline autoPlay />
  277. <div className={classes.bottomWrapper}>
  278. {!true&&<div className={classes.bottomItem}>
  279. <Avatar className={classes.bottomIcon}
  280. sx={{backgroundColor: '#ffffff',color: 'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  281. <ScreenShareIcon fontSize="medium" />
  282. </Avatar>
  283. <Typography variant="h6" className={classes.titleIconBottom}>Screencast</Typography>
  284. </div>}
  285. <div className={classes.bottomItem}>
  286. <Avatar className={classes.bottomIcon}
  287. sx={{backgroundColor: true?'rgb(88, 88, 88)':'#ffffff',color: true?'#ffffff':'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  288. {true?<VideocamOffIcon fontSize="medium" />:<VideocamIcon fontSize="medium" />}
  289. </Avatar>
  290. <Typography variant="h6" className={classes.titleIconBottom}>{true?'Stop Video':'Start Video'}</Typography>
  291. </div>
  292. <div className={classes.bottomItem}>
  293. <Avatar className={classes.bottomIconEndAccept} onClick={handleLeaveCall}
  294. sx={{backgroundColor: '#f02a2a',color: '#ffffff', width: 44, height: 44,zIndex:0}}>
  295. <CallEndIcon fontSize="medium" />
  296. </Avatar>
  297. <Typography variant="h6" className={classes.titleIconBottom}>
  298. {callStatus === 'is calling you' ? 'Decline' : 'End Call'}
  299. </Typography>
  300. </div>
  301. {callStatus === 'is calling you' &&
  302. <div className={classes.bottomItem} style={{position:"relative"}}>
  303. <div className={classes.ringPulsate}></div>
  304. <Avatar className={classes.bottomIconEndAccept} onClick={handleAnswerCall}
  305. sx={{ backgroundColor: '#21f519', color: '#ffffff', width: 44, height: 44, zIndex: 0 }}>
  306. <PhoneIcon fontSize="medium" />
  307. </Avatar>
  308. <Typography variant="h6" className={classes.titleIconBottom}>
  309. Accept
  310. </Typography>
  311. </div>}
  312. <div className={classes.bottomItem}>
  313. <Avatar className={classes.bottomIcon}
  314. sx={{backgroundColor: true?'rgb(88, 88, 88)':'#ffffff',color: true?'#ffffff':'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  315. {true?<MicIcon fontSize="medium" />:<MicOffIcon fontSize="medium" />}
  316. </Avatar>
  317. <Typography variant="h6" className={classes.titleIconBottom}>{true?'Mute':'Unmute'}</Typography>
  318. </div>
  319. </div>
  320. </div>
  321. </div>
  322. )
  323. }
  324. export default CallBar