import React, { useEffect, useState } from 'react'; import { Box, Icon, PressBox, Text, useUIKitTheme } from '@sendbird/uikit-react-native-foundation'; import { LoadingSpinner, ProgressBar } from '@sendbird/uikit-react-native-foundation'; import { createStyleSheet } from '@sendbird/uikit-react-native-foundation'; import { SendbirdFileMessage, millsToMSS } from '@sendbird/uikit-utils'; import { ThreadParentMessageRendererProps } from './index'; export type VoiceFileMessageState = { status: 'preparing' | 'playing' | 'paused'; currentTime: number; duration: number; }; type Props = ThreadParentMessageRendererProps<{ durationMetaArrayKey?: string; onUnmount: () => void; initialCurrentTime?: number; }>; const ThreadParentMessageFileVoice = (props: Props) => { const { onLongPress, onToggleVoiceMessage, parentMessage, durationMetaArrayKey = 'KEY_VOICE_MESSAGE_DURATION', onUnmount, initialCurrentTime, } = props; const fileMessage: SendbirdFileMessage = parentMessage as SendbirdFileMessage; if (!fileMessage) return null; const { colors } = useUIKitTheme(); const [state, setState] = useState(() => { const meta = fileMessage.metaArrays.find((it) => it.key === durationMetaArrayKey); const value = meta?.value?.[0]; const initialDuration = value ? parseInt(value, 10) : 0; return { status: 'paused', currentTime: initialCurrentTime || 0, duration: initialDuration, }; }); useEffect(() => { return () => { onUnmount(); }; }, []); const uiColors = colors.ui.groupChannelMessage['incoming']; const remainingTime = state.duration - state.currentTime; return ( onToggleVoiceMessage?.(state, setState)} onLongPress={onLongPress}> {state.status === 'preparing' ? ( ) : ( )} {millsToMSS(state.currentTime === 0 ? state.duration : remainingTime)} } /> ); }; const styles = createStyleSheet({ container: { borderRadius: 16, overflow: 'hidden', maxWidth: 136, }, }); export default ThreadParentMessageFileVoice;