index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 [name, setName] = useState<string>('')
  162. const [lastName, setLastName] = useState<string>('')
  163. const [avatarUrl, setAvatarUrl] = useState<string>('')
  164. const [color, setColor] = useState<string>('')
  165. const [number,setNumber] = useState<string>('')
  166. const handleLeaveCall = () => {
  167. connectionRef.current.destroy();
  168. setCallStatus('')
  169. };
  170. // requesting, waiting ,ringing, hanging up,line busy
  171. const handleStartCall = useCallback(async () => {
  172. const stream = await navigator.mediaDevices.getUserMedia({
  173. video: true,
  174. audio: true
  175. })
  176. const peer = new Peer({
  177. initiator: true,
  178. trickle: false,
  179. stream
  180. });
  181. peer.on("signal", (data:any) => {
  182. socket.emit("callTo", {
  183. to: chat.socketId,
  184. signalData: data,
  185. from: mySocket,
  186. userId: _id,
  187. companionId:chat.companionId
  188. })
  189. setCallStatus('ringing')
  190. });
  191. // peer.on("stream", (stream: any) => {
  192. // console.log(stream,'user stream')
  193. // });
  194. // socket.on("acceptedCall", ({ signal }: any) => {
  195. // // peer.signal(signal)
  196. // setCallStatus('accepted')
  197. // console.log(signal,'signal accepted from companion')
  198. // });
  199. connectionRef.current = peer;
  200. },[chat.socketId,chat.companionId,_id,socket,setCallStatus,mySocket])
  201. const handleAnswerCall = useCallback(async () => {
  202. const stream = await navigator.mediaDevices.getUserMedia({
  203. video: true,
  204. audio: true
  205. })
  206. const peer = new Peer({
  207. initiator: false,
  208. trickle: false,
  209. stream,
  210. });
  211. peer.on("signal", (data: any) => {
  212. socket.emit("answerCall", { signal: data, to: companionSocket });
  213. console.log(data,'sent the signal on answer')
  214. });
  215. // peer.on("stream", (stream: any) => {
  216. // console.log(stream,'user stream')
  217. // });
  218. // peer.signal();
  219. connectionRef.current = peer;
  220. },[socket,companionSocket])
  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. console.log(data,'incomeCall')
  235. })
  236. socket.on('acceptedCall', (data: any) => {
  237. console.log(data,'acceptedCall')
  238. })
  239. },[socket,setCallStatus,setName,setLastName,setCompanionSocket])
  240. useEffect(() => {
  241. if(callStatus === 'requesting') handleStartCall()
  242. }, [callStatus, handleStartCall])
  243. useEffect(() => {
  244. if (callStatus === '') {
  245. setName(chat.name)
  246. setLastName(chat.lastName)
  247. setAvatarUrl(chat.avatarUrl)
  248. setColor(chat.color)
  249. setNumber(chat.number)
  250. }
  251. }, [callStatus,chat])
  252. return (
  253. <div className={classes.container} style={{top: callStatus?0:'-100%'}}>
  254. <div className={classes.modalCall}>
  255. <div className={classes.rightIcons} style={{marginBottom: true?0:40,}}>
  256. <div className={classes.rightIconWrapper}>
  257. <MinimizeIcon fontSize='small' />
  258. </div>
  259. <div className={classes.rightIconWrapper}>
  260. <CropLandscapeIcon fontSize='small' />
  261. </div>
  262. <div className={classes.rightIconWrapperClose} onClick={handleLeaveCall}>
  263. <CloseIcon fontSize='small' />
  264. </div>
  265. </div>
  266. {<ListItemAvatar style={{marginBottom:5}}>
  267. <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
  268. sx={{ background: color, width: 120, height: 120, marginRight: 2, fontSize:30,zIndex:0}}>
  269. {`${firstLetter(name)}${firstLetter(lastName)}`}
  270. </Avatar>
  271. </ListItemAvatar>}
  272. {<div style={{marginBottom:'auto'}}>
  273. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  274. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  275. primaryTypographyProps={{ color: '#dfdfdf', fontSize: 20, fontWeight: 500 }}/>
  276. <ListItemText primary={number} primaryTypographyProps={{ color: '#ffffff', fontSize: 15, fontWeight: 500,textAlign:"center" }}/>
  277. <ListItemText secondary={callStatus+'...'} secondaryTypographyProps={{ color: "#dfdfdf",textAlign: "center" }} />
  278. </div>}
  279. <video className={true ? classes.shareScreenActive : classes.shareScreenDisabled} ref={userVideoRef} playsInline autoPlay />
  280. <div className={classes.bottomWrapper}>
  281. {!true&&<div className={classes.bottomItem}>
  282. <Avatar className={classes.bottomIcon}
  283. sx={{backgroundColor: '#ffffff',color: 'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  284. <ScreenShareIcon fontSize="medium" />
  285. </Avatar>
  286. <Typography variant="h6" className={classes.titleIconBottom}>Screencast</Typography>
  287. </div>}
  288. <div className={classes.bottomItem}>
  289. <Avatar className={classes.bottomIcon}
  290. sx={{backgroundColor: true?'rgb(88, 88, 88)':'#ffffff',color: true?'#ffffff':'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  291. {true?<VideocamOffIcon fontSize="medium" />:<VideocamIcon fontSize="medium" />}
  292. </Avatar>
  293. <Typography variant="h6" className={classes.titleIconBottom}>{true?'Stop Video':'Start Video'}</Typography>
  294. </div>
  295. <div className={classes.bottomItem}>
  296. <Avatar className={classes.bottomIconEndAccept} onClick={handleLeaveCall}
  297. sx={{backgroundColor: '#f02a2a',color: '#ffffff', width: 44, height: 44,zIndex:0}}>
  298. <CallEndIcon fontSize="medium" />
  299. </Avatar>
  300. <Typography variant="h6" className={classes.titleIconBottom}>
  301. {callStatus === 'is calling you' ? 'Decline' : 'End Call'}
  302. </Typography>
  303. </div>
  304. {callStatus === 'is calling you' &&
  305. <div className={classes.bottomItem} style={{position:"relative"}}>
  306. <div className={classes.ringPulsate}></div>
  307. <Avatar className={classes.bottomIconEndAccept} onClick={handleAnswerCall}
  308. sx={{ backgroundColor: '#21f519', color: '#ffffff', width: 44, height: 44, zIndex: 0 }}>
  309. <PhoneIcon fontSize="medium" />
  310. </Avatar>
  311. <Typography variant="h6" className={classes.titleIconBottom}>
  312. Accept
  313. </Typography>
  314. </div>}
  315. <div className={classes.bottomItem}>
  316. <Avatar className={classes.bottomIcon}
  317. sx={{backgroundColor: true?'rgb(88, 88, 88)':'#ffffff',color: true?'#ffffff':'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  318. {true?<MicIcon fontSize="medium" />:<MicOffIcon fontSize="medium" />}
  319. </Avatar>
  320. <Typography variant="h6" className={classes.titleIconBottom}>{true?'Mute':'Unmute'}</Typography>
  321. </div>
  322. </div>
  323. </div>
  324. </div>
  325. )
  326. }
  327. export default CallBar