index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import React, { FC, useEffect, useState } from 'react';
  2. import { ScrollView, Text, TouchableOpacity, View, Image, StyleSheet } from 'react-native';
  3. import { NavigationProp } from '@react-navigation/native';
  4. import ImageView from 'react-native-image-viewing';
  5. import { storage, StoreType } from 'src/storage';
  6. import {
  7. PageWrapper,
  8. Header,
  9. Loading,
  10. Input,
  11. AvatarWithInitials,
  12. WarningModal
  13. } from 'src/components';
  14. import {
  15. usePostGetGroupMembersQuery,
  16. usePostGetGroupSettingsQuery,
  17. usePostLeaveGroupMutation,
  18. usePostRemoveGroupFromListMutation,
  19. usePostSetMuteForGroupMutation
  20. } from '@api/chat';
  21. import { Colors } from 'src/theme';
  22. import { API_HOST } from 'src/constants';
  23. import GroupIcon from 'assets/icons/messages/group-chat.svg';
  24. import { getFontSize } from 'src/utils';
  25. import ExitIcon from 'assets/icons/messages/exit.svg';
  26. import TrashIcon from 'assets/icons/travels-screens/trash-solid.svg';
  27. import BellSlashIcon from 'assets/icons/messages/bell-slash.svg';
  28. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  29. import EditIcon from 'assets/icons/travels-screens/pen-to-square.svg';
  30. import UserPlusIcon from 'assets/icons/user-plus.svg';
  31. import { NAVIGATION_PAGES } from 'src/types';
  32. import { SheetManager } from 'react-native-actions-sheet';
  33. import EditGroupModal from '../Components/EditGroupModal';
  34. type Props = {
  35. navigation: NavigationProp<any>;
  36. route: any;
  37. };
  38. const GroupSettingScreen: FC<Props> = ({ navigation, route }) => {
  39. const token = storage.get('token', StoreType.STRING) as string;
  40. const groupToken = route.params.groupToken;
  41. const insets = useSafeAreaInsets();
  42. const [canSeeMembers, setCanSeeMembers] = useState(false);
  43. const { data, refetch } = usePostGetGroupSettingsQuery(token, groupToken, true);
  44. const { data: members, refetch: refetchMembers } = usePostGetGroupMembersQuery(
  45. token,
  46. groupToken,
  47. canSeeMembers
  48. );
  49. const [fullSizeImageVisible, setFullSizeImageVisible] = useState(false);
  50. const [muted, setMuted] = useState(false);
  51. const [modalState, setModalState] = useState({
  52. isWarningVisible: false,
  53. title: '',
  54. buttonTitle: '',
  55. message: '',
  56. action: () => {}
  57. });
  58. const { mutateAsync: muteGroup } = usePostSetMuteForGroupMutation();
  59. const { mutateAsync: leaveGroup } = usePostLeaveGroupMutation();
  60. const { mutateAsync: removeGroupFromList } = usePostRemoveGroupFromListMutation();
  61. useEffect(() => {
  62. if (data && data.settings) {
  63. setCanSeeMembers(data.settings.members_can_see_members === 1 || data.settings.admin === 1);
  64. setMuted(data.settings.muted === 1);
  65. }
  66. }, [data]);
  67. const handleMute = async () => {
  68. await muteGroup(
  69. {
  70. token,
  71. value: muted ? 0 : 1,
  72. group_token: groupToken
  73. },
  74. {
  75. onSuccess: () => {
  76. setMuted(!muted);
  77. }
  78. }
  79. );
  80. };
  81. const handleLeaveGroup = async () => {
  82. if (!data) return;
  83. setModalState({
  84. isWarningVisible: true,
  85. title: `Leave group ${data.settings.name}`,
  86. buttonTitle: 'Leave',
  87. message: `Are you sure you want to leave ${data.settings.name}?`,
  88. action: async () => {
  89. await leaveGroup(
  90. {
  91. token,
  92. group_token: groupToken
  93. },
  94. {
  95. onSuccess: () => {
  96. navigation.navigate(NAVIGATION_PAGES.CHATS_LIST);
  97. }
  98. }
  99. );
  100. }
  101. });
  102. };
  103. const handleDeleteGroup = async () => {
  104. if (!data) return;
  105. setModalState({
  106. isWarningVisible: true,
  107. title: `Delete ${data.settings.name}`,
  108. buttonTitle: 'Delete',
  109. message: `Are you sure you want to delete this group chat?\nThis action will remove the chat from your history, but it won't affect other participants.`,
  110. action: async () => {
  111. await removeGroupFromList(
  112. {
  113. token,
  114. group_token: groupToken
  115. },
  116. {
  117. onSuccess: () => {
  118. navigation.navigate(NAVIGATION_PAGES.CHATS_LIST);
  119. }
  120. }
  121. );
  122. }
  123. });
  124. };
  125. const openEditModal = () => {
  126. if (!data) return;
  127. SheetManager.show('edit-group-modal', {
  128. payload: {
  129. settings: data.settings,
  130. members: data.settings.admin === 1 ? members?.settings : [],
  131. token,
  132. groupToken,
  133. refetch,
  134. refetchMembers
  135. }
  136. });
  137. };
  138. if (!data) return <Loading />;
  139. return (
  140. <PageWrapper>
  141. <Header
  142. label={data.settings.name}
  143. rightElement={
  144. data.settings.members_can_edit_settings === 1 || data.settings.admin === 1 ? (
  145. <TouchableOpacity style={{ padding: 6 }} onPress={openEditModal}>
  146. <EditIcon fill={Colors.DARK_BLUE} width={18} height={18} />
  147. </TouchableOpacity>
  148. ) : null
  149. }
  150. />
  151. <ScrollView
  152. showsVerticalScrollIndicator={false}
  153. style={{ flex: 1 }}
  154. contentContainerStyle={{
  155. paddingBottom: insets.bottom,
  156. justifyContent: 'space-between',
  157. flex: 1
  158. }}
  159. >
  160. <View style={{ gap: 16 }}>
  161. <View style={styles.photoContainer}>
  162. <TouchableOpacity
  163. style={styles.photoContainer}
  164. onPress={() => setFullSizeImageVisible(true)}
  165. disabled={!data.settings.avatar}
  166. >
  167. {!data.settings.avatar ? (
  168. <GroupIcon width={80} height={80} fill={Colors.LIGHT_GRAY} />
  169. ) : (
  170. <Image
  171. source={{ uri: API_HOST + data.settings.avatar }}
  172. style={{
  173. width: 80,
  174. height: 80,
  175. borderRadius: 40,
  176. borderWidth: 1,
  177. borderColor: Colors.FILL_LIGHT
  178. }}
  179. />
  180. )}
  181. </TouchableOpacity>
  182. <Text style={styles.bigText}>{data.settings.name}</Text>
  183. <Text style={{ fontSize: getFontSize(12), fontWeight: '600', color: Colors.DARK_BLUE }}>
  184. {data.settings.member_count} nomads
  185. </Text>
  186. </View>
  187. {data.settings.description && (
  188. <Input
  189. editable={false}
  190. value={data.settings.description}
  191. header="Description"
  192. multiline
  193. height={58}
  194. />
  195. )}
  196. {canSeeMembers && members ? (
  197. <View style={{ gap: 8, marginBottom: 16 }}>
  198. <View
  199. style={{
  200. flexDirection: 'row',
  201. justifyContent: 'space-between',
  202. alignItems: 'center'
  203. }}
  204. >
  205. <Text style={styles.title}>{members.settings.length} nomads</Text>
  206. {data.settings.members_can_add_new_members === 1 || data.settings.admin === 1 ? (
  207. <TouchableOpacity style={{ padding: 6, paddingRight: 0 }}>
  208. <UserPlusIcon fill={Colors.ORANGE} height={18} width={23} />
  209. </TouchableOpacity>
  210. ) : null}
  211. </View>
  212. <View style={{ gap: 6 }}>
  213. {(data.settings.member_count > 4
  214. ? members.settings.slice(0, 4)
  215. : members.settings
  216. ).map((member, index) => (
  217. <TouchableOpacity key={index} style={styles.userItem} onPress={() => {}}>
  218. {member.avatar ? (
  219. <Image source={{ uri: API_HOST + member.avatar }} style={styles.avatar} />
  220. ) : (
  221. <AvatarWithInitials
  222. text={`${member.name?.split(' ')[0][0]}${member.name?.split(' ')[1][0]}`}
  223. flag={API_HOST + member?.flag}
  224. size={36}
  225. fontSize={16}
  226. borderColor={Colors.LIGHT_GRAY}
  227. borderWidth={1}
  228. />
  229. )}
  230. <View style={{ flex: 1 }}>
  231. <Text style={styles.userName}>{member.name}</Text>
  232. </View>
  233. <View style={{ justifyContent: 'center', alignItems: 'center' }}>
  234. {member.admin === 1 && (
  235. <Text
  236. style={{
  237. fontSize: getFontSize(10),
  238. fontWeight: '600',
  239. color: Colors.LIGHT_GRAY
  240. }}
  241. >
  242. Admin
  243. </Text>
  244. )}
  245. </View>
  246. </TouchableOpacity>
  247. ))}
  248. {data.settings.member_count > 4 ? (
  249. <TouchableOpacity style={{ padding: 8, alignItems: 'center' }}>
  250. <Text
  251. style={{
  252. color: Colors.DARK_BLUE,
  253. fontSize: getFontSize(12),
  254. fontWeight: '700'
  255. }}
  256. >
  257. All nomads...
  258. </Text>
  259. </TouchableOpacity>
  260. ) : null}
  261. </View>
  262. </View>
  263. ) : null}
  264. </View>
  265. <View style={{ gap: 16 }}>
  266. <View style={styles.optionsContainer}>
  267. <TouchableOpacity style={styles.option} onPress={handleMute}>
  268. <Text style={styles.optionText}>{muted ? 'Unmute' : 'Mute'}</Text>
  269. <BellSlashIcon fill={Colors.DARK_BLUE} />
  270. </TouchableOpacity>
  271. </View>
  272. <View style={[styles.optionsContainer, { paddingVertical: 0, gap: 0 }]}>
  273. <TouchableOpacity
  274. style={[styles.option, styles.dangerOption]}
  275. onPress={handleLeaveGroup}
  276. >
  277. <Text style={[styles.optionText, styles.dangerText]}>Leave group chat</Text>
  278. <ExitIcon fill={Colors.RED} width={16} />
  279. </TouchableOpacity>
  280. <TouchableOpacity
  281. style={[styles.option, styles.dangerOption]}
  282. onPress={handleDeleteGroup}
  283. >
  284. <Text style={[styles.optionText, styles.dangerText]}>Delete group chat</Text>
  285. <TrashIcon fill={Colors.RED} width={18} height={18} />
  286. </TouchableOpacity>
  287. </View>
  288. </View>
  289. </ScrollView>
  290. <WarningModal
  291. type={'delete'}
  292. isVisible={modalState.isWarningVisible}
  293. buttonTitle={modalState.buttonTitle}
  294. message={modalState.message}
  295. action={modalState.action}
  296. onClose={() => setModalState({ ...modalState, isWarningVisible: false })}
  297. title={modalState.title}
  298. />
  299. <ImageView
  300. images={[{ uri: API_HOST + data.settings.avatar_full }]}
  301. keyExtractor={(imageSrc, index) => index.toString()}
  302. imageIndex={0}
  303. visible={fullSizeImageVisible}
  304. onRequestClose={() => setFullSizeImageVisible(false)}
  305. swipeToCloseEnabled={false}
  306. backgroundColor={Colors.DARK_BLUE}
  307. doubleTapToZoomEnabled={true}
  308. />
  309. <EditGroupModal />
  310. </PageWrapper>
  311. );
  312. };
  313. const styles = StyleSheet.create({
  314. photoContainer: {
  315. alignItems: 'center',
  316. gap: 8
  317. },
  318. groupPhoto: {
  319. width: 80,
  320. height: 80,
  321. borderRadius: 40,
  322. alignItems: 'center',
  323. justifyContent: 'center'
  324. },
  325. bigText: {
  326. color: Colors.DARK_BLUE,
  327. fontSize: getFontSize(18),
  328. fontFamily: 'montserrat-700'
  329. },
  330. userItem: {
  331. flexDirection: 'row',
  332. alignItems: 'center',
  333. paddingVertical: 8,
  334. paddingHorizontal: 12,
  335. backgroundColor: Colors.FILL_LIGHT,
  336. gap: 8,
  337. borderRadius: 8
  338. },
  339. avatar: {
  340. width: 36,
  341. height: 36,
  342. borderRadius: 18,
  343. borderWidth: 1,
  344. borderColor: Colors.LIGHT_GRAY
  345. },
  346. userName: {
  347. color: Colors.DARK_BLUE,
  348. fontSize: getFontSize(14),
  349. fontFamily: 'montserrat-700'
  350. },
  351. title: {
  352. color: Colors.DARK_BLUE,
  353. fontSize: getFontSize(14),
  354. fontFamily: 'redhat-700'
  355. },
  356. optionsContainer: {
  357. paddingVertical: 10,
  358. paddingHorizontal: 8,
  359. gap: 16,
  360. borderRadius: 8,
  361. backgroundColor: Colors.FILL_LIGHT
  362. },
  363. option: {
  364. flexDirection: 'row',
  365. alignItems: 'center',
  366. justifyContent: 'space-between'
  367. },
  368. optionText: {
  369. fontSize: getFontSize(12),
  370. fontWeight: '600',
  371. color: Colors.DARK_BLUE
  372. },
  373. dangerOption: {
  374. paddingVertical: 10,
  375. borderBottomWidth: 1,
  376. borderBlockColor: Colors.WHITE
  377. },
  378. dangerText: {
  379. color: Colors.RED
  380. }
  381. });
  382. export default GroupSettingScreen;