-
Notifications
You must be signed in to change notification settings - Fork 305
/
AttachmentPickerSelectionBar.tsx
119 lines (109 loc) · 3.39 KB
/
AttachmentPickerSelectionBar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React from 'react';
import { Alert, Linking, StyleSheet, TouchableOpacity, View } from 'react-native';
import { useAttachmentPickerContext } from '../../../contexts/attachmentPickerContext/AttachmentPickerContext';
import { useMessageInputContext } from '../../../contexts/messageInputContext/MessageInputContext';
import { useTheme } from '../../../contexts/themeContext/ThemeContext';
import { useTranslationContext } from '../../../contexts/translationContext/TranslationContext';
import { takePhoto } from '../../../native';
const styles = StyleSheet.create({
container: {
alignItems: 'center',
flexDirection: 'row',
paddingHorizontal: 6,
},
icon: {
marginHorizontal: 12,
},
});
export const AttachmentPickerSelectionBar = () => {
const {
attachmentSelectionBarHeight,
CameraSelectorIcon,
closePicker,
FileSelectorIcon,
ImageSelectorIcon,
selectedPicker,
setSelectedImages,
setSelectedPicker,
} = useAttachmentPickerContext();
const { t } = useTranslationContext();
const { compressImageQuality, hasFilePicker, imageUploads, pickFile } = useMessageInputContext();
const {
theme: {
attachmentSelectionBar: { container, icon },
},
} = useTheme();
const setPicker = (selection: 'images') => {
if (selectedPicker === selection) {
setSelectedPicker(undefined);
closePicker();
} else {
setSelectedPicker(selection);
}
};
const openFilePicker = () => {
setSelectedPicker(undefined);
closePicker();
pickFile();
};
const takeAndUploadImage = async () => {
setSelectedPicker(undefined);
closePicker();
const photo = await takePhoto({ compressImageQuality });
if (photo.askToOpenSettings) {
Alert.alert(
t('Allow camera access in device settings'),
t('Device camera is used to take photos or videos.'),
[
{ style: 'cancel', text: t('Cancel') },
{ onPress: () => Linking.openSettings(), style: 'default', text: t('Open Settings') },
],
);
}
if (!photo.cancelled) {
setSelectedImages((images) => [...images, photo]);
}
};
return (
<View style={[styles.container, container, { height: attachmentSelectionBarHeight }]}>
<TouchableOpacity
hitSlop={{ bottom: 15, top: 15 }}
onPress={() => setPicker('images')}
testID='upload-photo-touchable'
>
<View style={[styles.icon, icon]}>
<ImageSelectorIcon
numberOfImageUploads={imageUploads.length}
selectedPicker={selectedPicker}
/>
</View>
</TouchableOpacity>
{hasFilePicker && (
<TouchableOpacity
hitSlop={{ bottom: 15, top: 15 }}
onPress={openFilePicker}
testID='upload-file-touchable'
>
<View style={[styles.icon, icon]}>
<FileSelectorIcon
numberOfImageUploads={imageUploads.length}
selectedPicker={selectedPicker}
/>
</View>
</TouchableOpacity>
)}
<TouchableOpacity
hitSlop={{ bottom: 15, top: 15 }}
onPress={takeAndUploadImage}
testID='take-photo-touchable'
>
<View style={[styles.icon, icon]}>
<CameraSelectorIcon
numberOfImageUploads={imageUploads.length}
selectedPicker={selectedPicker}
/>
</View>
</TouchableOpacity>
</View>
);
};