index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 } from '@material-ui/core'
  6. import { useDispatch,useSelector } from 'react-redux';
  7. import { useState, useRef, useEffect } from 'react';
  8. import MenuList from './MenuList'
  9. import DeleteModal from './DeleteModal';
  10. import CallModal from './CallModal';
  11. import { getAuthorizationState } from '../../../../../redux/authorization/selector';
  12. import { actionRightIsOpen } from '../../../../../redux/control/action'
  13. const Peer = require('simple-peer')
  14. const useStyles = makeStyles({
  15. container: {
  16. marginLeft: 20,
  17. display: 'flex',
  18. alignContent: 'center',
  19. alignItems:'center'
  20. },
  21. })
  22. interface IButtons {
  23. setIsSomeSelected: React.Dispatch<React.SetStateAction<boolean>>,
  24. socket:any
  25. }
  26. const Buttons = ({setIsSomeSelected,socket}:IButtons) => {
  27. const classes = useStyles()
  28. const dispatch = useDispatch()
  29. const { name, lastName } = useSelector(getAuthorizationState)
  30. const shareRef = useRef<any>(null)
  31. const videoRef = useRef<any>(null)
  32. const [modalDelete, setModalDelete] = useState<boolean>(false)
  33. const [modalCall, setModalCall] = useState<boolean>(false)
  34. const [mySocketId, setMySocketId] = useState<string>('')
  35. const handleOpenSearch = (e: any) => {
  36. e.stopPropagation()
  37. dispatch(actionRightIsOpen('search'))
  38. }
  39. const handleStartCall = async (e: any) => {
  40. e.stopPropagation()
  41. setModalCall(true)
  42. const stream = await navigator.mediaDevices.getUserMedia({
  43. video: true,
  44. audio: true
  45. })
  46. const peer = new Peer({
  47. initiator: true,
  48. trickle: false,
  49. stream
  50. });
  51. peer.on("signal", (data:any) => {
  52. socket.emit("call", {
  53. userToCall: 'FUCeH4L8nAtPu7DlAAAN',
  54. signalData: data,
  55. from: socket.id,
  56. name: `${name} ${lastName}`,
  57. })
  58. });
  59. peer.on("stream", (companionStream:any) => {});
  60. socket.on("accepted", ({ signal }:any) => {
  61. peer.signal(signal);
  62. });
  63. // connectionRef.current = peer;
  64. }
  65. const handleAnswerCall = async () => {
  66. const stream = await navigator.mediaDevices.getUserMedia({
  67. video: true,
  68. audio: true
  69. })
  70. const peer = new Peer({
  71. initiator: false,
  72. trickle: false,
  73. stream,
  74. });
  75. peer.on("signal", (data:any) => {
  76. socket.emit("answer", { signal: data, to: 'caller' });
  77. });
  78. peer.on("stream", (companionStream:any) => {});
  79. peer.signal();
  80. // connectionRef.current = peer;
  81. };
  82. //leaveCall function to destroy the peer-to-peer connection
  83. const HandleLeaveCall = () => {
  84. // connectionRef.current.destroy();
  85. };
  86. return (
  87. <Stack className={classes.container} direction="row">
  88. <IconButton onClick={handleOpenSearch} aria-label="delete" size="medium">
  89. <SearchIcon fontSize='medium'/>
  90. </IconButton>
  91. <IconButton onClick={handleStartCall} aria-label="delete" size="medium">
  92. <PhoneIcon fontSize='medium'/>
  93. </IconButton>
  94. <MenuList setModalDelete={setModalDelete} setIsSomeSelected={setIsSomeSelected}/>
  95. {modalDelete && <DeleteModal setModalDelete={setModalDelete} />}
  96. {modalCall && <CallModal setModalCall={setModalCall} shareRef={shareRef} videoRef={videoRef}/>}
  97. </Stack>
  98. );
  99. }
  100. export default Buttons