UNPKG

softchatjs-react-native

Version:

React native UI SDK for softchatjs-core. Create a free account at: https://www.softchatjs.com

335 lines (324 loc) 9.2 kB
// src/components/Conversations/ConversationList.tsx import React4 from "react"; import { TouchableOpacity, View as View2, StyleSheet as StyleSheet2, Text as Text2, FlatList } from "react-native"; import { useRef } from "react"; // src/utils/index.ts import moment from "moment"; import { MessageStates } from "softchatjs-core"; var getUserInfoWithId = (userId, participantList) => { let presentUser = participantList.find((participant) => participant.participantId === userId); let otherParticipants = participantList.filter((participant) => participant.participantId !== userId); return { presentUser: presentUser?.participantDetails, receivingUser: otherParticipants[0]?.participantDetails }; }; var truncate = (str, len) => { return str.length > len ? str.substring(0, len) + "..." : str; }; var getConversationTitle = (userId, converstaion) => { if (converstaion.conversationType !== "group-chat") { const userInfos = getUserInfoWithId(userId, converstaion.participantList); const firstname = userInfos.receivingUser?.firstname; const username = userInfos.receivingUser?.username; return firstname ? firstname : username; } return converstaion.groupMeta?.groupName || "no-groupname"; }; function formatMessageTime(time) { return moment(new Date(time)).format("hh:mm a"); } // src/constants/Colors.ts var Colors = { greyLighter: "#F0F0F0" }; // src/theme/colors.ts var teal = { 50: "#DCF2F0", 100: "#A9DFD8", 200: "#73CABE", 300: "#3AB4A4", 400: "#00A391", 500: "#00927E", 600: "#008572", 700: "#007662", 800: "#006654", 900: "#004A38" }; var green = { 50: "#E6F5E4", 100: "#C2E6BD", 200: "#9AD693", 300: "#6FC666", 400: "#4ABA42", 500: "#17AE13", 600: "#029F04", 700: "#008D00", 800: "#007C00", 900: "#005E00" }; var grey = { 50: "#F6F6FF", 100: "#F2F1FF", 200: "#EBEBF9", 300: "#CAC9D7", 400: "#ACACB9", 500: "#82818F", 600: "#6D6D7A", 700: "#4D4D59", 800: "#2B2B36", 900: "#21222D", A100: "#1D1E26" }; var stone = { 50: "#F8F8F8", 100: "#EFEFEF", 200: "#E8E8E8", 300: "#D9D9D9", 400: "#D2D2D2", 500: "#A0A0A0", 600: "#87888C", 700: "#2C2D33", 800: "#1D1E26", 900: "#171821" }; // src/contexts/ChatProvider.tsx import React3, { createContext as createContext3, useContext as useContext3 } from "react"; // src/contexts/ModalProvider.tsx import React, { createContext, useContext, useState } from "react"; import { Modal, View } from "react-native"; var initial = { displayModal: () => { }, resetModal: () => { }, modalProps: { dismissable: true, justifyContent: "center", children: null, animation: "slide", containerWidth: "100%" } }; var ModalProviderContext = createContext(initial); // src/theme/index.ts var theme = { background: { primary: stone[900], secondary: grey[900], disabled: grey[800] }, text: { primary: "black", secondary: stone[200], disabled: stone[500] }, action: { primary: teal[50], secondary: stone[300] }, chatBubble: { left: { bgColor: grey[900], messageColor: stone[200], messageTimeColor: "grey", replyBorderColor: stone[200] }, right: { bgColor: "#474952", messageColor: "white", messageTimeColor: "grey", replyBorderColor: green[900] } }, icon: "white", divider: stone[700] }; var theme_default = theme; // src/contexts/MessageStateContext.tsx import React2, { createContext as createContext2, useState as useState2, useContext as useContext2 } from "react"; import { Audio } from "expo-av"; // src/constants/defaultUser.ts var defaultUser_default = { id: "", uid: "", username: "", firstname: "", lastname: "", profileUrl: "", color: "", custom: {} }; // src/contexts/MessageStateContext.tsx var initialMessageStateContext = { globalTextMessage: "", setGlobalTextMessage: () => { }, stickers: [], setStickers: () => { }, pendingMessages: [], addNewPendingMessages: (message) => { }, removePendingMessage: (messageId) => { }, updatePendingMessage: (messageId, message) => { }, playVoiceMessage: (media) => { }, pauseVoiceMessage: () => { }, resumeVoiceMessage: () => { }, audioState: null, unload: () => { }, sound: null, activeVoiceMessage: null, avPlayBackStatus: null, userMeta: defaultUser_default, setUserMeta: () => { }, conversationList: [], setConversationList: () => { } }; var MessageStateContext = createContext2( initialMessageStateContext ); // src/contexts/ChatProvider.tsx var ConfigContext = createContext3({ theme: theme_default, client: null, fontFamily: void 0, fontScale: 1 }); function useConfig() { return useContext3(ConfigContext); } // src/components/Conversations/ConversationList.tsx import { Image } from "expo-image"; var avatarSize = 50; var ConversationAvatar = ({ type, chatUserId, participantList, groupMeta, conversationTitle, fontFamily }) => { if (type === "private-chat") { const userInfo = getUserInfoWithId(chatUserId, participantList); if (userInfo.presentUser?.profileUrl) { return /* @__PURE__ */ React4.createElement(Image, { source: { uri: userInfo.presentUser.profileUrl }, cachePolicy: "disk", style: styles.avatar }); } else { return /* @__PURE__ */ React4.createElement(View2, { style: styles.avatar }, /* @__PURE__ */ React4.createElement(Text2, { style: { ...styles.avatarInitials, fontFamily } }, conversationTitle ? conversationTitle.substring(0, 1) : "")); } } else { if (groupMeta?.groupIcon) { return /* @__PURE__ */ React4.createElement(Image, { source: { uri: groupMeta.groupIcon }, style: styles.avatar, cachePolicy: "disk" }); } else { return /* @__PURE__ */ React4.createElement(View2, { style: styles.avatar }, /* @__PURE__ */ React4.createElement(Text2, { style: { fontFamily } }, conversationTitle ? conversationTitle.substring(0, 1) : "")); } } }; var ConversationTitle = ({ title, fontFamily }) => { const { theme: theme2 } = useConfig(); return /* @__PURE__ */ React4.createElement(View2, null, /* @__PURE__ */ React4.createElement(Text2, { style: { ...styles.conversationTitle, fontFamily, color: theme2?.text.secondary } }, title)); }; var ListItem = (props) => { const { conversation, chatUserId, isLastItem, onPress } = props; const lastMessage = conversation.messages[conversation.messages.length - 1]; let conversationTitle = getConversationTitle(chatUserId, conversation); const { fontFamily } = useConfig(); return /* @__PURE__ */ React4.createElement( TouchableOpacity, { onPress, style: [styles.listItem, !isLastItem && { borderBottomWidth: 0.5, borderBottomColor: Colors.greyLighter }] }, /* @__PURE__ */ React4.createElement(View2, { style: { flexDirection: "row", alignItems: "center" } }, /* @__PURE__ */ React4.createElement( ConversationAvatar, { fontFamily, chatUserId, participantList: conversation.participantList, type: conversation.conversationType, groupMeta: conversation.groupMeta, conversationTitle } ), /* @__PURE__ */ React4.createElement(View2, { style: { maxWidth: "80%" } }, conversationTitle && /* @__PURE__ */ React4.createElement(ConversationTitle, { fontFamily, title: conversationTitle }), /* @__PURE__ */ React4.createElement(Text2, { style: { ...styles.messageText, fontFamily } }, truncate(lastMessage.message, 45)))), /* @__PURE__ */ React4.createElement(Text2, { style: { ...styles.messageTime, fontFamily } }, formatMessageTime(lastMessage.createdAt)) ); }; function ConversationList(props) { const { conversations, chatUserId, onOpen } = props; const flatListRef = useRef(null); return /* @__PURE__ */ React4.createElement(View2, { style: styles.main }, /* @__PURE__ */ React4.createElement( FlatList, { ref: flatListRef, data: conversations, renderItem: ({ item, index }) => /* @__PURE__ */ React4.createElement( ListItem, { onPress: () => onOpen(), chatUserId, key: index, conversation: item, isLastItem: conversations.length === index + 1 } ) } )); } var styles = StyleSheet2.create({ main: { height: "100%", width: "100%" }, conversationTitle: { fontSize: 20, textTransform: "capitalize" }, messageText: { color: "black", fontSize: 17 }, messageTime: {}, avatar: { height: avatarSize, width: avatarSize, borderRadius: avatarSize, backgroundColor: Colors.greyLighter, alignItems: "center", justifyContent: "center", marginEnd: 10 }, avatarInitials: { fontSize: 30, textTransform: "capitalize", color: "white" }, listItem: { height: 80, width: "100%", paddingHorizontal: 10, flexDirection: "row", alignItems: "center", justifyContent: "space-between", backgroundColor: stone[600] // backgroundColor: 'lightgrey', } }); export { ConversationTitle, ListItem, ConversationList as default }; //# sourceMappingURL=ConversationList.mjs.map