Skip to content

Commit a69b623

Browse files
compose: Allow multiple file selection when uploading pictures
Fixes: #2366 Co-authored-by: Akash Dhiman <akash.d0407@gmail.com>
1 parent c851ac1 commit a69b623

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/compose/ComposeMenu.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ export default function ComposeMenu(props: Props): Node {
124124

125125
const { assets } = response;
126126

127-
// TODO: support sending multiple files; see library's docs for how to
128-
// let `assets` have more than one item in `response`.
129-
const firstAsset = assets && assets[0];
130-
131-
if (!firstAsset) {
127+
if (!assets || !assets[0]) {
132128
// TODO: See if we these unexpected situations actually happen. …Ah,
133129
// yep, reportedly (and we've seen in Sentry):
134130
// https://github.com/react-native-image-picker/react-native-image-picker/issues/1945
@@ -139,22 +135,39 @@ export default function ComposeMenu(props: Props): Node {
139135
return;
140136
}
141137

142-
const { uri, fileName } = firstAsset;
138+
const attachments = [];
139+
let numMalformed = 0;
140+
assets.forEach((asset, i) => {
141+
const { uri, fileName } = asset;
143142

144-
if (uri == null || fileName == null) {
145-
// TODO: See if these unexpected situations actually happen.
146-
showErrorAlert(_('Error'), _('Failed to attach your file.'));
147-
logging.error(
148-
'First (should be only) asset returned from image picker had nullish `url` and/or `fileName`',
149-
{
143+
if (uri == null || fileName == null) {
144+
// TODO: See if these unexpected situations actually happen.
145+
logging.error('An asset returned from image picker had nullish `url` and/or `fileName`', {
150146
'uri == null': uri == null,
151147
'fileName == null': fileName == null,
152-
},
153-
);
154-
return;
148+
i,
149+
});
150+
numMalformed++;
151+
return;
152+
}
153+
154+
attachments.push({ name: chooseUploadImageFilename(uri, fileName), url: uri });
155+
});
156+
157+
if (numMalformed > 0) {
158+
if (assets.length === 1 && numMalformed === 1) {
159+
showErrorAlert(_('Error'), _('Failed to attach your file.'));
160+
return;
161+
} else if (assets.length === numMalformed) {
162+
showErrorAlert(_('Error'), _('Failed to attach your files.'));
163+
return;
164+
} else {
165+
showErrorAlert(_('Error'), _('Failed to attach some of your files.'));
166+
// no return; `attachments` will have some items that we can insert
167+
}
155168
}
156169

157-
insertAttachments([{ name: chooseUploadImageFilename(uri, fileName), url: uri }]);
170+
insertAttachments(attachments);
158171
},
159172
[_, insertAttachments],
160173
);
@@ -167,6 +180,17 @@ export default function ComposeMenu(props: Props): Node {
167180

168181
quality: 1.0,
169182
includeBase64: false,
183+
184+
// From the doc:
185+
// https://github.com/react-native-image-picker/react-native-image-picker/tree/v4.8.4#options
186+
// > […] use `0` to allow any number of files. Only iOS version >=
187+
// > 14 support `0` and also it supports providing any integer value
188+
//
189+
// On Android, the implementation now seems to put you into a kind
190+
// of file picker UI filtered down to images, instead of into the
191+
// Gallery app. See screen recordings on a recent Android:
192+
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/Android.20select.20multiple.20photos/near/1423109
193+
selectionLimit: 0,
170194
},
171195
handleImagePickerResponse,
172196
);

static/translations/messages_en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"Confirm": "Confirm",
33
"Failed to attach your file.": "Failed to attach your file.",
4+
"Failed to attach your files.": "Failed to attach your files.",
5+
"Failed to attach some of your files.": "Failed to attach some of your files.",
46
"Configure permissions": "Configure permissions",
57
"You": "You",
68
"Discard changes": "Discard changes",

0 commit comments

Comments
 (0)