Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes ui representation for domain resolution #1336

Merged
merged 8 commits into from
Jun 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -609,7 +616,7 @@ export const ChatPreviewList: React.FC<IChatPreviewListProps> = (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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
// React + Web3 Essentials
import { ethers } from 'ethers';
import React, { useContext, useEffect, useRef, useState } from 'react';

// External Packages
import { CONSTANTS, IFeeds, IUser } from '@pushprotocol/restapi';
import styled from 'styled-components';

// Internal Compoonents
import {
deriveChatId,
getAddress,
getNewChatUser,
pCAIP10ToWallet,
traceStackCalls,
walletToPCAIP10,
} from '../../../helpers';
import { useChatData, usePushChatStream } from '../../../hooks';
import { deriveChatId, getDomainIfExists, pCAIP10ToWallet } from '../../../helpers';
import { useChatData } from '../../../hooks';
import useFetchChat from '../../../hooks/chat/useFetchChat';
import useFetchMessageUtilities from '../../../hooks/chat/useFetchMessageUtilities';
import useGetGroupByIDnew from '../../../hooks/chat/useGetGroupByIDnew';
import usePushUser from '../../../hooks/usePushUser';
import { Button, Section, Span, Spinner } from '../../reusables';
import { ChatPreview } from '../ChatPreview';
import {
displayDefaultUser,
generateRandomNonce,
transformChatItems,
transformStreamToIChatPreviewPayload,
} from '../helpers';
import { generateRandomNonce, getChatParticipantDisplayName, transformStreamToIChatPreviewPayload } from '../helpers';

// Internal Configs
import { ThemeContext } from '../theme/ThemeProvider';
Expand All @@ -37,7 +21,6 @@ import { ThemeContext } from '../theme/ThemeProvider';
// Interfaces & Types
import {
ChatPreviewSearchListErrorCodes,
Group,
IChatPreviewSearchListError,
IChatPreviewSearchListProps,
IChatPreviewPayload,
Expand Down Expand Up @@ -73,7 +56,6 @@ interface IChatPreviewListMeta {
}

// Constants
const CHAT_PAGE_LIMIT = 10;
const SCROLL_LIMIT = 25;

// Exported Interfaces & Types
Expand All @@ -82,9 +64,7 @@ const SCROLL_LIMIT = 25;
export const ChatPreviewSearchList: React.FC<IChatPreviewSearchListProps> = (options: IChatPreviewSearchListProps) => {
// get hooks
const { user } = useChatData();
const { fetchUserProfile } = usePushUser();
const { getGroupByIDnew } = useGetGroupByIDnew();
const { fetchLatestMessage, fetchChatList } = useFetchMessageUtilities();

// set chat preview list
const [chatPreviewList, setChatPreviewList] = useState<IChatPreviewList>({
Expand Down Expand Up @@ -192,7 +172,7 @@ export const ChatPreviewSearchList: React.FC<IChatPreviewSearchListProps> = (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);
}
Expand All @@ -211,7 +191,6 @@ export const ChatPreviewSearchList: React.FC<IChatPreviewSearchListProps> = (opt
const groupInfo = await getGroupByIDnew({
groupId: derivedChatId,
});

if (groupInfo) {
searchedChat = {
...searchedChat,
Expand All @@ -230,19 +209,17 @@ export const ChatPreviewSearchList: React.FC<IChatPreviewSearchListProps> = (opt
} else {
const userProfile = await user?.info({ overrideAccount: chatInfo.recipient });
console.debug('UIWeb::components::ChatPreviewSearchList::loadMoreChats::userProfile', userProfile);

searchedChat = {
...searchedChat,
chatId: derivedChatId,
chatParticipant: derivedChatId,
chatParticipant: getChatParticipantDisplayName(derivedChatId, formattedChatId),
chatGroup: false,
chatPic: userProfile?.profile?.picture || null,
chatMsg: {
messageType: 'Text',
messageContent: chatInfo?.list === 'CHATS' ? 'Resume Chat!' : 'Start Chat!',
},
};

resolved = true;
}
} else {
Expand Down Expand Up @@ -349,22 +326,6 @@ export const ChatPreviewSearchList: React.FC<IChatPreviewSearchListProps> = (opt
});
};

//Transform group creation stream
const transformGroupCreationStream: (item: any) => void = async (item: any) => {
const transformedItem: IChatPreviewPayload = {
chatId: item?.chatId,
chatPic: item?.meta.image,
chatParticipant: item?.meta.name,
chatGroup: true,
chatTimestamp: undefined,
chatMsg: {
messageType: '',
messageContent: '',
},
};
addChatItems([transformedItem], false);
};

// Transform stream message
const transformStreamMessage: (item: any) => void = async (item: any) => {
if (!user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import styled from 'styled-components';
import {
deriveChatId,
getAddress,
getDomainIfExists,
pCAIP10ToWallet,
resolveWeb3Name,
shortenText,
Expand Down Expand Up @@ -166,6 +167,7 @@ export const ChatProfile: React.FC<IChatProfile> = ({
profile.abbrRecipient = getAbbreiatedRecipient(recipient);
profile.desc = profileInfo.profile?.desc;
profile.isGroup = false;
profile.web3Name = getDomainIfExists(chatId);
} else {
throw new Error(
'UIWeb::ChatProfile::user.profile.info fetch error, possible push user does not exist.'
Expand All @@ -180,13 +182,14 @@ export const ChatProfile: React.FC<IChatProfile> = ({
profile.icon = null;
profile.chatId = derivedChatId;
profile.recipient = recipient;
profile.web3Name = getDomainIfExists(chatId);
profile.abbrRecipient = getAbbreiatedRecipient(recipient);
profile.desc = '';
profile.isGroup = false;
}

// get and set web3 name asynchrounously
if (profile.recipient) {
if (profile.recipient && !profile.web3Name) {
setupWeb3Name(profile.recipient);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -163,7 +163,7 @@ export const MessageInput: React.FC<MessageInputProps> = ({
if (!user) return;
if (chatId) {
let derivedChatId = chatId;
if (derivedChatId.includes('.')) {
if (getDomainIfExists(derivedChatId)) {
derivedChatId = (await deriveChatId(chatId, user))!;
}

Expand Down
8 changes: 5 additions & 3 deletions packages/uiweb/src/lib/components/chat/helpers/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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==`;
Expand Down Expand Up @@ -108,8 +108,6 @@ export const formatAddress = async (chatPreviewPayload: IChatPreviewPayload, env
// check and remove eip155:
if (formattedAddress.includes('eip155:')) {
formattedAddress = formattedAddress.replace('eip155:', '');
} else if (formattedAddress.includes('.')) {
formattedAddress = (await getAddress(formattedAddress, env))!;
}
}

Expand Down Expand Up @@ -228,3 +226,7 @@ export const transformStreamToIMessageIPFSWithCID: (item: any) => IMessageIPFSWi
};
return transformedItem;
};

export const getChatParticipantDisplayName = (derivedChatId: string, chatId: string) => {
return derivedChatId ? getDomainIfExists(chatId) ?? derivedChatId : derivedChatId;
};
3 changes: 2 additions & 1 deletion packages/uiweb/src/lib/helpers/chat/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion packages/uiweb/src/lib/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))!;
}

Expand All @@ -40,3 +40,5 @@ export const isMessageEncrypted = (message: string) => {

return message.startsWith('U2FsdGVkX1');
};

export const getDomainIfExists = (chatId: string) => (chatId.includes('.') ? chatId : null);
Loading