@openpeepshq/rn-components 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/components/custom/modals/media/audio-picker-sheet.test.tsx +913 -0
- package/src/components/custom/modals/media/audio-picker-sheet.tsx +181 -49
- package/src/components/custom/modals/media/record-audio-sheet.test.tsx +626 -0
- package/src/components/custom/modals/media/record-audio-sheet.tsx +335 -111
- package/src/lib/uploadMedia.test.ts +190 -0
- package/src/lib/uploadMedia.ts +55 -0
|
@@ -1,26 +1,28 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from 'react-native';
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
useState,
|
|
4
|
+
useRef,
|
|
5
|
+
useEffect,
|
|
6
|
+
useCallback,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { View, Text } from 'react-native';
|
|
10
9
|
import { BottomSheetModal } from '@gorhom/bottom-sheet';
|
|
11
10
|
import { Button } from '../../../ui/button';
|
|
12
11
|
import { AudioLinesIcon, MicIcon } from '../../../icons/index';
|
|
12
|
+
import { PauseIcon, PlayIcon } from '../../../icons/index';
|
|
13
13
|
import { BaseSheet, SheetFooter } from '../common';
|
|
14
14
|
import { useTranslation } from 'react-i18next';
|
|
15
|
+
import AudioRecorderPlayer from 'react-native-audio-recorder-player';
|
|
15
16
|
import { MediaAttachment } from '@openpeepshq/common';
|
|
16
17
|
import { useOpenpeeps } from '@openpeepshq/react';
|
|
17
18
|
import { uploadMedia } from '../../../../lib/uploadMedia';
|
|
18
19
|
import { RecordAudioSheet } from './record-audio-sheet';
|
|
19
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
pick,
|
|
22
|
+
type DocumentPickerResponse,
|
|
23
|
+
} from '@react-native-documents/picker';
|
|
20
24
|
import { bottomSheetClose } from '../../../../lib/bottom-sheet-ref';
|
|
21
25
|
|
|
22
|
-
const { width: SCREEN_WIDTH } = Dimensions.get('window');
|
|
23
|
-
|
|
24
26
|
interface AudioPickerSheetProps {
|
|
25
27
|
onSelect: (audioAttachments: MediaAttachment[]) => void | Promise<void>;
|
|
26
28
|
/** When false, the sheet selects a single file (no multiple toggle). */
|
|
@@ -32,39 +34,119 @@ export const AudioPickerSheet = forwardRef<
|
|
|
32
34
|
AudioPickerSheetProps
|
|
33
35
|
>(({ onSelect, multiple = true }, ref) => {
|
|
34
36
|
const [selectedAudios, setSelectedAudios] = useState<string[]>([]);
|
|
37
|
+
// URIs that came from a confirmed recording (not the file picker), so Done
|
|
38
|
+
// can tell the recorder's .m4a output apart from a picked container.
|
|
39
|
+
const [recordedUrls, setRecordedUrls] = useState<Set<string>>(new Set());
|
|
40
|
+
// The document picker's `name`/`type` per picked URI. Recordings never go
|
|
41
|
+
// through the file picker, so a URI found here is a picked file by
|
|
42
|
+
// definition.
|
|
43
|
+
const [pickMeta, setPickMeta] = useState<Map<string, DocumentPickerResponse>>(
|
|
44
|
+
new Map()
|
|
45
|
+
);
|
|
35
46
|
const [isMultipleSelect, setIsMultipleSelect] = useState(false);
|
|
36
|
-
|
|
47
|
+
// The one take currently playing in the preview (web's picker shows an
|
|
48
|
+
// <audio controls> per selection; RN has no scrub bar, so the equivalent
|
|
49
|
+
// parity is a play/pause affordance on the visible take).
|
|
50
|
+
const [playingUri, setPlayingUri] = useState<string | null>(null);
|
|
51
|
+
const playerRef = useRef<AudioRecorderPlayer | null>(null);
|
|
37
52
|
const { t } = useTranslation();
|
|
38
53
|
const { openpeepsApi } = useOpenpeeps();
|
|
39
54
|
const createAttachment = openpeepsApi.createMediaAttachmentAction();
|
|
40
55
|
const recordAudioSheetRef = useRef<BottomSheetModal>(null);
|
|
41
56
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
57
|
+
// Preview player for the visible take. Created lazily on first play and
|
|
58
|
+
// torn down by `stopPreviewPlayback` (Done/Cancel, a replaced selection,
|
|
59
|
+
// or unmount) so a confirmed take never keeps playing after the sheet is
|
|
60
|
+
// gone.
|
|
61
|
+
const stopPreviewPlayback = useCallback(() => {
|
|
62
|
+
const player = playerRef.current;
|
|
63
|
+
if (player) {
|
|
64
|
+
player.removePlayBackListener();
|
|
65
|
+
void player.stopPlayer().catch(() => undefined);
|
|
66
|
+
playerRef.current = null;
|
|
67
|
+
}
|
|
68
|
+
setPlayingUri(null);
|
|
69
|
+
}, []);
|
|
70
|
+
|
|
71
|
+
const togglePreviewPlayback = (uri: string) => {
|
|
72
|
+
if (playingUri === uri) {
|
|
73
|
+
stopPreviewPlayback();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const player = (playerRef.current ??= new AudioRecorderPlayer());
|
|
77
|
+
void (async () => {
|
|
78
|
+
if (playingUri) await player.stopPlayer().catch(() => undefined);
|
|
79
|
+
await player.startPlayer(uri);
|
|
80
|
+
player.addPlayBackListener((e) => {
|
|
81
|
+
// The native player reports the end of the take through
|
|
82
|
+
// `isFinished` (the callback keeps firing while playing, so the
|
|
83
|
+
// flag distinguishes natural end from a user stop, which goes
|
|
84
|
+
// through `stopPreviewPlayback` and never sets it) — the same
|
|
85
|
+
// end-of-playback transition web's <audio> gets from `ended`.
|
|
86
|
+
if (e.isFinished) stopPreviewPlayback();
|
|
87
|
+
});
|
|
88
|
+
setPlayingUri(uri);
|
|
89
|
+
})().catch(() => stopPreviewPlayback());
|
|
47
90
|
};
|
|
48
91
|
|
|
92
|
+
// A take in flight must not outlive the sheet.
|
|
93
|
+
useEffect(
|
|
94
|
+
() => () => {
|
|
95
|
+
const player = playerRef.current;
|
|
96
|
+
if (player) {
|
|
97
|
+
player.removePlayBackListener();
|
|
98
|
+
void player.stopPlayer().catch(() => undefined);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
[]
|
|
102
|
+
);
|
|
103
|
+
|
|
49
104
|
const resetStates = () => {
|
|
50
105
|
setSelectedAudios([]);
|
|
106
|
+
setRecordedUrls(new Set());
|
|
107
|
+
setPickMeta(new Map());
|
|
51
108
|
setIsMultipleSelect(false);
|
|
52
|
-
|
|
109
|
+
stopPreviewPlayback();
|
|
53
110
|
};
|
|
54
111
|
|
|
55
112
|
const onConfirm = async () => {
|
|
56
|
-
const
|
|
113
|
+
const results = await Promise.all(
|
|
57
114
|
selectedAudios.map((media) =>
|
|
58
115
|
uploadMedia({
|
|
59
116
|
mediaUri: media,
|
|
60
117
|
createAttachments: createAttachment,
|
|
61
118
|
type: 'audio',
|
|
62
|
-
|
|
119
|
+
// Web tags composer audio `post-audio` in
|
|
120
|
+
// `platform/react/src/components/post/post-form/ComposeAttachments.tsx`
|
|
121
|
+
// (`usageForFile`, reached from `enqueueFile`); RN mirrors it so
|
|
122
|
+
// server/client logic keyed on the value sees one tag across
|
|
123
|
+
// platforms.
|
|
124
|
+
usage: 'post-audio',
|
|
63
125
|
alt: 'audio',
|
|
126
|
+
// Recordings are the recorder's own .m4a output; picked files are
|
|
127
|
+
// not, and their reported name/mimetype must match their bytes.
|
|
128
|
+
isRecordedAudio: recordedUrls.has(media),
|
|
129
|
+
// Picked files carry the document picker's authoritative name/type
|
|
130
|
+
// (an opaque content:// URI has no extension to guess from). A
|
|
131
|
+
// `null` here marks a recording, whose known .m4a output is
|
|
132
|
+
// trusted instead — uploadMedia falls back to the URI inference
|
|
133
|
+
// only when both are missing.
|
|
134
|
+
name: pickMeta.get(media)?.name ?? null,
|
|
135
|
+
mimeType: pickMeta.get(media)?.type ?? null,
|
|
64
136
|
})
|
|
65
137
|
)
|
|
66
|
-
)
|
|
67
|
-
|
|
138
|
+
);
|
|
139
|
+
const attachments = results.filter(Boolean) as MediaAttachment[];
|
|
140
|
+
if (results.every((r) => !r)) {
|
|
141
|
+
// Every upload failed (e.g. the server rejected an over-cap audio):
|
|
142
|
+
// keep the sheet open with the takes/selection intact so the user can
|
|
143
|
+
// retry instead of re-recording from scratch.
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// Partial failure (some uploads succeed): close with the successes and
|
|
147
|
+
// drop the failed takes — each failure is already reported by the
|
|
148
|
+
// per-upload error toast in uploadMedia.
|
|
149
|
+
onSelect(attachments);
|
|
68
150
|
resetStates();
|
|
69
151
|
bottomSheetClose(ref);
|
|
70
152
|
};
|
|
@@ -74,37 +156,39 @@ export const AudioPickerSheet = forwardRef<
|
|
|
74
156
|
return null;
|
|
75
157
|
}
|
|
76
158
|
|
|
77
|
-
|
|
159
|
+
// The paged carousel would need a per-slide player to stay in sync as it
|
|
160
|
+
// scrolls; the visible take gets a play/pause instead (parity with web's
|
|
161
|
+
// <audio controls>, without a scrub bar RN has no hook for).
|
|
162
|
+
const visibleUri = selectedAudios[0];
|
|
163
|
+
const isPlaying = playingUri === visibleUri;
|
|
164
|
+
// The icon toggles Play/Pause, so the label must track the state too —
|
|
165
|
+
// a static "Play audio" would misdescribe the control while playing.
|
|
166
|
+
const playLabel = t(
|
|
167
|
+
isPlaying ? 'common.media.audio.pause' : 'common.media.audio.play'
|
|
168
|
+
);
|
|
78
169
|
|
|
79
170
|
return (
|
|
80
|
-
<View className="w-full mb-4">
|
|
81
|
-
<
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
style={{ width: VIDEO_WIDTH }}
|
|
95
|
-
className="items-center justify-center"
|
|
96
|
-
>
|
|
97
|
-
<MicIcon size={24} color="white" />
|
|
98
|
-
</View>
|
|
99
|
-
))}
|
|
100
|
-
</ScrollView>
|
|
171
|
+
<View className="w-full mb-4 items-center justify-center py-6">
|
|
172
|
+
<View className="flex-row items-center gap-4">
|
|
173
|
+
<Button
|
|
174
|
+
variant={'ghost'}
|
|
175
|
+
accessibilityLabel={playLabel}
|
|
176
|
+
onPress={() => togglePreviewPlayback(visibleUri)}
|
|
177
|
+
>
|
|
178
|
+
{isPlaying ? (
|
|
179
|
+
<PauseIcon size={24} color="white" />
|
|
180
|
+
) : (
|
|
181
|
+
<PlayIcon size={24} color="white" />
|
|
182
|
+
)}
|
|
183
|
+
</Button>
|
|
184
|
+
</View>
|
|
101
185
|
{selectedAudios.length > 1 && (
|
|
102
186
|
<View className="absolute bottom-4 w-full flex-row justify-center gap-2">
|
|
103
187
|
{selectedAudios.map((_, index) => (
|
|
104
188
|
<View
|
|
105
189
|
key={index}
|
|
106
190
|
className={`w-2 h-2 rounded-full ${
|
|
107
|
-
index ===
|
|
191
|
+
index === 0 ? 'bg-white' : 'bg-white/50'
|
|
108
192
|
}`}
|
|
109
193
|
/>
|
|
110
194
|
))}
|
|
@@ -114,8 +198,35 @@ export const AudioPickerSheet = forwardRef<
|
|
|
114
198
|
);
|
|
115
199
|
};
|
|
116
200
|
|
|
117
|
-
const handleRecordComplete = async (
|
|
118
|
-
|
|
201
|
+
const handleRecordComplete = async (recordedUri: string) => {
|
|
202
|
+
// `recordedUri` is the single take the record sheet just confirmed — the
|
|
203
|
+
// sheet's payload is a bare string, not the picker's `MediaAttachment[]`
|
|
204
|
+
// selection, so it is kept a distinct identifier from `selectedAudios`.
|
|
205
|
+
// Single mode replaces (the pick path above does the same), so a second
|
|
206
|
+
// confirmed recording cannot leave two takes selected with one Done.
|
|
207
|
+
// The replaced take's URI is dropped from `recordedUrls` too, so the set
|
|
208
|
+
// does not accumulate stale entries across re-records (review item 10).
|
|
209
|
+
if (multiple) {
|
|
210
|
+
setSelectedAudios([...selectedAudios, recordedUri]);
|
|
211
|
+
} else {
|
|
212
|
+
const replaced = selectedAudios[0];
|
|
213
|
+
// The replacement drops the visible take (mirroring the pick path): if
|
|
214
|
+
// a preview was playing the replaced take, stopping it here keeps the
|
|
215
|
+
// `playingUri`/label in sync with the now-visible take instead of
|
|
216
|
+
// keeping a discarded take audible (unique per-take paths mean the old
|
|
217
|
+
// file still exists and would keep playing).
|
|
218
|
+
stopPreviewPlayback();
|
|
219
|
+
setSelectedAudios([recordedUri]);
|
|
220
|
+
setRecordedUrls((prev) => {
|
|
221
|
+
const next = new Set(prev);
|
|
222
|
+
if (replaced) next.delete(replaced);
|
|
223
|
+
next.add(recordedUri);
|
|
224
|
+
return next;
|
|
225
|
+
});
|
|
226
|
+
recordAudioSheetRef.current?.close();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
setRecordedUrls((prev) => new Set(prev).add(recordedUri));
|
|
119
230
|
recordAudioSheetRef.current?.close();
|
|
120
231
|
};
|
|
121
232
|
|
|
@@ -149,14 +260,35 @@ export const AudioPickerSheet = forwardRef<
|
|
|
149
260
|
});
|
|
150
261
|
if (result) {
|
|
151
262
|
const uris = result.map((r) => r.uri);
|
|
152
|
-
// Single mode replaces the pick; multiple mode appends.
|
|
263
|
+
// Single mode replaces the pick; multiple mode appends. Picked
|
|
264
|
+
// files replace the visible take, so its preview stops.
|
|
265
|
+
stopPreviewPlayback();
|
|
153
266
|
setSelectedAudios(multiple ? [...selectedAudios, ...uris] : uris);
|
|
267
|
+
// The picker's `name`/`type` are the file's authoritative
|
|
268
|
+
// metadata (an opaque content:// URI carries no extension to
|
|
269
|
+
// guess from) — kept alongside the URI so Done can hand them to
|
|
270
|
+
// uploadMedia. Picked files are never the recorder's output.
|
|
271
|
+
setPickMeta((prev) => {
|
|
272
|
+
const next: Map<string, DocumentPickerResponse> = multiple
|
|
273
|
+
? new Map(prev)
|
|
274
|
+
: new Map();
|
|
275
|
+
for (const r of result) next.set(r.uri, r);
|
|
276
|
+
return next;
|
|
277
|
+
});
|
|
154
278
|
}
|
|
155
279
|
}}
|
|
156
280
|
>
|
|
157
281
|
<AudioLinesIcon size={16} className="text-foreground" />
|
|
158
282
|
</Button>
|
|
159
283
|
|
|
284
|
+
<Button
|
|
285
|
+
variant={'ghost'}
|
|
286
|
+
accessibilityLabel={t('posts.form.recordVoice')}
|
|
287
|
+
onPress={() => recordAudioSheetRef.current?.present()}
|
|
288
|
+
>
|
|
289
|
+
<MicIcon size={16} className="text-foreground" />
|
|
290
|
+
</Button>
|
|
291
|
+
|
|
160
292
|
<SheetFooter
|
|
161
293
|
onCancel={() => {
|
|
162
294
|
resetStates();
|