@@ -2,17 +2,17 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
22import { FlatList , LayoutChangeEvent , StyleSheet , View } from 'react-native' ;
33
44import {
5- isAudioAttachment ,
65 isLocalAudioAttachment ,
76 isLocalFileAttachment ,
87 isLocalImageAttachment ,
98 isLocalVoiceRecordingAttachment ,
109 isVideoAttachment ,
11- isVoiceRecordingAttachment ,
1210 LocalAttachment ,
1311 LocalImageAttachment ,
1412} from 'stream-chat' ;
1513
14+ import { useAudioPreviewManager } from './hooks/useAudioPreviewManager' ;
15+
1616import { useMessageComposer } from '../../contexts' ;
1717import { useAttachmentManagerState } from '../../contexts/messageInputContext/hooks/useAttachmentManagerState' ;
1818import {
@@ -21,7 +21,6 @@ import {
2121} from '../../contexts/messageInputContext/MessageInputContext' ;
2222import { useTheme } from '../../contexts/themeContext/ThemeContext' ;
2323import { isSoundPackageAvailable } from '../../native' ;
24- import { AudioConfig } from '../../types/types' ;
2524
2625const IMAGE_PREVIEW_SIZE = 100 ;
2726const FILE_PREVIEW_HEIGHT = 60 ;
@@ -42,9 +41,6 @@ const UnMemoizedAttachmentUploadListPreview = (
4241 props : AttachmentUploadPreviewListPropsWithContext ,
4342) => {
4443 const [ flatListWidth , setFlatListWidth ] = useState ( 0 ) ;
45- const [ audioAttachmentsStateMap , setAudioAttachmentsStateMap ] = useState <
46- Record < string , AudioConfig >
47- > ( { } ) ;
4844 const flatListRef = useRef < FlatList < LocalAttachment > | null > ( null ) ;
4945 const {
5046 AudioAttachmentUploadPreview,
@@ -68,82 +64,15 @@ const UnMemoizedAttachmentUploadListPreview = (
6864 const fileUploads = useMemo ( ( ) => {
6965 return attachments . filter ( ( attachment ) => ! isLocalImageAttachment ( attachment ) ) ;
7066 } , [ attachments ] ) ;
71-
72- useEffect ( ( ) => {
73- const newAudioAttachmentsStateMap = fileUploads . reduce (
74- ( acc , attachment ) => {
75- if ( isAudioAttachment ( attachment ) || isVoiceRecordingAttachment ( attachment ) ) {
76- acc [ attachment . localMetadata . id ] = {
77- duration :
78- attachment . duration ||
79- audioAttachmentsStateMap [ attachment . localMetadata . id ] ?. duration ||
80- 0 ,
81- paused : true ,
82- progress : 0 ,
83- } ;
84- }
85- return acc ;
86- } ,
87- { } as Record < string , AudioConfig > ,
67+ const audioUploads = useMemo ( ( ) => {
68+ return fileUploads . filter (
69+ ( attachment ) =>
70+ isLocalAudioAttachment ( attachment ) || isLocalVoiceRecordingAttachment ( attachment ) ,
8871 ) ;
89-
90- setAudioAttachmentsStateMap ( newAudioAttachmentsStateMap ) ;
91- // eslint-disable-next-line react-hooks/exhaustive-deps
9272 } , [ fileUploads ] ) ;
9373
94- // Handler triggered when an audio is loaded in the message input. The initial state is defined for the audio here
95- // and the duration is set.
96- const onLoad = useCallback ( ( index : string , duration : number ) => {
97- setAudioAttachmentsStateMap ( ( prevState ) => ( {
98- ...prevState ,
99- [ index ] : {
100- ...prevState [ index ] ,
101- duration,
102- } ,
103- } ) ) ;
104- } , [ ] ) ;
105-
106- // The handler which is triggered when the audio progresses/ the thumb is dragged in the progress control. The
107- // progressed duration is set here.
108- const onProgress = useCallback ( ( index : string , progress : number ) => {
109- setAudioAttachmentsStateMap ( ( prevState ) => ( {
110- ...prevState ,
111- [ index ] : {
112- ...prevState [ index ] ,
113- progress,
114- } ,
115- } ) ) ;
116- } , [ ] ) ;
117-
118- // The handler which controls or sets the paused/played state of the audio.
119- const onPlayPause = useCallback ( ( index : string , pausedStatus ?: boolean ) => {
120- if ( pausedStatus === false ) {
121- // In this case, all others except the index are set to paused.
122- setAudioAttachmentsStateMap ( ( prevState ) => {
123- const newState = { ...prevState } ;
124- Object . keys ( newState ) . forEach ( ( key ) => {
125- if ( key !== index ) {
126- newState [ key ] . paused = true ;
127- }
128- } ) ;
129- return {
130- ...newState ,
131- [ index ] : {
132- ...newState [ index ] ,
133- paused : false ,
134- } ,
135- } ;
136- } ) ;
137- } else {
138- setAudioAttachmentsStateMap ( ( prevState ) => ( {
139- ...prevState ,
140- [ index ] : {
141- ...prevState [ index ] ,
142- paused : true ,
143- } ,
144- } ) ) ;
145- }
146- } , [ ] ) ;
74+ const { audioAttachmentsStateMap, onLoad, onProgress, onPlayPause } =
75+ useAudioPreviewManager ( audioUploads ) ;
14776
14877 const renderImageItem = useCallback (
14978 ( { item } : { item : LocalImageAttachment } ) => {
0 commit comments