import { useEffect, useRef, useState } from "react"; import { Avatar, Divider, Space, SpinLoading, Toast } from "antd-mobile"; import { VoiceIcon } from "@workspace/shared"; import { Message } from "../../../types"; import "./index.less"; import { Refresh } from "@icon-park/react"; interface DefinedProps { data: Message[]; isRecording: boolean; onRefresh: (formData: FormData, messageId: number) => void; } function Index(props: DefinedProps) { const { data, isRecording } = props; const audioRefs = useRef<{ [key: string]: HTMLAudioElement }>({}); const [isPlaying, setIsPlating] = useState(false); const [currentPlayingId, setCurrentPlayingId] = useState(); useEffect(() => { if (isRecording && currentPlayingId) { audioRefs.current[currentPlayingId].pause(); audioRefs.current[currentPlayingId].currentTime = 0; setCurrentPlayingId(undefined); } }, [isRecording, currentPlayingId]); const playAudio = (id: number, audioUrl: string) => { if (isRecording) { Toast.show("录音中,无法播放音频"); return; } if (currentPlayingId && audioRefs.current[currentPlayingId]) { audioRefs.current[currentPlayingId].pause(); audioRefs.current[currentPlayingId].currentTime = 0; } if (currentPlayingId !== id) { setCurrentPlayingId(id); audioRefs.current[id] = new Audio(audioUrl); audioRefs.current[id].play(); audioRefs.current[id].onended = () => { setCurrentPlayingId(undefined); }; } else { audioRefs.current[id].pause(); audioRefs.current[id].currentTime = 0; setCurrentPlayingId(undefined); } }; const renderAvatar = (item: Message) => { return ; }; const refreshMessage = async (messageId: number, e: React.MouseEvent) => { e.stopPropagation(); const formData = new FormData(); formData.append("msgId", messageId.toString()); props.onRefresh(formData, messageId); }; const renderTranslateResult = (item: Message) => { if (item.isTranslating) { return (
翻译中...
); } else { if (item.transStatus === 1) { return item.transResult?.length ? ( {item.transResult} ) : ( 未知语句 refreshMessage(item.id, e)} size="12" fill="#333" /> ); } else { return ( { e.stopPropagation(); }} > 翻译失败,请重试 refreshMessage(item.id, e)} size="12" fill="#333" /> ); } } }; return (
{data.map((item, index) => (
{renderAvatar(item)}
{item.isTranslating && !item.isRefresh ? "" : item.petName ?? "未知宠物"} {item.createTime}
playAudio(item.id, item.contentText)}>
{item.contentDuration}''
{renderTranslateResult(item)}
))}
); } export default Index;