Skip to content

Commit

Permalink
Merge pull request #63 from contacto-io/CNTO-7149/recording-in-vc
Browse files Browse the repository at this point in the history
Added the option to show Audio player speeds upfront
  • Loading branch information
rishabhPlivo authored Oct 5, 2023
2 parents 500d686 + 9458058 commit 5daffcd
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"registry": "https://npm.pkg.github.com"
},
"repository": "git://github.com/contacto-io/contacto-console",
"version": "0.5.37",
"version": "0.5.40",
"main": "build/index.js",
"module": "build/index.es.js",
"files": [
Expand Down
41 changes: 11 additions & 30 deletions src/components/AudioPlayer/components/PlaybackSpeed.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
/* eslint-disable no-unused-vars */
import React, { useEffect, useState } from 'react'
import { Popover } from 'antd'
import { Button } from '../../Button/index'
import { RenderButtons } from '../helpers/utils'

const speeds = [0.5, 1, 1.5, 2]

const speeds = [0.8, 1, 1.2, 1.5, 1.7, 2, 2.5]
export default function PlaybackSpeed({ waveSurfer }) {
const [speed, setSpeed] = useState(waveSurfer?.getPlaybackRate())

const handleSpeedChange = (speed) => {
setSpeed(speed)
waveSurfer?.setPlaybackRate(speed)
}

useEffect(() => {
setSpeed(waveSurfer?.getPlaybackRate())
}, [waveSurfer])

return (
<Popover
overlayClassName="contacto-player-speed"
title="Playback Speed"
content={() => (
<div>
{speeds.map((speed) => (
<Button
key={speed}
className="contacto-player-speed"
type={speed === waveSurfer?.getPlaybackRate() ? 'secondary' : 'table-action-link'}
onClick={() => handleSpeedChange(speed)}
>
{speed}x
</Button>
))}
</div>
)}
>
<Button className="contacto-player-speed-trigger" type="secondary">
{speed}x
</Button>
</Popover>
<RenderButtons
speeds={speeds}
waveSurfer={waveSurfer}
setSpeed={setSpeed}
parentClassName="contacto-player-speed"
buttonClassName="contacto-player-speed-buttons"
/>
)
}
33 changes: 33 additions & 0 deletions src/components/AudioPlayer/components/PlaybackSpeedPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useEffect, useState } from 'react'
import { Popover } from 'antd'
import { Button } from '../../Button/index'
import { RenderButtons } from '../helpers/utils'

const speeds = [0.8, 1, 1.2, 1.5, 1.7, 2, 2.5]
export default function PlaybackSpeedPopup({ waveSurfer }) {
const [speed, setSpeed] = useState(waveSurfer?.getPlaybackRate())

useEffect(() => {
setSpeed(waveSurfer?.getPlaybackRate())
}, [waveSurfer])

return (
<Popover
overlayClassName="contacto-player-speed-popup"
title="Playback Speed"
content={() => (
<RenderButtons
speeds={speeds}
waveSurfer={waveSurfer}
setSpeed={setSpeed}
parentClassName="contacto-player-speed-popup"
buttonClassName="contacto-player-speed-popup"
/>
)}
>
<Button className="contacto-player-speed-trigger" type="secondary">
{speed}x
</Button>
</Popover>
)
}
35 changes: 35 additions & 0 deletions src/components/AudioPlayer/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import React from 'react'
import { Button } from '../../Button/index'

export const getDisplayTime = (seconds) => {
const int = +seconds / 60
let minutes = parseInt(int)
Expand All @@ -10,3 +13,35 @@ export const getDisplayTime = (seconds) => {
export const generateId = (prefix) => {
return `${prefix}${Math.random().toString(36).slice(2)}`
}

export const handleSpeedChange = (speed, waveSurfer, setSpeed) => {
setSpeed(speed)
waveSurfer?.setPlaybackRate(speed)
}

export const getType = (speed, waveSurfer) => {
return speed === waveSurfer?.getPlaybackRate() ? 'secondary' : 'table-action-link'
}

export const RenderButtons = ({
speeds,
waveSurfer,
setSpeed,
parentClassName,
buttonClassName,
}) => {
return (
<div className={parentClassName}>
{speeds.map((speed) => (
<Button
key={speed}
className={buttonClassName}
type={getType(speed, waveSurfer)}
onClick={() => handleSpeedChange(speed, waveSurfer, setSpeed)}
>
{speed}x
</Button>
))}
</div>
)
}
9 changes: 7 additions & 2 deletions src/components/AudioPlayer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { forwardRef, useState, useLayoutEffect } from 'react'
import PlayPauseIcon from './components/PlayPauseIcon'
import useWaveSurfer from './helpers/useWaveSurfer'
import PlaybackSpeed from './components/PlaybackSpeed'
import PlaybackSpeedPopup from './components/PlaybackSpeedPopup'
import { Button } from '../Button/index'
import { Icon } from '../Icon/index'
import { getDisplayTime } from './helpers/utils'
import './styles.scss'

// TODO: @Ritik Add Error Boundaries
const AudioPlayer = forwardRef((props, ref) => {
const { className, url } = props
const { className, url, showSpeedInPopup = true } = props
const { playerConfig, durationConfig, waveSurferRef } = useWaveSurfer(url)

const { isPlaying, loading } = playerConfig
Expand Down Expand Up @@ -52,7 +53,11 @@ const AudioPlayer = forwardRef((props, ref) => {
<div className="audio-controls-time right">
<Text type="caption">{getDisplayTime(totalDuration)}</Text>
</div>
<PlaybackSpeed waveSurfer={waveSurferInstance} />
{showSpeedInPopup ? (
<PlaybackSpeedPopup waveSurfer={waveSurferInstance} />
) : (
<PlaybackSpeed waveSurfer={waveSurferInstance} />
)}
</div>
</div>
)
Expand Down
6 changes: 6 additions & 0 deletions src/components/AudioPlayer/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ export const Default = Template.bind({})
Default.args = {
url: 'https://wavesurfer-js.org/wavesurfer-code/examples/audio/mono.mp3',
}

export const WithSpeed = Template.bind({})
WithSpeed.args = {
url: 'https://wavesurfer-js.org/wavesurfer-code/examples/audio/mono.mp3',
showSpeedInPopup: false,
}
18 changes: 15 additions & 3 deletions src/components/AudioPlayer/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

&.contacto-button--table-action-link {
background-color: var(--background-highlight-color);
background-color: var(--background-highlight-color) !important;

&:hover {
background-color: var(--background-highlight-color) !important;
Expand Down Expand Up @@ -90,7 +90,7 @@
}
}

.contacto-player-speed {
.contacto-player-speed-popup {
padding-top: 8px;

.ant-popover-arrow {
Expand Down Expand Up @@ -119,10 +119,22 @@
align-items: center;
gap: 8px;

.contacto-player-speed {
.contacto-player-speed-popup {
@include playback-speed-button;
}
}
}
}
}

.contacto-player-speed {
display: flex;
justify-content: center;
align-items: center;
gap: 8px;
margin-left: 24px;

.contacto-player-speed-buttons {
@include playback-speed-button;
}
}

0 comments on commit 5daffcd

Please sign in to comment.