UNPKG

softchatjs-react-native

Version:

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

201 lines (200 loc) 7.23 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/index.ts var utils_exports = {}; __export(utils_exports, { convertToMinutes: () => convertToMinutes, formatConversationTime: () => formatConversationTime, formatMessageTime: () => formatMessageTime, generateConversationId: () => generateConversationId, generateFillerTimestamps: () => generateFillerTimestamps, generateId: () => generateId, getConversationTitle: () => getConversationTitle, getParticipant: () => getParticipant, getQuotedMessage: () => getQuotedMessage, getRandomColor: () => getRandomColor, getUnreadMessageIds: () => getUnreadMessageIds, getUserInfoWithId: () => getUserInfoWithId, getUsernameInitials: () => getUsernameInitials, restructureMessages: () => restructureMessages, stopPropagation: () => stopPropagation, truncate: () => truncate }); module.exports = __toCommonJS(utils_exports); var import_moment = __toESM(require("moment")); var import_softchatjs_core = require("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 (0, import_moment.default)(new Date(time)).format("hh:mm a"); } function formatConversationTime(time) { const now = (0, import_moment.default)(); const then = (0, import_moment.default)(time); const duration = import_moment.default.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 === import_softchatjs_core.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 = (0, import_moment.default)(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; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { convertToMinutes, formatConversationTime, formatMessageTime, generateConversationId, generateFillerTimestamps, generateId, getConversationTitle, getParticipant, getQuotedMessage, getRandomColor, getUnreadMessageIds, getUserInfoWithId, getUsernameInitials, restructureMessages, stopPropagation, truncate }); //# sourceMappingURL=index.js.map