Roster.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Peer, PeerControls, PeerList } from '@andyet/simplewebrtc';
  2. import VisibilityIcon from 'material-icons-svg/components/baseline/Visibility';
  3. import VisibilityOffIcon from 'material-icons-svg/components/baseline/VisibilityOff';
  4. import VolumeOffIcon from 'material-icons-svg/components/baseline/VolumeOff';
  5. import VolumeUpIcon from 'material-icons-svg/components/baseline/VolumeUp';
  6. import React, { useContext } from 'react';
  7. import styled from 'styled-components';
  8. import HiddenPeers from '../contexts/HiddenPeers';
  9. import Placeholders from '../contexts/Placeholders';
  10. import { TalkyButton } from '../styles/button';
  11. const Button = styled(TalkyButton)({
  12. marginRight: '5px'
  13. });
  14. const Container = styled.ul({
  15. listStyle: 'none',
  16. '& li': {
  17. marginBottom: '10px'
  18. }
  19. });
  20. interface PeerListItemProps {
  21. peer: Peer;
  22. }
  23. // PeerListItem renders the displayName and mute controls for a peer.
  24. const PeerListItem: React.SFC<PeerListItemProps> = ({ peer }) => {
  25. const { hiddenPeers, togglePeer } = useContext(HiddenPeers);
  26. const isHidden = hiddenPeers.includes(peer.id);
  27. return (
  28. <li>
  29. <PeerControls
  30. peer={peer}
  31. render={({ isMuted, mute, unmute }) => (
  32. <Button onClick={() => (isMuted ? unmute() : mute())}>
  33. {isMuted ? (
  34. <VolumeOffIcon fill="#505658" />
  35. ) : (
  36. <VolumeUpIcon fill="#505658" />
  37. )}
  38. </Button>
  39. )}
  40. />
  41. <Button onClick={() => togglePeer(peer.id)}>
  42. {isHidden ? (
  43. <VisibilityOffIcon fill="#505658" />
  44. ) : (
  45. <VisibilityIcon fill="#505658" />
  46. )}
  47. </Button>
  48. {peer.displayName || 'Anonymous'}
  49. </li>
  50. );
  51. };
  52. interface Props {
  53. roomAddress: string;
  54. }
  55. const Roster: React.SFC<Props> = ({ roomAddress }) => (
  56. <PeerList
  57. room={roomAddress}
  58. render={({ peers }) => {
  59. return peers.length > 0 ? (
  60. <Container>
  61. {peers.map(peer => (
  62. <PeerListItem key={peer.id} peer={peer} />
  63. ))}
  64. </Container>
  65. ) : (
  66. <Placeholders.Consumer>
  67. {({ emptyRosterPlaceholder }) => (
  68. <div
  69. ref={node => {
  70. if (
  71. node &&
  72. emptyRosterPlaceholder &&
  73. node.childElementCount === 0
  74. ) {
  75. const el = emptyRosterPlaceholder();
  76. if (el) {
  77. node.appendChild(el);
  78. }
  79. }
  80. }}
  81. />
  82. )}
  83. </Placeholders.Consumer>
  84. );
  85. }}
  86. />
  87. );
  88. export default Roster;