AttachmentsModal.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import React, { useCallback, useState } from 'react';
  2. import { StyleSheet, TouchableOpacity, View, Text, Button } from 'react-native';
  3. import ActionSheet, { Route, SheetManager, useSheetRouter } from 'react-native-actions-sheet';
  4. import { getFontSize } from 'src/utils';
  5. import { Colors } from 'src/theme';
  6. import { WarningProps } from '../types';
  7. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  8. import { usePostReportConversationMutation } from '@api/chat';
  9. import * as ImagePicker from 'expo-image-picker';
  10. import * as DocumentPicker from 'react-native-document-picker';
  11. import MegaphoneIcon from 'assets/icons/messages/megaphone.svg';
  12. import { MaterialCommunityIcons } from '@expo/vector-icons';
  13. import RouteB from './RouteB';
  14. const AttachmentsModal = () => {
  15. const insets = useSafeAreaInsets();
  16. const [chatData, setChatData] = useState<any>(null);
  17. const [shouldOpenWarningModal, setShouldOpenWarningModal] = useState<WarningProps | null>(null);
  18. const { mutateAsync: reportUser } = usePostReportConversationMutation();
  19. const router = useSheetRouter('sheet-router');
  20. const [currentLocation, setCurrentLocation] = useState<{
  21. latitude: number;
  22. longitude: number;
  23. } | null>(null);
  24. const [selectedLocation, setSelectedLocation] = useState<{
  25. latitude: number;
  26. longitude: number;
  27. } | null>(null);
  28. const [isLoading, setIsLoading] = useState(true);
  29. const handleSheetOpen = (payload: any) => {
  30. setChatData(payload);
  31. };
  32. const handleReport = async () => {
  33. if (!chatData) return;
  34. setShouldOpenWarningModal({
  35. title: `Report ${chatData.name}`,
  36. buttonTitle: 'Report',
  37. message: `Are you sure you want to report ${chatData.name}?\nIf you proceed, the chat history with ${chatData.name} will become visible to NomadMania admins for investigation.`,
  38. action: async () => {
  39. await reportUser({
  40. token: chatData.token,
  41. reported_user_id: chatData.uid
  42. });
  43. }
  44. });
  45. setTimeout(() => {
  46. SheetManager.hide('chat-attachments');
  47. setShouldOpenWarningModal(null);
  48. }, 300);
  49. };
  50. const handleOpenGallery = useCallback(async () => {
  51. if (!chatData) return;
  52. try {
  53. const perm = await ImagePicker.requestMediaLibraryPermissionsAsync();
  54. if (!perm.granted) {
  55. console.warn('Permission for gallery not granted');
  56. return;
  57. }
  58. const result = await ImagePicker.launchImageLibraryAsync({
  59. mediaTypes: ImagePicker.MediaTypeOptions.All,
  60. allowsMultipleSelection: true,
  61. quality: 1,
  62. selectionLimit: 4
  63. });
  64. if (!result.canceled && result.assets) {
  65. const files = result.assets.map((asset) => ({
  66. uri: asset.uri,
  67. type: asset.type === 'video' ? 'video' : 'image'
  68. }));
  69. chatData.onSendMedia(files);
  70. }
  71. SheetManager.hide('chat-attachments');
  72. } catch (err) {
  73. console.warn('Gallery error: ', err);
  74. }
  75. }, [chatData?.onSendMedia, chatData?.closeOptions]);
  76. const handleOpenCamera = useCallback(async () => {
  77. if (!chatData) return;
  78. try {
  79. const perm = await ImagePicker.requestCameraPermissionsAsync();
  80. if (!perm.granted) {
  81. console.warn('Permission for camera not granted');
  82. return;
  83. }
  84. const result = await ImagePicker.launchCameraAsync({
  85. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  86. quality: 1
  87. });
  88. if (!result.canceled && result.assets) {
  89. const files = result.assets.map((asset) => ({
  90. uri: asset.uri,
  91. type: asset.type === 'video' ? 'video' : 'image'
  92. }));
  93. chatData.onSendMedia(files);
  94. }
  95. SheetManager.hide('chat-attachments');
  96. } catch (err) {
  97. console.warn('Camera error: ', err);
  98. }
  99. }, [chatData?.onSendMedia, chatData?.closeOptions]);
  100. const handleShareLiveLocation = useCallback(() => {
  101. if (!chatData) return;
  102. chatData.onShareLiveLocation();
  103. SheetManager.hide('chat-attachments');
  104. }, [chatData?.onShareLiveLocation, chatData?.closeOptions]);
  105. const handleSendFile = useCallback(async () => {
  106. if (!chatData) return;
  107. try {
  108. const res = await DocumentPicker.pick({
  109. type: [DocumentPicker.types.allFiles],
  110. allowMultiSelection: false
  111. });
  112. const file = {
  113. uri: res[0].uri,
  114. name: res[0].name,
  115. type: res[0].type
  116. };
  117. if (chatData.onSendFile) {
  118. chatData.onSendFile([file]);
  119. }
  120. } catch (err) {
  121. if (DocumentPicker.isCancel(err)) {
  122. console.log('User canceled document picker');
  123. } else {
  124. console.warn('DocumentPicker error:', err);
  125. }
  126. }
  127. SheetManager.hide('chat-attachments');
  128. }, [chatData?.onSendFile, chatData?.closeOptions]);
  129. const RouteA = () => {
  130. const router = useSheetRouter('sheet-router');
  131. return (
  132. <View
  133. style={[
  134. styles.container,
  135. { paddingBottom: 8 + insets.bottom, backgroundColor: Colors.FILL_LIGHT }
  136. ]}
  137. >
  138. <View style={styles.optionRow}>
  139. <TouchableOpacity style={styles.optionItem} onPress={handleOpenGallery}>
  140. <MaterialCommunityIcons name="image" size={36} color={Colors.ORANGE} />
  141. <Text style={styles.optionLabel}>Gallery</Text>
  142. </TouchableOpacity>
  143. <TouchableOpacity style={styles.optionItem} onPress={handleOpenCamera}>
  144. <MaterialCommunityIcons name="camera" size={36} color={Colors.ORANGE} />
  145. <Text style={styles.optionLabel}>Camera</Text>
  146. </TouchableOpacity>
  147. <TouchableOpacity
  148. style={styles.optionItem}
  149. onPress={() => {
  150. router?.navigate('route-b');
  151. }}
  152. >
  153. <MaterialCommunityIcons name="map-marker" size={36} color={Colors.ORANGE} />
  154. <Text style={styles.optionLabel}>Location</Text>
  155. </TouchableOpacity>
  156. <TouchableOpacity style={styles.optionItem} onPress={handleShareLiveLocation}>
  157. <MaterialCommunityIcons name="navigation" size={36} color={Colors.ORANGE} />
  158. <Text style={styles.optionLabel}>Live</Text>
  159. </TouchableOpacity>
  160. <TouchableOpacity style={styles.optionItem} onPress={handleSendFile}>
  161. <MaterialCommunityIcons name="file" size={36} color={Colors.ORANGE} />
  162. <Text style={styles.optionLabel}>File</Text>
  163. </TouchableOpacity>
  164. <TouchableOpacity style={styles.optionItem} onPress={handleReport}>
  165. <MegaphoneIcon fill={Colors.RED} width={36} height={36} />
  166. <Text style={styles.optionLabel}>Report</Text>
  167. </TouchableOpacity>
  168. <View style={styles.optionItem}></View>
  169. </View>
  170. </View>
  171. );
  172. };
  173. const routes: Route[] = [
  174. {
  175. name: 'route-a',
  176. component: RouteA
  177. },
  178. {
  179. name: 'route-b',
  180. component: RouteB,
  181. params: { onSendLocation: chatData?.onSendLocation, insetsBottom: insets.bottom }
  182. }
  183. ];
  184. return (
  185. <ActionSheet
  186. id="chat-attachments"
  187. gestureEnabled={true}
  188. containerStyle={{
  189. backgroundColor: Colors.FILL_LIGHT
  190. }}
  191. enableRouterBackNavigation={true}
  192. routes={routes}
  193. initialRoute="route-a"
  194. defaultOverlayOpacity={0}
  195. indicatorStyle={{ backgroundColor: Colors.WHITE }}
  196. onBeforeShow={(sheetRef) => {
  197. const payload = sheetRef || null;
  198. handleSheetOpen(payload);
  199. }}
  200. onClose={() => {
  201. if (shouldOpenWarningModal) {
  202. chatData?.setModalInfo({
  203. visible: true,
  204. type: 'delete',
  205. title: shouldOpenWarningModal.title,
  206. buttonTitle: shouldOpenWarningModal.buttonTitle,
  207. message: shouldOpenWarningModal.message,
  208. action: shouldOpenWarningModal.action
  209. });
  210. }
  211. }}
  212. />
  213. );
  214. };
  215. const styles = StyleSheet.create({
  216. option: {
  217. flexDirection: 'row',
  218. alignItems: 'center',
  219. justifyContent: 'space-between'
  220. },
  221. optionText: {
  222. fontSize: getFontSize(12),
  223. fontWeight: '600',
  224. color: Colors.DARK_BLUE
  225. },
  226. dangerOption: {
  227. paddingVertical: 10,
  228. borderBottomWidth: 1,
  229. borderBlockColor: Colors.WHITE
  230. },
  231. dangerText: {
  232. color: Colors.RED
  233. },
  234. container: {
  235. backgroundColor: Colors.WHITE
  236. },
  237. optionRow: {
  238. flexDirection: 'row',
  239. justifyContent: 'space-between',
  240. paddingHorizontal: '5%',
  241. marginVertical: 20,
  242. flexWrap: 'wrap'
  243. },
  244. optionItem: {
  245. width: '30%',
  246. paddingVertical: 8,
  247. marginBottom: 12,
  248. alignItems: 'center'
  249. },
  250. optionLabel: {
  251. marginTop: 6,
  252. fontSize: 12,
  253. color: Colors.DARK_BLUE,
  254. fontWeight: '700'
  255. }
  256. });
  257. export default AttachmentsModal;