From 067b0abb3507fe9c7052a8b026c24aa556fb14e2 Mon Sep 17 00:00:00 2001 From: Monalisha Mishra Date: Thu, 6 Jun 2024 18:41:49 +0530 Subject: [PATCH] fix: added new function to check includes --- .../chat/ChatPreviewList/ChatPreviewList.tsx | 11 +++++++++-- .../ChatPreviewSearchList/ChatPreviewSearchList.tsx | 4 ++-- .../lib/components/chat/ChatProfile/ChatProfile.tsx | 5 +++-- .../lib/components/chat/MessageInput/MessageInput.tsx | 4 ++-- .../uiweb/src/lib/components/chat/helpers/helper.ts | 4 ++-- packages/uiweb/src/lib/helpers/chat/search.ts | 3 ++- packages/uiweb/src/lib/helpers/utils.ts | 4 +++- 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/uiweb/src/lib/components/chat/ChatPreviewList/ChatPreviewList.tsx b/packages/uiweb/src/lib/components/chat/ChatPreviewList/ChatPreviewList.tsx index 5d16295de..a31fe7d39 100644 --- a/packages/uiweb/src/lib/components/chat/ChatPreviewList/ChatPreviewList.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatPreviewList/ChatPreviewList.tsx @@ -7,7 +7,14 @@ import { CONSTANTS, IFeeds, IUser } from '@pushprotocol/restapi'; import styled from 'styled-components'; // Internal Compoonents -import { getAddress, getNewChatUser, pCAIP10ToWallet, traceStackCalls, walletToPCAIP10 } from '../../../helpers'; +import { + getAddress, + getDomainIfExists, + getNewChatUser, + pCAIP10ToWallet, + traceStackCalls, + walletToPCAIP10, +} from '../../../helpers'; import { useChatData, usePushChatStream } from '../../../hooks'; import useFetchChat from '../../../hooks/chat/useFetchChat'; import useFetchMessageUtilities from '../../../hooks/chat/useFetchMessageUtilities'; @@ -609,7 +616,7 @@ export const ChatPreviewList: React.FC = (options: IChatP let userProfile: IUser | undefined = undefined; let groupProfile: Group; - if (formattedChatId.includes('.')) { + if (getDomainIfExists(formattedChatId)) { const address = await getAddress(formattedChatId, user ? user.env : CONSTANTS.ENV.PROD); if (address) formattedChatId = pCAIP10ToWallet(address); else { diff --git a/packages/uiweb/src/lib/components/chat/ChatPreviewSearchList/ChatPreviewSearchList.tsx b/packages/uiweb/src/lib/components/chat/ChatPreviewSearchList/ChatPreviewSearchList.tsx index e067d2cbe..3369166a0 100644 --- a/packages/uiweb/src/lib/components/chat/ChatPreviewSearchList/ChatPreviewSearchList.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatPreviewSearchList/ChatPreviewSearchList.tsx @@ -5,7 +5,7 @@ import React, { useContext, useEffect, useRef, useState } from 'react'; import styled from 'styled-components'; // Internal Compoonents -import { deriveChatId, pCAIP10ToWallet } from '../../../helpers'; +import { deriveChatId, getDomainIfExists, pCAIP10ToWallet } from '../../../helpers'; import { useChatData } from '../../../hooks'; import useFetchChat from '../../../hooks/chat/useFetchChat'; import useGetGroupByIDnew from '../../../hooks/chat/useGetGroupByIDnew'; @@ -172,7 +172,7 @@ export const ChatPreviewSearchList: React.FC = (opt let derivedChatId = formattedChatId; // Check if the chatId is ENS / Web3 Name - if (formattedChatId.includes('.')) { + if (getDomainIfExists(formattedChatId)) { // resolve web3 name derivedChatId = await deriveChatId(formattedChatId, user); } diff --git a/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx b/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx index bc78e8065..4237895d1 100644 --- a/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx @@ -12,6 +12,7 @@ import styled from 'styled-components'; import { deriveChatId, getAddress, + getDomainIfExists, pCAIP10ToWallet, resolveWeb3Name, shortenText, @@ -166,7 +167,7 @@ export const ChatProfile: React.FC = ({ profile.abbrRecipient = getAbbreiatedRecipient(recipient); profile.desc = profileInfo.profile?.desc; profile.isGroup = false; - profile.web3Name = chatId.includes('.') ? chatId : null; + profile.web3Name = getDomainIfExists(chatId); } else { throw new Error( 'UIWeb::ChatProfile::user.profile.info fetch error, possible push user does not exist.' @@ -181,7 +182,7 @@ export const ChatProfile: React.FC = ({ profile.icon = null; profile.chatId = derivedChatId; profile.recipient = recipient; - profile.web3Name = chatId.includes('.') ? chatId : null; + profile.web3Name = getDomainIfExists(chatId); profile.abbrRecipient = getAbbreiatedRecipient(recipient); profile.desc = ''; profile.isGroup = false; diff --git a/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx b/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx index 2c6a75eb6..104d6f28a 100644 --- a/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx +++ b/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx @@ -6,7 +6,7 @@ import { createPortal } from 'react-dom'; import { MdCheckCircle, MdError } from 'react-icons/md'; import styled from 'styled-components'; -import { deriveChatId, pCAIP10ToWallet, setAccessControl, walletToPCAIP10 } from '../../../helpers'; +import { deriveChatId, getDomainIfExists, pCAIP10ToWallet, setAccessControl, walletToPCAIP10 } from '../../../helpers'; import { useChatData, useClickAway, useDeviceWidthCheck, usePushChatStream } from '../../../hooks'; import useFetchChat from '../../../hooks/chat/useFetchChat'; import useGetGroupByIDnew from '../../../hooks/chat/useGetGroupByIDnew'; @@ -163,7 +163,7 @@ export const MessageInput: React.FC = ({ if (!user) return; if (chatId) { let derivedChatId = chatId; - if (derivedChatId.includes('.')) { + if (getDomainIfExists(derivedChatId)) { derivedChatId = (await deriveChatId(chatId, user))!; } diff --git a/packages/uiweb/src/lib/components/chat/helpers/helper.ts b/packages/uiweb/src/lib/components/chat/helpers/helper.ts index 7e0d165d7..d03f7e3ac 100644 --- a/packages/uiweb/src/lib/components/chat/helpers/helper.ts +++ b/packages/uiweb/src/lib/components/chat/helpers/helper.ts @@ -2,7 +2,7 @@ import { Env, IFeeds, IMessageIPFSWithCID, IUser, ParticipantStatus } from '@pus import { ethers } from 'ethers'; import moment from 'moment'; import { ProfilePicture } from '../../../config'; -import { getAddress, walletToPCAIP10 } from '../../../helpers'; +import { getAddress, getDomainIfExists, walletToPCAIP10 } from '../../../helpers'; import { Group, IChatPreviewPayload, IMessagePayload, User } from '../exportedTypes'; export const profilePicture = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvklEQVR4AcXBsW2FMBiF0Y8r3GQb6jeBxRauYRpo4yGQkMd4A7kg7Z/GUfSKe8703fKDkTATZsJsrr0RlZSJ9r4RLayMvLmJjnQS1d6IhJkwE2bT13U/DBzp5BN73xgRZsJMmM1HOolqb/yWiWpvjJSUiRZWopIykTATZsJs5g+1N6KSMiO1N/5DmAkzYTa9Lh6MhJkwE2ZzSZlo7xvRwson3txERzqJhJkwE2bT6+JhoKTMJ2pvjAgzYSbMfgDlXixqjH6gRgAAAABJRU5ErkJggg==`; @@ -228,5 +228,5 @@ export const transformStreamToIMessageIPFSWithCID: (item: any) => IMessageIPFSWi }; export const getChatParticipantDisplayName = (derivedChatId: string, chatId: string) => { - return derivedChatId ? (chatId.includes('.') ? chatId : derivedChatId) : derivedChatId; + return derivedChatId ? getDomainIfExists(chatId) ?? derivedChatId : derivedChatId; }; diff --git a/packages/uiweb/src/lib/helpers/chat/search.ts b/packages/uiweb/src/lib/helpers/chat/search.ts index 36b73ac7c..f9eab5b15 100644 --- a/packages/uiweb/src/lib/helpers/chat/search.ts +++ b/packages/uiweb/src/lib/helpers/chat/search.ts @@ -5,6 +5,7 @@ import { pCAIP10ToWallet, walletToPCAIP10 } from '../address'; import { getUdResolverClient } from '../udResolver'; import { displayDefaultUser } from './user'; import { createWeb3Name } from '@web3-name-sdk/core'; +import { getDomainIfExists } from '../utils'; export const getObjectsWithMatchingKeys = ( obj: ChatFeedsType, @@ -61,7 +62,7 @@ export const getAddress = async (searchText: string, env: Env) => { const udResolverClient = getUdResolverClient(env); const web3NameClient = createWeb3Name(); let address: string | null = null; - if (searchText.includes('.')) { + if (getDomainIfExists(searchText)) { try { address = await web3NameClient.getAddress(searchText); if (!address) { diff --git a/packages/uiweb/src/lib/helpers/utils.ts b/packages/uiweb/src/lib/helpers/utils.ts index 8cd145e13..f865e5567 100644 --- a/packages/uiweb/src/lib/helpers/utils.ts +++ b/packages/uiweb/src/lib/helpers/utils.ts @@ -28,7 +28,7 @@ export const deriveChatId = async (chatId: string, user: PushAPI | undefined): P } else if (chatId.includes('eip155:')) { // remove eip155: chatId = chatId.replace('eip155:', ''); - } else if (chatId.includes('.')) { + } else if (getDomainIfExists(chatId)) { chatId = (await getAddress(chatId, user ? user.env : CONSTANTS.ENV.PROD))!; } @@ -40,3 +40,5 @@ export const isMessageEncrypted = (message: string) => { return message.startsWith('U2FsdGVkX1'); }; + +export const getDomainIfExists = (chatId: string) => (chatId.includes('.') ? chatId : null);