Skip to content

Commit cccdd38

Browse files
committed
fix: mitigate breaking changes
1 parent de18a12 commit cccdd38

File tree

9 files changed

+305
-24
lines changed

9 files changed

+305
-24
lines changed

package/src/components/Attachment/AudioAttachment.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
VideoSeekResponse,
2424
} from '../../native';
2525
import { AudioPlayerState } from '../../state-store/audio-player';
26+
import { AudioConfig } from '../../types/types';
2627
import { getTrimmedAttachmentTitle } from '../../utils/getTrimmedAttachmentTitle';
2728
import { ProgressControl } from '../ProgressControl/ProgressControl';
2829
import { WaveProgressBar } from '../ProgressControl/WaveProgressBar';
@@ -32,16 +33,44 @@ const ONE_SECOND_IN_MILLISECONDS = 1000;
3233

3334
dayjs.extend(duration);
3435

35-
export type AudioAttachmentType = StreamAudioAttachment | StreamVoiceRecordingAttachment;
36+
export type AudioAttachmentType = AudioConfig &
37+
Pick<
38+
StreamAudioAttachment | StreamVoiceRecordingAttachment,
39+
'waveform_data' | 'asset_url' | 'title' | 'mime_type'
40+
> & {
41+
id: string;
42+
type: 'audio' | 'voiceRecording';
43+
};
3644

3745
export type AudioAttachmentProps = {
3846
item: AudioAttachmentType;
3947
message?: LocalMessage;
4048
titleMaxLength?: number;
4149
hideProgressBar?: boolean;
50+
/**
51+
* If true, the speed settings button will be shown.
52+
*/
4253
showSpeedSettings?: boolean;
4354
testID?: string;
55+
/**
56+
* If true, the audio attachment is in preview mode in the message input.
57+
*/
4458
isPreview?: boolean;
59+
/**
60+
* Callback to be called when the audio is loaded
61+
* @deprecated This is deprecated and will be removed in the future.
62+
*/
63+
onLoad?: (index: string, duration: number) => void;
64+
/**
65+
* Callback to be called when the audio is played or paused
66+
* @deprecated This is deprecated and will be removed in the future.
67+
*/
68+
onPlayPause?: (index: string, pausedStatus?: boolean) => void;
69+
/**
70+
* Callback to be called when the audio progresses
71+
* @deprecated This is deprecated and will be removed in the future.
72+
*/
73+
onProgress?: (index: string, progress: number) => void;
4574
};
4675

4776
const audioPlayerSelector = (state: AudioPlayerState) => ({

package/src/components/Attachment/FileAttachmentGroup.tsx

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
33

4-
import { isAudioAttachment, isVoiceRecordingAttachment } from 'stream-chat';
4+
import { Attachment, isAudioAttachment, isVoiceRecordingAttachment } from 'stream-chat';
55

66
import { Attachment as AttachmentDefault } from './Attachment';
77

@@ -30,9 +30,87 @@ export type FileAttachmentGroupPropsWithContext = Pick<MessageContextValue, 'fil
3030
}>;
3131
};
3232

33+
type FilesToDisplayType = Attachment & {
34+
duration: number;
35+
paused: boolean;
36+
progress: number;
37+
};
38+
3339
const FileAttachmentGroupWithContext = (props: FileAttachmentGroupPropsWithContext) => {
3440
const { Attachment, AudioAttachment, files, message, styles: stylesProp = {} } = props;
3541

42+
const [filesToDisplay, setFilesToDisplay] = useState<FilesToDisplayType[]>(() =>
43+
files.map((file) => ({ ...file, duration: file.duration || 0, paused: true, progress: 0 })),
44+
);
45+
46+
useEffect(() => {
47+
setFilesToDisplay(
48+
files.map((file) => ({ ...file, duration: file.duration || 0, paused: true, progress: 0 })),
49+
);
50+
}, [files]);
51+
52+
/**
53+
* Handler triggered when an audio is loaded in the message input. The initial state is defined for the audio here and the duration is set.
54+
* @param index - The index of the audio
55+
* @param duration - The duration of the audio
56+
*
57+
* @deprecated This is deprecated and will be removed in the future.
58+
* FIXME: Remove this in the next major version.
59+
*/
60+
const onLoad = (index: string, duration: number) => {
61+
setFilesToDisplay((prevFilesToDisplay) =>
62+
prevFilesToDisplay.map((fileToDisplay, id) => ({
63+
...fileToDisplay,
64+
duration: id.toString() === index ? duration : fileToDisplay.duration,
65+
})),
66+
);
67+
};
68+
69+
/**
70+
* Handler which is triggered when the audio progresses/ the thumb is dragged in the progress control. The progressed duration is set here.
71+
* @param index - The index of the audio
72+
* @param progress - The progress of the audio
73+
*
74+
* @deprecated This is deprecated and will be removed in the future.
75+
* FIXME: Remove this in the next major version.
76+
*/
77+
const onProgress = (index: string, progress: number) => {
78+
setFilesToDisplay((prevFilesToDisplay) =>
79+
prevFilesToDisplay.map((filesToDisplay, id) => ({
80+
...filesToDisplay,
81+
progress: id.toString() === index ? progress : filesToDisplay.progress,
82+
})),
83+
);
84+
};
85+
86+
/**
87+
* Handler which controls or sets the paused/played state of the audio.
88+
* @param index - The index of the audio
89+
* @param pausedStatus - The paused status of the audio
90+
*
91+
* @deprecated This is deprecated and will be removed in the future.
92+
* FIXME: Remove this in the next major version.
93+
*/
94+
const onPlayPause = (index: string, pausedStatus?: boolean) => {
95+
if (pausedStatus === false) {
96+
// If the status is false we set the audio with the index as playing and the others as paused.
97+
setFilesToDisplay((prevFilesToDisplay) =>
98+
prevFilesToDisplay.map((fileToDisplay, id) => ({
99+
...fileToDisplay,
100+
paused: id.toString() !== index,
101+
})),
102+
);
103+
} else {
104+
// If the status is true we simply set all the audio's paused state as true.
105+
setFilesToDisplay((prevFilesToDisplay) =>
106+
prevFilesToDisplay.map((fileToDisplay) => ({
107+
...fileToDisplay,
108+
paused: true,
109+
})),
110+
);
111+
}
112+
};
113+
36114
const {
37115
theme: {
38116
messageSimple: {
@@ -43,7 +121,7 @@ const FileAttachmentGroupWithContext = (props: FileAttachmentGroupPropsWithConte
43121

44122
return (
45123
<View style={[styles.container, container, stylesProp.container]}>
46-
{files.map((file, index) => (
124+
{filesToDisplay.map((file, index) => (
47125
<View
48126
key={`file-by-attachment-group-${message.id}-${index}`}
49127
style={[
@@ -54,7 +132,14 @@ const FileAttachmentGroupWithContext = (props: FileAttachmentGroupPropsWithConte
54132
>
55133
{(isAudioAttachment(file) || isVoiceRecordingAttachment(file)) &&
56134
isSoundPackageAvailable() ? (
57-
<AudioAttachment item={file} message={message} showSpeedSettings={true} />
135+
<AudioAttachment
136+
item={{ ...file, id: index.toString(), type: file.type }}
137+
message={message}
138+
onLoad={onLoad}
139+
onPlayPause={onPlayPause}
140+
onProgress={onProgress}
141+
showSpeedSettings={true}
142+
/>
58143
) : (
59144
<Attachment attachment={file} />
60145
)}

package/src/components/MessageInput/AttachmentUploadPreviewList.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
LocalImageAttachment,
1212
} from 'stream-chat';
1313

14+
import { useAudioPreviewManager } from './hooks/useAudioPreviewManager';
15+
1416
import { useMessageComposer } from '../../contexts';
1517
import { useAttachmentManagerState } from '../../contexts/messageInputContext/hooks/useAttachmentManagerState';
1618
import {
@@ -62,6 +64,15 @@ const UnMemoizedAttachmentUploadListPreview = (
6264
const fileUploads = useMemo(() => {
6365
return attachments.filter((attachment) => !isLocalImageAttachment(attachment));
6466
}, [attachments]);
67+
const audioUploads = useMemo(() => {
68+
return fileUploads.filter(
69+
(attachment) =>
70+
isLocalAudioAttachment(attachment) || isLocalVoiceRecordingAttachment(attachment),
71+
);
72+
}, [fileUploads]);
73+
74+
const { audioAttachmentsStateMap, onLoad, onProgress, onPlayPause } =
75+
useAudioPreviewManager(audioUploads);
6576

6677
const renderImageItem = useCallback(
6778
({ item }: { item: LocalImageAttachment }) => {
@@ -89,7 +100,11 @@ const UnMemoizedAttachmentUploadListPreview = (
89100
return (
90101
<AudioAttachmentUploadPreview
91102
attachment={item}
103+
audioAttachmentConfig={audioAttachmentsStateMap[item.localMetadata.id]}
92104
handleRetry={attachmentManager.uploadAttachment}
105+
onLoad={onLoad}
106+
onPlayPause={onPlayPause}
107+
onProgress={onProgress}
93108
removeAttachments={attachmentManager.removeAttachments}
94109
/>
95110
);
@@ -98,7 +113,11 @@ const UnMemoizedAttachmentUploadListPreview = (
98113
return (
99114
<AudioAttachmentUploadPreview
100115
attachment={item}
116+
audioAttachmentConfig={audioAttachmentsStateMap[item.localMetadata.id]}
101117
handleRetry={attachmentManager.uploadAttachment}
118+
onLoad={onLoad}
119+
onPlayPause={onPlayPause}
120+
onProgress={onProgress}
102121
removeAttachments={attachmentManager.removeAttachments}
103122
/>
104123
);
@@ -138,7 +157,11 @@ const UnMemoizedAttachmentUploadListPreview = (
138157
VideoAttachmentUploadPreview,
139158
attachmentManager.removeAttachments,
140159
attachmentManager.uploadAttachment,
160+
audioAttachmentsStateMap,
141161
flatListWidth,
162+
onLoad,
163+
onPlayPause,
164+
onProgress,
142165
],
143166
);
144167

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
311311
const {
312312
deleteVoiceRecording,
313313
micLocked,
314+
onVoicePlayerPlayPause,
315+
paused,
314316
permissionsGranted,
317+
position,
318+
progress,
315319
recording,
316320
recordingDuration,
317321
recordingStatus,
@@ -445,6 +449,10 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
445449
/>
446450
{recordingStatus === 'stopped' ? (
447451
<AudioRecordingPreview
452+
onVoicePlayerPlayPause={onVoicePlayerPlayPause}
453+
paused={paused}
454+
position={position}
455+
progress={progress}
448456
recordingDuration={recordingDuration}
449457
uri={
450458
typeof recording !== 'string'

package/src/components/MessageInput/components/AttachmentPreview/AudioAttachmentUploadPreview.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,44 @@ import { DismissAttachmentUpload } from './DismissAttachmentUpload';
1111
import { AudioAttachment } from '../../../../components/Attachment/AudioAttachment';
1212
import { useChatContext } from '../../../../contexts/chatContext/ChatContext';
1313
import { useMessageComposer } from '../../../../contexts/messageInputContext/hooks/useMessageComposer';
14-
import { UploadAttachmentPreviewProps } from '../../../../types/types';
14+
import { AudioConfig, UploadAttachmentPreviewProps } from '../../../../types/types';
1515
import { getIndicatorTypeForFileState, ProgressIndicatorTypes } from '../../../../utils/utils';
1616

1717
export type AudioAttachmentUploadPreviewProps<CustomLocalMetadata = Record<string, unknown>> =
1818
UploadAttachmentPreviewProps<
1919
LocalAudioAttachment<CustomLocalMetadata> | LocalVoiceRecordingAttachment<CustomLocalMetadata>
20-
>;
20+
> & {
21+
/**
22+
* The audio attachment config
23+
*
24+
* @deprecated This is deprecated and will be removed in the future.
25+
*/
26+
audioAttachmentConfig: AudioConfig;
27+
/**
28+
* Callback to be called when the audio is loaded
29+
* @deprecated This is deprecated and will be removed in the future.
30+
*/
31+
onLoad: (index: string, duration: number) => void;
32+
/**
33+
* Callback to be called when the audio is played or paused
34+
* @deprecated This is deprecated and will be removed in the future.
35+
*/
36+
onPlayPause: (index: string, pausedStatus?: boolean) => void;
37+
/**
38+
* Callback to be called when the audio progresses
39+
* @deprecated This is deprecated and will be removed in the future.
40+
*/
41+
onProgress: (index: string, progress: number) => void;
42+
};
2143

2244
export const AudioAttachmentUploadPreview = ({
2345
attachment,
46+
audioAttachmentConfig,
2447
handleRetry,
2548
removeAttachments,
49+
onLoad,
50+
onPlayPause,
51+
onProgress,
2652
}: AudioAttachmentUploadPreviewProps) => {
2753
const { enableOfflineSupport } = useChatContext();
2854
const indicatorType = getIndicatorTypeForFileState(
@@ -43,8 +69,9 @@ export const AudioAttachmentUploadPreview = ({
4369
...attachment,
4470
asset_url: assetUrl,
4571
id: attachment.localMetadata.id,
72+
...audioAttachmentConfig,
4673
}),
47-
[attachment, assetUrl],
74+
[attachment, assetUrl, audioAttachmentConfig],
4875
);
4976

5077
const onRetryHandler = useCallback(() => {
@@ -66,6 +93,9 @@ export const AudioAttachmentUploadPreview = ({
6693
hideProgressBar={true}
6794
isPreview={true}
6895
item={finalAttachment}
96+
onLoad={onLoad}
97+
onPlayPause={onPlayPause}
98+
onProgress={onProgress}
6999
showSpeedSettings={false}
70100
titleMaxLength={12}
71101
/>

package/src/components/MessageInput/components/AudioRecorder/AudioRecordingPreview.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ export type AudioRecordingPreviewProps = {
2222
* The waveform data to be presented to show the audio levels.
2323
*/
2424
waveformData: number[];
25+
/**
26+
* Boolean used to show the paused state of the player.
27+
*
28+
* @deprecated This is deprecated and will be removed in the future in favour of the global audio manager.
29+
* FIXME: Remove this in the next major version.
30+
*/
31+
paused: boolean;
32+
/**
33+
* Number used to show the current position of the audio being played.
34+
*
35+
* @deprecated This is deprecated and will be removed in the future in favour of the global audio manager.
36+
* FIXME: Remove this in the next major version.
37+
*/
38+
position: number;
39+
/**
40+
* Number used to show the percentage of progress of the audio being played. It should be in 0-1 range.
41+
*
42+
* @deprecated This is deprecated and will be removed in the future in favour of the global audio manager.
43+
* FIXME: Remove this in the next major version.
44+
*/
45+
progress: number;
46+
/**
47+
* Function to play or pause the audio player.
48+
*
49+
* @deprecated This is deprecated and will be removed in the future in favour of the global audio manager.
50+
* FIXME: Remove this in the next major version.
51+
*/
52+
onVoicePlayerPlayPause?: () => Promise<void>;
2553
};
2654

2755
const audioPlayerSelector = (state: AudioPlayerState) => ({

0 commit comments

Comments
 (0)