Playerbar.js 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { useEffect, useState } from "react"
  2. import { connect } from "react-redux"
  3. import * as action from "../actions"
  4. const Timestamp = ({time}) =>
  5. <small style={{margin: '0 10px'}}>
  6. {Math.floor((time / 60) % 60) < 10? `0` + Math.floor((time / 60) % 60) : Math.floor((time / 60) % 60)} :
  7. {Math.floor(time % 60) < 10? `0`+ Math.floor(time % 60) : Math.floor(time % 60)}
  8. </small>
  9. const Playerbar = ({ player, playTrack, pauseTrack, switchTrack, setTrackVolume, setCurrentTime }) => {
  10. let [_player, setPlayer] = useState()
  11. useEffect(() => {
  12. setPlayer(player)
  13. }, [player, _player])
  14. return (
  15. <>
  16. {_player?.track ?
  17. <footer style={{display: `${_player?.track ? 'block' : 'none'}`, width: '95%', margin:'0 auto'}}>
  18. <input
  19. style={{width:'100%'}}
  20. type="range" min="0" max={_player?.duration} step="any"
  21. value={_player?.currentTime}
  22. onChange={(e) => setCurrentTime(e.target.value)}
  23. />
  24. <div style={{display:"flex", justifyContent:"space-between", width:'95%', margin:'0 auto'}}>
  25. {/* <small style={{margin: '0 10px'}}>
  26. {Math.floor((_player?.currentTime / 60) % 60) < 10?
  27. `0` + Math.floor((_player?.currentTime / 60) % 60) : Math.floor((_player?.currentTime / 60) % 60)} :
  28. {Math.floor(_player?.currentTime % 60) < 10?
  29. `0`+ Math.floor(_player?.currentTime % 60) : Math.floor(_player?.currentTime % 60)}
  30. </small> */}
  31. <Timestamp time={_player?.currentTime} />
  32. {_player ? (_player.track?.id3?.artist && _player.track?.id3?.title ?
  33. <strong>{_player.track?.id3?.artist} - {_player.track?.id3?.title}</strong> :
  34. <strong>{_player.track?.originalFileName}</strong>) : ''}
  35. {/* <small style={{margin: '0 10px'}}>
  36. {Math.floor((_player?.duration / 60) % 60) < 10?
  37. `0` + Math.floor((_player?.duration / 60) % 60) : Math.floor((_player?.duration / 60) % 60)} :
  38. {Math.floor(_player?.duration % 60) < 10?
  39. `0`+ Math.floor(_player?.duration % 60) : Math.floor(_player?.duration % 60)}
  40. </small> */}
  41. <Timestamp time={_player?.duration} />
  42. </div>
  43. <div style={{marginTop:'0.5%'}}>
  44. <button
  45. style={{ fontSize: '2vh' }}
  46. onClick={() => switchTrack(false, _player?.playlistIndex, _player?.playlist)}
  47. >
  48. {_player?.playlistIndex === 0 ? `\u23F4` : `\u23EE`}
  49. </button>
  50. {_player?.isPlaying ?
  51. <button style={{ fontSize: '2vh' }} onClick={() => pauseTrack()}>{`\u23F8`}</button> :
  52. <button style={{ fontSize: '2vh' }} onClick={() => playTrack()}>{`\u23F5`}</button>
  53. }
  54. <button
  55. style={{ fontSize: '2vh' }}
  56. onClick={() => switchTrack(true, _player?.playlistIndex, _player?.playlist)}
  57. >
  58. {_player?.playlistIndex === (_player?.playlist?.constructor.name === 'Array'? _player?.playlist?.length - 1 : _player?.playlist?.tracks.length - 1) ?
  59. `\u23F5` : `\u23ED`}
  60. </button>
  61. <input style={{marginLeft: '1%'}} type="range" min="0" max="1" step="any" onChange={(e) => setTrackVolume(e.target.value)} />
  62. </div>
  63. <small>{_player?.playlistIndex}</small>
  64. </footer> : <div style={{width:'inherit', height:'inherit', padding:'2em'}}>C'mon, Push the play button on some track :)</div>}
  65. </>
  66. )
  67. }
  68. export const PlayerbarConnect = connect(
  69. state => ({ player: state.player || {} }),
  70. {
  71. playTrack: action.playTrack,
  72. pauseTrack: action.pauseTrack,
  73. switchTrack: action.switchTrack,
  74. setTrackVolume: action.setTrackVolume,
  75. setCurrentTime: action.setNewTrackCurrentTime
  76. }
  77. )(Playerbar)