diff --git a/src/assets/icons/refresh.svg b/src/assets/icons/refresh.svg new file mode 100644 index 00000000..2e778119 --- /dev/null +++ b/src/assets/icons/refresh.svg @@ -0,0 +1,3 @@ + + + diff --git a/src/common/component/VoiceMessage.tsx b/src/common/component/VoiceMessage.tsx index b47f789d..26b26143 100644 --- a/src/common/component/VoiceMessage.tsx +++ b/src/common/component/VoiceMessage.tsx @@ -3,6 +3,7 @@ import dayjs from 'dayjs'; import clsx from 'clsx'; import WaveSurfer from 'wavesurfer.js'; import IconPause from '../../assets/icons/pause.svg'; +import IconRefresh from '../../assets/icons/refresh.svg'; import IconPlay from '../../assets/icons/play.circle.svg'; import BASE_URL from '../../app/config'; @@ -21,47 +22,54 @@ const VoiceMessage = ({ file_path }: { file_path: string }) => { // const [audio, setAudio] = useState(new Audio(`${BASE_URL}/resource/file?file_path=${encodeURIComponent( // file_path // )}`)); - useEffect(() => { + const initWave = (file_path: string) => { let wave: WaveSurfer | null = null; + const audioSrc = `${BASE_URL}/resource/file?file_path=${encodeURIComponent( + file_path + )}`; + wave = WaveSurfer.create({ + container: containerRef.current ?? "", + // maxCanvasWidth: 200, + height: 32, + waveColor: '#0BA5EC', + cursorColor: '#0BA5AA', + progressColor: "#0BA5AA", + hideScrollbar: true, + // mediaControls: true, + normalize: true, + }); + wave.load(audioSrc); + + wave.on('error', function (err) { + console.log("voice message error", err); + setStatus("error"); + if (waveRef.current) { + waveRef.current.destroy(); + } + }); + wave.on('play', function () { + setPlaying(true); + }); + wave.on('pause', function () { + setPlaying(false); + }); + wave.on('ready', function () { + setStatus("ready"); + console.log("readd"); + + if (waveRef.current) { + const dur = waveRef.current.getDuration(); + const num = Math.ceil(dur); + const durDisplay = dayjs.duration(num, 'seconds').format('mm:ss'); + setDuration(durDisplay); + } + }); + waveRef.current = wave; + }; + useEffect(() => { + if (containerRef.current && file_path) { - const audioSrc = `${BASE_URL}/resource/file?file_path=${encodeURIComponent( - file_path - )}`; - wave = WaveSurfer.create({ - container: containerRef.current, - // maxCanvasWidth: 200, - height: 32, - waveColor: '#0BA5EC', - cursorColor: '#0BA5AA', - progressColor: "#0BA5AA", - hideScrollbar: true, - // mediaControls: true, - normalize: true, - }); - wave.load(audioSrc); - - wave.on('error', function (err) { - console.log("voice message error", err); - setStatus("error"); - }); - wave.on('play', function () { - setPlaying(true); - }); - wave.on('pause', function () { - setPlaying(false); - }); - wave.on('ready', function () { - setStatus("ready"); - console.log("readd"); - - if (waveRef.current) { - const dur = waveRef.current.getDuration(); - const num = Math.ceil(dur); - const durDisplay = dayjs.duration(num, 'seconds').format('mm:ss'); - setDuration(durDisplay); - } - }); - waveRef.current = wave; + initWave(file_path); } return () => { if (waveRef.current) { @@ -78,16 +86,21 @@ const VoiceMessage = ({ file_path }: { file_path: string }) => { } } }; + const handleReload = () => { + initWave(file_path); + }; + const notReady = status !== "ready"; return ( -
- -
+
{status == "loading" && Loading voice message...} {status == "error" && Load voice message error}
- + {status !== "error" && } + {status === "error" && }
); };