UNPKG

softchatjs-react-native

Version:

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

152 lines 4.79 kB
// src/utils/index.ts import moment from "moment"; import { MessageStates } from "softchatjs-core"; function generateConversationId(str1, str2) { const sortedStrings = [str1, str2].sort(); const combinedString = sortedStrings.join("_"); const hash = hashCode(combinedString); return hash.toString(); } function hashCode(str) { let hash = 0; if (str.length == 0) { return hash; } for (let i = 0; i < str.length; i++) { let char = str.charCodeAt(i); hash = (hash << 5) - hash + char; hash = hash & hash; } return hash; } var generateId = () => { let uuid = ""; const characters = "abcdef0123456789"; for (let i = 0; i < 32; i++) { const randomNumber = Math.floor(Math.random() * characters.length); const character = characters.charAt(randomNumber); if (i === 8 || i === 12 || i === 16 || i === 20) { uuid += "-"; } uuid += character; } return uuid; }; 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"; }; var getUsernameInitials = (username) => { return username.substring(0, 1); }; function formatMessageTime(time) { return moment(new Date(time)).format("hh:mm a"); } function formatConversationTime(time) { const now = moment(); const then = moment(time); const duration = moment.duration(now.diff(then)); const years = Math.floor(duration.asYears()); if (years > 0) return years + "yr"; const months = Math.floor(duration.asMonths()); if (months > 0) return months + "mo"; const weeks = Math.floor(duration.asWeeks()); if (weeks > 0) return weeks + "w"; const days = Math.floor(duration.asDays()); if (days > 0) return days + "d"; const hours = Math.floor(duration.asHours()); if (hours > 0) return hours + "h"; const minutes = Math.floor(duration.asMinutes()); if (minutes > 0) return minutes + "m"; return "Just now"; } var generateFillerTimestamps = () => { return { createdAt: /* @__PURE__ */ new Date(), updatedAt: /* @__PURE__ */ new Date() }; }; var getUnreadMessageIds = (conversation, userId) => { var ids = []; conversation.messages.map((m) => { if (m.messageState === MessageStates.SENT && m.from !== userId) { ids.push(m.messageId); } }); return ids; }; var getQuotedMessage = (messageId, messages) => { const message = messages.find((msg) => msg.messageId === messageId); return message; }; var stopPropagation = (event) => { event.stopPropagation(); }; var getRandomColor = () => { const letters = "0123456789ABCDEF"; let color = "#"; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; }; var getParticipant = (uid, participantList) => { return participantList.find((p) => p.participantDetails.uid === uid); }; function convertToMinutes(seconds) { var _seconds = Number(seconds.toFixed(0)); const minutes = Math.floor(_seconds / 60); const remainingSeconds = _seconds % 60; const paddedMinutes = String(minutes).padStart(2, "0"); const paddedSeconds = String(remainingSeconds).padStart(2, "0"); return `${paddedMinutes}:${paddedSeconds}`; } var restructureMessages = (data) => { const groupMessagesByDate = data.reduce((acc, item) => { if (typeof item !== "string") { var date = moment(item.createdAt).format("MMMM DD, YYYY"); if (acc[date]) { acc[date].unshift(item); } else { acc[date] = [item]; } } return acc; }, {}); const _messages = Object.entries(groupMessagesByDate).flatMap( ([date, messages]) => [...messages.reverse(), date] ); return _messages; }; export { convertToMinutes, formatConversationTime, formatMessageTime, generateConversationId, generateFillerTimestamps, generateId, getConversationTitle, getParticipant, getQuotedMessage, getRandomColor, getUnreadMessageIds, getUserInfoWithId, getUsernameInitials, restructureMessages, stopPropagation, truncate }; //# sourceMappingURL=index.mjs.map