Skip to content

Commit 8b1eff9

Browse files
authored
feat: Make the DocumentPicker dependency optional (#1809)
* feat: make the document picker optional * style: fix lint issues
1 parent 32d9af8 commit 8b1eff9

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed
Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1-
import DocumentPicker from 'react-native-document-picker';
1+
/**
2+
* Types are approximated from what we need from the DocumentPicker API.
3+
*
4+
* For its full API, see https://github.com/rnmods/react-native-document-picker/blob/master/src/index.tsx
5+
* */
6+
type ResponseValue = {
7+
name: string;
8+
uri: string;
9+
size: number;
10+
type: string;
11+
};
212

3-
export const pickDocument = async ({ maxNumberOfFiles }: { maxNumberOfFiles: number }) => {
4-
try {
5-
let res = await DocumentPicker.pickMultiple({
6-
type: [DocumentPicker.types.allFiles],
7-
});
13+
let DocumentPicker: {
14+
types: { allFiles: string };
15+
pickMultiple: (opts?: { type: string[] }) => Promise<ResponseValue[]>;
16+
};
817

9-
if (maxNumberOfFiles && res.length > maxNumberOfFiles) {
10-
res = res.slice(0, maxNumberOfFiles);
11-
}
18+
try {
19+
DocumentPicker = require('react-native-document-picker').default;
20+
} catch (err) {
21+
console.log('react-native-document-picker is not installed');
22+
}
1223

13-
return {
14-
cancelled: false,
15-
docs: res.map(({ name, size, type, uri }) => ({
16-
name,
17-
size,
18-
type,
19-
uri,
20-
})),
21-
};
22-
} catch (err) {
23-
return {
24-
cancelled: true,
25-
};
26-
}
27-
};
24+
export const pickDocument = DocumentPicker
25+
? async ({ maxNumberOfFiles }: { maxNumberOfFiles: number }) => {
26+
try {
27+
let res = await DocumentPicker.pickMultiple({
28+
type: [DocumentPicker.types.allFiles],
29+
});
30+
31+
if (maxNumberOfFiles && res.length > maxNumberOfFiles) {
32+
res = res.slice(0, maxNumberOfFiles);
33+
}
34+
35+
return {
36+
cancelled: false,
37+
docs: res.map(({ name, size, type, uri }) => ({
38+
name,
39+
size,
40+
type,
41+
uri,
42+
})),
43+
};
44+
} catch (err) {
45+
return {
46+
cancelled: true,
47+
};
48+
}
49+
}
50+
: null;

package/src/components/Channel/Channel.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import {
7171
ThumbsUpReaction,
7272
WutReaction,
7373
} from '../../icons';
74-
import { FlatList as FlatListDefault } from '../../native';
74+
import { FlatList as FlatListDefault, pickDocument } from '../../native';
7575
import type { DefaultStreamChatGenerics } from '../../types/types';
7676
import { generateRandomId, MessageStatusTypes, ReactionData } from '../../utils/utils';
7777
import { Attachment as AttachmentDefault } from '../Attachment/Attachment';
@@ -447,7 +447,8 @@ const ChannelWithContext = <
447447
handleRetry,
448448
handleThreadReply,
449449
hasCommands = true,
450-
hasFilePicker = true,
450+
// If pickDocument isn't available, default to hiding the file picker
451+
hasFilePicker = pickDocument !== null,
451452
hasImagePicker = true,
452453
hideDateSeparators = false,
453454
hideStickyDateHeader = false,

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,13 @@ export const MessageInputProvider = <
574574
};
575575

576576
const pickFile = async () => {
577+
if (pickDocument === null) {
578+
console.log(
579+
'The file picker is not installed. Check our Getting Started documentation to install it.',
580+
);
581+
return;
582+
}
583+
577584
if (numberOfUploads >= value.maxNumberOfFiles) {
578585
Alert.alert('Maximum number of files reached');
579586
return;

package/src/native.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export const registerNativeHandlers = (handlers: Handlers) => {
251251
getPhotos = handlers.getPhotos;
252252
}
253253

254-
if (handlers.pickDocument) {
254+
if (handlers.pickDocument !== undefined) {
255255
pickDocument = handlers.pickDocument;
256256
}
257257

0 commit comments

Comments
 (0)