index.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { makeStyles, Typography } from '@material-ui/core'
  2. import { useState,useEffect } from 'react';
  3. import { useSelector } from 'react-redux';
  4. import ListItemText from '@mui/material/ListItemText';
  5. import ListItemAvatar from '@mui/material/ListItemAvatar';
  6. import Avatar from '@mui/material/Avatar';
  7. import MinimizeIcon from '@mui/icons-material/Minimize';
  8. import CropLandscapeIcon from '@mui/icons-material/CropLandscape';
  9. import CloseIcon from '@mui/icons-material/Close';
  10. import ScreenShareIcon from '@mui/icons-material/ScreenShare';
  11. import StopScreenShareIcon from '@mui/icons-material/StopScreenShare';
  12. import VideocamIcon from '@mui/icons-material/Videocam';
  13. import VideocamOffIcon from '@mui/icons-material/VideocamOff';
  14. import MicIcon from '@mui/icons-material/Mic';
  15. import MicOffIcon from '@mui/icons-material/MicOff';
  16. import CallEndIcon from '@mui/icons-material/CallEnd';
  17. import Moveable from "react-moveable";
  18. import { OnDrag } from "react-moveable";
  19. import { getChat } from '../../../../../../redux/chat/selector';
  20. import { prodAwsS3,firstLetter,slicedWord } from '../../../../../../helpers'
  21. const useStyles = makeStyles({
  22. overlay: {
  23. position: 'fixed',
  24. top: 0,
  25. left: 0,
  26. width: '100vw',
  27. height: '100vh',
  28. zIndex: 100,
  29. overflow: 'hidden',
  30. display: 'flex',
  31. justifyContent: 'center',
  32. alignItems: 'center',
  33. alignContent: "center",
  34. backgroundColor: 'rgba(104, 105, 104, 0.6)',
  35. },
  36. shareScreenActive: {
  37. width: '90%',
  38. borderRadius: 7,
  39. margin: 'auto',
  40. border:'solid 2px #0084ff',
  41. },
  42. shareScreenDisabled: {
  43. width: 0,
  44. height:0,
  45. },
  46. modalCall: {
  47. background: 'rgb(36, 36, 36)',
  48. display: 'flex',
  49. flexDirection: 'column',
  50. justifyContent: 'start',
  51. alignItems: 'center',
  52. justifyItems:"center",
  53. width: '34vw',
  54. height:'50vh',
  55. borderRadius: 7,
  56. },
  57. rightIcons: {
  58. display: 'flex',
  59. justifyContent: 'end',
  60. alignContent: 'center',
  61. alignItems: 'center',
  62. width:'100%'
  63. },
  64. rightIconWrapper: {
  65. color: '#ffffff',
  66. cursor: 'pointer',
  67. padding:'3px 10px 3px 10px',
  68. '&:hover': {
  69. backgroundColor:'rgb(80, 80, 80)'
  70. }
  71. },
  72. rightIconWrapperClose: {
  73. color: '#ffffff',
  74. cursor: 'pointer',
  75. padding:'3px 10px 3px 10px',
  76. borderTopRightRadius:7,
  77. '&:hover': {
  78. backgroundColor:'#f02a2a'
  79. }
  80. },
  81. statusCall: {
  82. color: "#dfdfdf",
  83. animation: 'ripple 4s infinite ease-in-out',
  84. },
  85. animatedDots: {
  86. fontWeight: 'bold',
  87. display:'inline-block',
  88. fontFamily: 'monospace',
  89. clipPath: 'inset(0 3ch 0 0)',
  90. animation: `$run 2s steps(5) infinite`,
  91. },
  92. bottomWrapper: {
  93. display: 'flex',
  94. justifyContent: 'center',
  95. padding: 5
  96. },
  97. bottomItem: {
  98. display: 'flex',
  99. flexDirection: 'column',
  100. justifyContent: 'center',
  101. alignContent: 'center',
  102. alignItems: 'center',
  103. cursor:'pointer',
  104. width: 80,
  105. },
  106. bottomIcon: {
  107. cursor:'pointer',
  108. },
  109. titleIconBottom: {
  110. color: '#ffffff',
  111. fontSize: 13,
  112. paddingTop:7
  113. },
  114. '@keyframes run': {
  115. to: {
  116. clipPath: 'inset(0 -1ch 0 0)'
  117. },
  118. },
  119. })
  120. interface ICallModal {
  121. setModalCall:any,
  122. shareRef: any,
  123. videoRef: any,
  124. }
  125. const CallModal = ({setModalCall,shareRef,videoRef}:ICallModal) => {
  126. const classes = useStyles()
  127. const { name, lastName, avatarUrl, color } = useSelector(getChat)
  128. const [audio, setAudio] = useState<boolean>(true)
  129. const [video, setVideo] = useState<any>(false)
  130. const [share, setShare] = useState<any>(null)
  131. const handleShareScreen = async () => {
  132. const displayMediaStreamConstraints = {
  133. video: true,
  134. };
  135. const navigator:any = window.navigator
  136. const stream = await navigator.mediaDevices.getDisplayMedia(displayMediaStreamConstraints);
  137. shareRef.current.srcObject = stream;
  138. setShare(true)
  139. stream.getVideoTracks()[0].onended = () => setShare(false)
  140. };
  141. const handleAudio = () => setAudio(prevState => !prevState)
  142. const handleVideo = async () => {
  143. const displayMediaStreamConstraints = {
  144. audio: false,
  145. video: {
  146. width: {
  147. min: 1280,
  148. ideal: 1920,
  149. max: 2560,
  150. },
  151. height: {
  152. min: 720,
  153. ideal: 1080,
  154. max: 1440
  155. },
  156. facingMode: 'user'
  157. },
  158. };
  159. const navigator:any = window.navigator
  160. const stream = await navigator.mediaDevices.getUserMedia(displayMediaStreamConstraints)
  161. if (!video) {
  162. setVideo(true)
  163. videoRef.current.srcObject = stream;
  164. } else {
  165. videoRef.current.srcObject = null;
  166. stream.getVideoTracks()[0].stop()
  167. setVideo(false)
  168. }
  169. }
  170. const handleCloseCallModal = () => setModalCall(false)
  171. // requesting, waiting ,ringing, hanging up,line busy
  172. // useEffect(() => {
  173. // socket.on('connect', () => {
  174. // setIsConnected(true);
  175. // });
  176. // socket.on('disconnect', () => {
  177. // setIsConnected(false);
  178. // });
  179. // socket.on('pong', () => {
  180. // setLastPong(new Date().toISOString());
  181. // });
  182. // return () => {
  183. // socket.off('connect');
  184. // socket.off('disconnect');
  185. // socket.off('pong');
  186. // };
  187. // }, []);
  188. return (
  189. <div className={classes.overlay} >
  190. <video ref={videoRef} style={{width: video ? 250 : 0, height: video ? 'auto' : 0, cursor: 'pointer',
  191. position: 'absolute', top: 0, left: 0, zIndex: 100}} playsInline autoPlay muted/>
  192. <Moveable
  193. target={videoRef.current}
  194. draggable={true}
  195. throttleDrag={0}
  196. hideDefaultLines={true}
  197. renderDirections={[]}
  198. rotationPosition="none"
  199. origin={false}
  200. onDrag={({ target, transform }: OnDrag) =>
  201. target!.style.transform = transform }
  202. />
  203. <div className={classes.modalCall}>
  204. <div className={classes.rightIcons} style={{marginBottom: share?0:40,}}>
  205. <div className={classes.rightIconWrapper}>
  206. <MinimizeIcon fontSize='small' />
  207. </div>
  208. <div className={classes.rightIconWrapper}>
  209. <CropLandscapeIcon fontSize='small' />
  210. </div>
  211. <div className={classes.rightIconWrapperClose} onClick={handleCloseCallModal}>
  212. <CloseIcon fontSize='small' />
  213. </div>
  214. </div>
  215. {!share&&<ListItemAvatar style={{marginBottom:15}}>
  216. <Avatar alt={name} src={avatarUrl?`${prodAwsS3}/${avatarUrl}`:undefined}
  217. sx={{ background: color, width: 120, height: 120, marginRight: 2, fontSize:30,zIndex:0}}>
  218. {`${firstLetter(name)}${firstLetter(lastName)}`}
  219. </Avatar>
  220. </ListItemAvatar>}
  221. {!share&&<div style={{marginBottom:'auto'}}>
  222. <ListItemText primary={`${firstLetter(name)}${slicedWord(name, 15, 1)}
  223. ${firstLetter(lastName)}${slicedWord(lastName, 15, 1)}`}
  224. primaryTypographyProps={{ color: '#ffffff', fontSize: 20, fontWeight: 500 }} />
  225. <ListItemText secondary={<span className={classes.statusCall}>
  226. ringing<span className={classes.animatedDots}>
  227. ...</span>
  228. </span>} secondaryTypographyProps={{ textAlign: "center" }} />
  229. </div>}
  230. <video className={share?classes.shareScreenActive:classes.shareScreenDisabled} ref={shareRef} playsInline muted autoPlay/>
  231. <div className={classes.bottomWrapper}>
  232. {!share&&<div className={classes.bottomItem}>
  233. <Avatar className={classes.bottomIcon} onClick={handleShareScreen}
  234. sx={{backgroundColor: '#ffffff',color: 'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  235. <ScreenShareIcon fontSize="medium" />
  236. </Avatar>
  237. <Typography variant="h6" className={classes.titleIconBottom}>Screencast</Typography>
  238. </div>}
  239. <div className={classes.bottomItem}>
  240. <Avatar className={classes.bottomIcon} onClick={handleVideo}
  241. sx={{backgroundColor: video?'rgb(88, 88, 88)':'#ffffff',color: video?'#ffffff':'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  242. {video?<VideocamOffIcon fontSize="medium" />:<VideocamIcon fontSize="medium" />}
  243. </Avatar>
  244. <Typography variant="h6" className={classes.titleIconBottom}>{video?'Stop Video':'Start Video'}</Typography>
  245. </div>
  246. <div className={classes.bottomItem}>
  247. <Avatar className={classes.bottomIcon}
  248. sx={{backgroundColor: '#f02a2a',color: '#ffffff', width: 44, height: 44,zIndex:0}}>
  249. <CallEndIcon fontSize="medium" />
  250. </Avatar>
  251. <Typography variant="h6" className={classes.titleIconBottom}>End Call</Typography>
  252. </div>
  253. <div className={classes.bottomItem}>
  254. <Avatar className={classes.bottomIcon} onClick={handleAudio}
  255. sx={{backgroundColor: audio?'rgb(88, 88, 88)':'#ffffff',color: audio?'#ffffff':'rgb(36, 36, 36)', width: 44, height: 44,zIndex:0}}>
  256. {audio?<MicIcon fontSize="medium" />:<MicOffIcon fontSize="medium" />}
  257. </Avatar>
  258. <Typography variant="h6" className={classes.titleIconBottom}>{audio?'Mute':'Unmute'}</Typography>
  259. </div>
  260. </div>
  261. </div>
  262. </div>
  263. )
  264. }
  265. export default CallModal