diff --git a/packages/examples/sdk-frontend-react/src/app/ChatUITest/ChatViewComponent.tsx b/packages/examples/sdk-frontend-react/src/app/ChatUITest/ChatViewComponent.tsx index 82e854cce..30e0db5cc 100644 --- a/packages/examples/sdk-frontend-react/src/app/ChatUITest/ChatViewComponent.tsx +++ b/packages/examples/sdk-frontend-react/src/app/ChatUITest/ChatViewComponent.tsx @@ -9,11 +9,11 @@ const ChatViewComponentTest = () => { 'bafyreidesy6f4iu34eqccmqh55g35wu36lvlz42c63ivtmgjjhezlzdqta', 'bafyreig3gs4tpwxumiz5fxypyt4omlxhvrvuj66kfoyioeshawlau6lgem', ]; - +console.log('in chat view component') return (

Chat UI Test page

- {/* {console.log('in close')}} /> */} + {console.log('in close')}} /> {/* {console.log('in close')}} modalBackground={MODAL_BACKGROUND_TYPE.OVERLAY} modalPositionType={MODAL_POSITION_TYPE.RELATIVE}/> */} diff --git a/packages/uiweb/src/lib/components/chat/ChatProfile/AddWalletContent.tsx b/packages/uiweb/src/lib/components/chat/ChatProfile/AddWalletContent.tsx index e99062b39..9ccddc723 100644 --- a/packages/uiweb/src/lib/components/chat/ChatProfile/AddWalletContent.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatProfile/AddWalletContent.tsx @@ -24,6 +24,7 @@ import CloseIcon from '../../../icons/close.svg'; import { ChatSearchInput, CustomStyleParamsType, ModalHeader } from '../reusables'; import useGetChatProfile from '../../../hooks/useGetChatProfile'; import { BackIcon } from '../../../icons/Back'; +import useChatProfile from '../../../hooks/chat/useChatProfile'; type AddWalletContentProps = { @@ -50,7 +51,7 @@ export const AddWalletContent = ({ const [filteredUserData, setFilteredUserData] = useState(null); const { account, env } = useChatData(); const isMobile = useMediaQuery(device.mobileL); - const {fetchChatProfile} = useGetChatProfile(); + const {fetchUserChatProfile} = useChatProfile(); const groupInfoToast = useToast(); const customSearchStyle:CustomStyleParamsType = { background:theme.backgroundColor?.modalInputBackground, @@ -64,7 +65,7 @@ export const AddWalletContent = ({ //fix ens search const newChatUser = await getNewChatUser({ searchText: searchedText, - fetchChatProfile, + fetchChatProfile:fetchUserChatProfile, env, }); if(newChatUser){ diff --git a/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx b/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx index e47d24135..26b6643f9 100644 --- a/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatProfile/ChatProfile.tsx @@ -68,7 +68,7 @@ export const ChatProfile: React.FC = ({ const fetchProfileData = async () => { if (isValidETHAddress(chatId)) { - const ChatProfile = await fetchUserChatProfile({ profileId: chatId }); + const ChatProfile = await fetchUserChatProfile({ profileId: chatId,env }); const result = await resolveNewEns(chatId, provider); setEnsName(result); setChatInfo(ChatProfile); diff --git a/packages/uiweb/src/lib/components/chat/ChatViewBubble/ChatViewBubble.tsx b/packages/uiweb/src/lib/components/chat/ChatViewBubble/ChatViewBubble.tsx index 55973f0d7..99ab8e81d 100644 --- a/packages/uiweb/src/lib/components/chat/ChatViewBubble/ChatViewBubble.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatViewBubble/ChatViewBubble.tsx @@ -32,7 +32,7 @@ const SenderMessageAddress = ({ chat }: { chat: IMessagePayload }) => { const theme = useContext(ThemeContext); return ( <> - {(chat.fromDID).split(':')[1] !== account && ( + {(chat.fromCAIP10).split(':')[1] !== account && ( { fontWeight={theme.fontWeight?.chatReceivedBubbleAddressText} color={theme.textColor?.chatReceivedBubbleAddressText} > - {chat.fromDID.split(':')[1].slice(0, 6)}... - {chat.fromDID.split(':')[1].slice(-6)} + {chat.fromCAIP10.split(':')[1].slice(0, 6)}... + {chat.fromCAIP10.split(':')[1].slice(-6)} )} @@ -54,7 +54,7 @@ const SenderMessageProfilePicture = ({ chat }: { chat: IMessagePayload }) => { const [pfp, setPfp] = useState(''); const getUserPfp = async () => { const pfp = await getPfp({ - account: chat.fromDID.split(':')[1], + account: chat.fromCAIP10.split(':')[1], env: env, }); if (pfp) { @@ -63,10 +63,10 @@ const SenderMessageProfilePicture = ({ chat }: { chat: IMessagePayload }) => { }; useEffect(() => { getUserPfp(); - }, [account, chat.fromDID]); + }, [account, chat.fromCAIP10]); return (
- {(chat.fromDID || chat.fromDID).split(':')[1] !== account && ( + {(chat.fromCAIP10).split(':')[1] !== account && (
{pfp && ( { const { account } = useChatData(); const position = - pCAIP10ToWallet(decryptedMessagePayload.fromDID).toLowerCase() !== account?.toLowerCase() + pCAIP10ToWallet(decryptedMessagePayload.fromCAIP10).toLowerCase() !== account?.toLowerCase() ? 0 : 1; const { tweetId, messageType }: TwitterFeedReturnType = checkTwitterUrl({ @@ -348,7 +348,7 @@ export const ChatViewBubble = ({ decryptedMessagePayload }: { decryptedMessagePa }); const [isGroup, setIsGroup] = useState(false); useEffect(() => { - if ((decryptedMessagePayload.toDID).split(':')[0] === 'eip155') { + if ((decryptedMessagePayload.toCAIP10).split(':')[0] === 'eip155') { if (isGroup) { setIsGroup(false); } @@ -357,7 +357,7 @@ export const ChatViewBubble = ({ decryptedMessagePayload }: { decryptedMessagePa setIsGroup(true); } } - }, [decryptedMessagePayload.toDID, isGroup]); + }, [decryptedMessagePayload.toCAIP10, isGroup]); if (messageType === 'TwitterFeedLink') { decryptedMessagePayload.messageType = 'TwitterFeedLink'; diff --git a/packages/uiweb/src/lib/components/chat/ChatViewList/ChatViewList.tsx b/packages/uiweb/src/lib/components/chat/ChatViewList/ChatViewList.tsx index f3ba8c041..c6fae0e36 100644 --- a/packages/uiweb/src/lib/components/chat/ChatViewList/ChatViewList.tsx +++ b/packages/uiweb/src/lib/components/chat/ChatViewList/ChatViewList.tsx @@ -34,6 +34,7 @@ import useGetChatProfile from '../../../hooks/useGetChatProfile'; import useFetchChat from '../../../hooks/chat/useFetchChat'; import { ApproveRequestBubble } from './ApproveRequestBubble'; import { formatTime } from '../../../helpers/timestamp'; +import useChatProfile from '../../../hooks/chat/useChatProfile'; /** * @interface IThemeProps @@ -64,7 +65,7 @@ export const ChatViewList: React.FC = ( const listInnerRef = useRef(null); const [isMember, setIsMember] = useState(false); const { fetchChat } = useFetchChat(); - const { fetchChatProfile } = useGetChatProfile(); + const { fetchUserChatProfile } = useChatProfile(); const { getGroup } = useGetGroup(); const { messagesSinceLastConnection, groupInformationSinceLastConnection } = @@ -86,20 +87,17 @@ export const ChatViewList: React.FC = ( useEffect(() => { (async () => { if (alias) { - // console.log('chatss'); - // console.log(account, env, 'chatss') + const chat = await fetchChat(); - // console.log(chat, 'chatss calling main', alias) if (chat) { setConversationHash(chat?.threadhash as string); setChatFeed(chat as IFeeds); } else { - console.log('chatss calling elsez') let newChatFeed; let group; const result = await getNewChatUser({ searchText: chatId, - fetchChatProfile, + fetchChatProfile:fetchUserChatProfile, env, }); if (result) { diff --git a/packages/uiweb/src/lib/components/chat/ConnectButton/ConnectButton.tsx b/packages/uiweb/src/lib/components/chat/ConnectButton/ConnectButton.tsx index fc78f01cc..6a144303f 100644 --- a/packages/uiweb/src/lib/components/chat/ConnectButton/ConnectButton.tsx +++ b/packages/uiweb/src/lib/components/chat/ConnectButton/ConnectButton.tsx @@ -12,6 +12,7 @@ import useDecryptPGPKey from '../../../hooks/useDecryptPGPKey'; import { getAddressFromSigner } from '../../../helpers'; import { IChatTheme } from '../theme'; import { device } from '../../../config'; +import useChatProfile from '../../../hooks/chat/useChatProfile'; /** * @interface IThemeProps @@ -37,7 +38,7 @@ export const ConnectButtonSub = ({autoConnect = false}) => { setAlias } = useChatData(); const theme = useContext(ThemeContext); - const {fetchChatProfile} = useGetChatProfile(); + const {fetchUserChatProfile} = useChatProfile(); const {decryptPGPKey} = useDecryptPGPKey(); @@ -61,17 +62,18 @@ export const ConnectButtonSub = ({autoConnect = false}) => { setUserData() }, [wallet]) - useEffect(() => { - (async () => { - if (!alias && signer) { - const user = await fetchChatProfile({signer: signer, env}); - console.log("calllingggg in connect button") - if (user) { - setAlias(user); - } - } - })(); - }, [alias, signer]); + //initialise user hook + + // useEffect(() => { + // (async () => { + // if (!alias && signer) { + // const user = await fetchChatProfile({signer: signer, env}); + // if (user) { + // setAlias(user); + // } + // } + // })(); + // }, [alias, signer]); return !signer ? ( diff --git a/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx b/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx index 5b4a63bef..e648de412 100644 --- a/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx +++ b/packages/uiweb/src/lib/components/chat/MessageInput/MessageInput.tsx @@ -28,7 +28,7 @@ import { setAccessControl, } from '../../../helpers'; import useFetchChat from '../../../hooks/chat/useFetchChat'; -import useGetChatProfile from '../../../hooks/useGetChatProfile'; +import useChatProfile from '../../../hooks/chat/useChatProfile'; import useGetGroup from '../../../hooks/chat/useGetGroup'; import useApproveChatRequest from '../../../hooks/chat/useApproveChatRequest'; import { @@ -120,7 +120,7 @@ export const MessageInput: React.FC = ({ setAlias } = useChatData(); const { fetchChat } = useFetchChat(); - const { fetchChatProfile } = useGetChatProfile(); + const { fetchUserChatProfile } = useChatProfile(); const { getGroup } = useGetGroup(); const statusToast = useToast(); const textAreaRef = useRef(null); @@ -142,15 +142,16 @@ export const MessageInput: React.FC = ({ }, [textAreaRef, typedMessage]); //need to do something about fetching connectedUser in every component - useEffect(() => { - (async () => { - if (!alias && signer) { - const user = await fetchChatProfile({ signer: signer, env }); - console.log("calllingggg in message input") - if (user) setAlias(user); - } - })(); - }, [alias]); + //initalization not needed here + + // useEffect(() => { + // (async () => { + // if (!alias && signer) { + // const user = await fetchUserChatProfile({ profileId: account!,env }); + // if (user) setAlias(user); + // } + // })(); + // }, [alias]); useEffect(() => { const storedTimestampJSON = localStorage.getItem(chatId); @@ -209,7 +210,7 @@ export const MessageInput: React.FC = ({ let group; const result = await getNewChatUser({ searchText: chatId, - fetchChatProfile, + fetchChatProfile:fetchUserChatProfile, env, }); if (result) { diff --git a/packages/uiweb/src/lib/context/chatContext.ts b/packages/uiweb/src/lib/context/chatContext.ts index a7ab83f83..ac635d7ab 100644 --- a/packages/uiweb/src/lib/context/chatContext.ts +++ b/packages/uiweb/src/lib/context/chatContext.ts @@ -16,8 +16,8 @@ export interface IChatDataContextValues { setIsPushChatSocketConnected: React.Dispatch>; connectedProfile: IUser | undefined; setConnectedProfile: (connectedProfile: IUser) => void; - alias: any; - setAlias: React.Dispatch>; + pushUser: any; + setPushUser: React.Dispatch>; } export const initialChatDataContextValues: IChatDataContextValues = { @@ -45,8 +45,8 @@ export const initialChatDataContextValues: IChatDataContextValues = { setConnectedProfile: () => { /** */ }, - alias: null, - setAlias: () => { + pushUser: null, + setPushUser: () => { /** */ }, diff --git a/packages/uiweb/src/lib/dataProviders/ChatDataProvider.tsx b/packages/uiweb/src/lib/dataProviders/ChatDataProvider.tsx index 4088397a8..3563701d7 100644 --- a/packages/uiweb/src/lib/dataProviders/ChatDataProvider.tsx +++ b/packages/uiweb/src/lib/dataProviders/ChatDataProvider.tsx @@ -5,18 +5,16 @@ import { IChatDataContextValues, } from '../context/chatContext'; import { ThemeContext } from '../components/chat/theme/ThemeProvider'; -import useGetChatProfile from '../hooks/useGetChatProfile'; import { IUser, SignerType } from '@pushprotocol/restapi'; import { IChatTheme, lightChatTheme } from '../components/chat/theme'; import { getAddressFromSigner, pCAIP10ToWallet } from '../helpers'; - +import useInitializePushUser from '../hooks/chat/useInitializePushUser'; export interface IChatUIProviderProps { children: ReactNode; theme?: IChatTheme; account?: string | null; signer?: SignerType | undefined; - pgpPrivateKey?: string | null; env?: ENV; } @@ -27,64 +25,57 @@ export const ChatUIProvider = ({ signer = undefined, env = Constants.ENV.PROD, }: IChatUIProviderProps) => { - const [accountVal, setAccountVal] = useState(pCAIP10ToWallet(account!)); - const [pushChatSocket, setPushChatSocket] = useState(null); - const [signerVal, setSignerVal] = useState(signer); - const [alias, setAlias] = useState(null); + const [accountVal, setAccountVal] = useState( + pCAIP10ToWallet(account!) + ); + const [pushChatSocket, setPushChatSocket] = useState(null); + const [signerVal, setSignerVal] = useState(signer); + const [pushUser, setPushUser] = useState(null); const [envVal, setEnvVal] = useState(env); - const {fetchChatProfile} = useGetChatProfile(); - const [connectedProfile,setConnectedProfile]=useState(undefined); - + const [connectedProfile, setConnectedProfile] = useState( + undefined + ); + const { initializePushUser } = useInitializePushUser(); const [isPushChatSocketConnected, setIsPushChatSocketConnected] = - useState(false); + useState(false); useEffect(() => { - (async()=>{ + (async () => { resetStates(); setEnvVal(env); - + if (signer) { if (!account) { const address = await getAddressFromSigner(signer); setAccountVal(address); - } - else{ + } else { setAccountVal(account); } - } + } setSignerVal(signer); - })() - - }, [env,account, alias,signer]) - - - - - -const resetStates = () => { - setPushChatSocket(null); - setIsPushChatSocketConnected(false); - -}; - - + })(); + }, [env, account, signer]); -useEffect(() => { + useEffect(() => { (async () => { - let user; - if (!alias && signer) { - console.log("userrr",user); - if (user) { - setAlias(user); - } + if (Object.keys(signer || {}).length && account) { + const pushUser = await initializePushUser({ signer: signer!, account }); + setPushUser(pushUser); } })(); - }, [env, signer, alias]); + }, [signer, account, env]); + + const resetStates = () => { + setPushChatSocket(null); + setIsPushChatSocketConnected(false); + }; + + const value: IChatDataContextValues = { account: accountVal, - signer:signerVal, - setSigner:setSignerVal, + signer: signerVal, + setSigner: setSignerVal, setAccount: setAccountVal, env: envVal, setEnv: setEnvVal, @@ -94,11 +85,10 @@ useEffect(() => { setIsPushChatSocketConnected, connectedProfile, setConnectedProfile, - alias, - setAlias + pushUser, + setPushUser, }; - const PROVIDER_THEME = Object.assign({}, lightChatTheme, theme); return ( @@ -107,4 +97,4 @@ useEffect(() => { ); -}; \ No newline at end of file +}; diff --git a/packages/uiweb/src/lib/helpers/chat/chat.ts b/packages/uiweb/src/lib/helpers/chat/chat.ts index 293292d34..41520228a 100644 --- a/packages/uiweb/src/lib/helpers/chat/chat.ts +++ b/packages/uiweb/src/lib/helpers/chat/chat.ts @@ -218,10 +218,10 @@ export const getChatId = ({ msg: IMessageIPFS; account: string; }) => { - if (pCAIP10ToWallet(msg.fromDID).toLowerCase() === account.toLowerCase()) { - return msg.toDID; + if (pCAIP10ToWallet(msg.fromCAIP10).toLowerCase() === account.toLowerCase()) { + return msg.toCAIP10; } - return !isPCAIP(msg.toDID) ? msg.toDID : msg.fromDID; + return !isPCAIP(msg.toCAIP10) ? msg.toCAIP10 : msg.fromCAIP10; }; export const appendUniqueMessages = ( @@ -253,19 +253,19 @@ export const checkIfSameChat = ( chatId = walletToPCAIP10(chatId); if ( Object.keys(msg || {}).length && - (((chatId.toLowerCase() === (msg.fromDID?.toLowerCase())) && + (((chatId.toLowerCase() === (msg.fromCAIP10?.toLowerCase())) && ( walletToPCAIP10(account!).toLowerCase() === - msg.toDID?.toLowerCase())) || - ((chatId.toLowerCase() === (msg.toDID?.toLowerCase())) && + msg.toCAIP10?.toLowerCase())) || + ((chatId.toLowerCase() === (msg.toCAIP10?.toLowerCase())) && (walletToPCAIP10(account!).toLowerCase() === - msg.fromDID?.toLowerCase()))) + msg.fromCAIP10?.toLowerCase()))) ) { return true; } } else { if ( Object.keys(msg || {}).length && - (chatId.toLowerCase() === msg.toDID?.toLowerCase()) + (chatId.toLowerCase() === msg.toCAIP10?.toLowerCase()) ) { return true; } diff --git a/packages/uiweb/src/lib/helpers/chat/search.ts b/packages/uiweb/src/lib/helpers/chat/search.ts index ce5448dba..7a4b3aa06 100644 --- a/packages/uiweb/src/lib/helpers/chat/search.ts +++ b/packages/uiweb/src/lib/helpers/chat/search.ts @@ -74,7 +74,7 @@ export const getNewChatUser = async ({ // address = await getAddress(searchText, env); // } if (address) { - chatProfile = await fetchChatProfile({ profileId: address, env }); + chatProfile = await fetchChatProfile({ profileId: address,env }); if (!chatProfile) chatProfile = displayDefaultUser({ caip10: walletToPCAIP10(address) }); return chatProfile; diff --git a/packages/uiweb/src/lib/helpers/timestamp.ts b/packages/uiweb/src/lib/helpers/timestamp.ts index 79f45065f..a83c81a13 100644 --- a/packages/uiweb/src/lib/helpers/timestamp.ts +++ b/packages/uiweb/src/lib/helpers/timestamp.ts @@ -1,15 +1,15 @@ -export const formatTime = (timestamp : any) => { - let date; -let timestamp1; - if (typeof timestamp === "string") { - timestamp1 = parseInt(timestamp ); - }else{ - timestamp1 = timestamp - } - const time = new Date(timestamp1!); - if (!isNaN(time.getTime())){ - const time1 = time.toLocaleTimeString('en-US'); - date = time1.slice(0, -6) + time1.slice(-2); - } - return date; -} \ No newline at end of file +export const formatTime = (timestamp: any) => { + let date; + let timestamp1; + if (typeof timestamp === 'string') { + timestamp1 = parseInt(timestamp); + } else { + timestamp1 = timestamp; + } + const time = new Date(timestamp1!); + if (!isNaN(time.getTime())) { + const time1 = time.toLocaleTimeString('en-US'); + date = time1.slice(0, -6) + time1.slice(-2); + } + return date; +}; diff --git a/packages/uiweb/src/lib/hooks/chat/index.ts b/packages/uiweb/src/lib/hooks/chat/index.ts index d497e66d6..16e3f5f39 100644 --- a/packages/uiweb/src/lib/hooks/chat/index.ts +++ b/packages/uiweb/src/lib/hooks/chat/index.ts @@ -4,7 +4,6 @@ export * from './useChatData'; export * from './useChatProfile'; export * from './usePushChatSocket'; export * from './useFetchChat'; -export * from './useFetchConversationHash'; export * from './usePushSendMessage'; export * from './useGetGroupByID'; export * from './useAccount'; diff --git a/packages/uiweb/src/lib/hooks/chat/useApproveChatRequest.ts b/packages/uiweb/src/lib/hooks/chat/useApproveChatRequest.ts index 67e360503..644d39693 100644 --- a/packages/uiweb/src/lib/hooks/chat/useApproveChatRequest.ts +++ b/packages/uiweb/src/lib/hooks/chat/useApproveChatRequest.ts @@ -9,15 +9,14 @@ interface ApproveChatParams { const useApproveChatRequest = () => { const [error, setError] = useState(); const [loading, setLoading] = useState(false); - const { account, env,pgpPrivateKey,signer, alias } =useChatData(); + const { account, env,signer, pushUser } =useChatData(); const approveChatRequest = useCallback(async (options:ApproveChatParams) => { const { - chatId, } = options || {}; setLoading(true); try { - const response = await alias.chat.accept(chatId); + const response = await pushUser.chat.accept(chatId); setLoading(false); return response; } catch (error: Error | any) { @@ -28,7 +27,7 @@ const useApproveChatRequest = () => { } }, - [account,env,signer,alias] + [account,env,signer,pushUser] ); diff --git a/packages/uiweb/src/lib/hooks/chat/useChatProfile.ts b/packages/uiweb/src/lib/hooks/chat/useChatProfile.ts index 8a6d71411..4d384ae31 100644 --- a/packages/uiweb/src/lib/hooks/chat/useChatProfile.ts +++ b/packages/uiweb/src/lib/hooks/chat/useChatProfile.ts @@ -4,13 +4,17 @@ import { useChatData } from './useChatData'; export interface ProfileParams { profileId: string; + env:PushAPI.Env } +//need to change it to new sdk method const useChatProfile = () => { - const { env } = useChatData(); + // const { env } = useChatData(); + const fetchUserChatProfile = useCallback( async ({ - profileId + profileId, + env }: ProfileParams): Promise => { try { const profile = await PushAPI.user.get({ @@ -23,7 +27,7 @@ const useChatProfile = () => { return; } }, - [env] + [] ); return { fetchUserChatProfile }; diff --git a/packages/uiweb/src/lib/hooks/chat/useFetchChat.ts b/packages/uiweb/src/lib/hooks/chat/useFetchChat.ts index 602e6cc79..6ad60e341 100644 --- a/packages/uiweb/src/lib/hooks/chat/useFetchChat.ts +++ b/packages/uiweb/src/lib/hooks/chat/useFetchChat.ts @@ -4,23 +4,19 @@ import { useCallback, useContext, useState } from 'react'; import { useChatData } from './useChatData'; -interface fetchChat { - chatId?: string; -} + const useFetchChat = () => { const [error, setError] = useState(); const [loading, setLoading] = useState(false); - const { account, env, alias } = useChatData(); + const { account, env, pushUser } = useChatData(); const fetchChat = useCallback( async () => { setLoading(true); try { - console.log("chatss calling in hook before", alias); - const chat = await alias.chat.list("CHATS") - console.log('chatss in hook', chat); + const chat = await pushUser.chat.list("CHATS") return chat[0]; } catch (error: Error | any) { setLoading(false); @@ -32,7 +28,7 @@ const useFetchChat = () => { setLoading(false); } }, - [env,account, alias] + [env,account, pushUser] ); return { fetchChat, error, loading }; diff --git a/packages/uiweb/src/lib/hooks/chat/useFetchConversationHash.ts b/packages/uiweb/src/lib/hooks/chat/useFetchConversationHash.ts deleted file mode 100644 index 5b65d4c61..000000000 --- a/packages/uiweb/src/lib/hooks/chat/useFetchConversationHash.ts +++ /dev/null @@ -1,38 +0,0 @@ -import * as PushAPI from '@pushprotocol/restapi'; -import { Env } from '@pushprotocol/restapi'; -import { useCallback, useContext, useState } from 'react'; -import { useChatData } from './useChatData'; - -interface conversationHashParams { - conversationId: string; -} - -const useFetchConversationHash = () => { - const [error, setError] = useState(); - const [loading, setLoading] = useState(false); - const { account, env } = useChatData(); - - const fetchConversationHash = useCallback( - async ({ conversationId }: conversationHashParams) => { - setLoading(true); - try { - const response = await PushAPI.chat.conversationHash({ - conversationId, - account: account ? account : '0xeeE5A266D7cD954bE3Eb99062172E7071E664023', - env: env, - }); - setLoading(false); - return response; - } catch (error: Error | any) { - setLoading(false); - setError(error.message); - console.log(error); - return; - } - }, - [env, account] - ); - return { fetchConversationHash, error, loading }; -}; - -export default useFetchConversationHash; diff --git a/packages/uiweb/src/lib/hooks/chat/useFetchHistoryMessages.ts b/packages/uiweb/src/lib/hooks/chat/useFetchHistoryMessages.ts index 9a7b049a7..dde4850fc 100644 --- a/packages/uiweb/src/lib/hooks/chat/useFetchHistoryMessages.ts +++ b/packages/uiweb/src/lib/hooks/chat/useFetchHistoryMessages.ts @@ -1,8 +1,7 @@ -import * as PushAPI from '@pushprotocol/restapi'; -import type { IMessageIPFS } from '@pushprotocol/restapi'; + import { useCallback, useContext, useState } from 'react'; -import { ChatDataContext } from '../../context'; + import { useChatData } from './useChatData'; @@ -19,23 +18,13 @@ const useFetchHistoryMessages const [error, setError] = useState(); const [loading, setLoading] = useState(false); - const { account, env, alias } = useChatData(); + const { account, env, pushUser } = useChatData(); const historyMessages = useCallback(async ({chatId}: HistoryMessagesParams) => { setLoading(true); try { - // const chatHistory:IMessageIPFS[] = await PushAPI.chat.history({ - // threadhash: threadHash, - // account:account ? account : '0xeeE5A266D7cD954bE3Eb99062172E7071E664023', - // toDecrypt: pgpPrivateKey ? true : false, - // pgpPrivateKey: String(pgpPrivateKey), - // limit: limit, - // env: env - // }); - console.log(alias, "chatHistoryyy") - const chatHistory = await alias.chat.history(chatId) - console.log(chatHistory, "chatHistoryyy") + const chatHistory = await pushUser.chat.history(chatId) chatHistory.reverse(); return chatHistory; } catch (error: Error | any) { @@ -46,7 +35,7 @@ const useFetchHistoryMessages } finally { setLoading(false); } - }, [account,env, alias]); + }, [account,env, pushUser]); return { historyMessages, error, loading }; }; diff --git a/packages/uiweb/src/lib/hooks/chat/useInitializePushUser.ts b/packages/uiweb/src/lib/hooks/chat/useInitializePushUser.ts new file mode 100644 index 000000000..b445c062d --- /dev/null +++ b/packages/uiweb/src/lib/hooks/chat/useInitializePushUser.ts @@ -0,0 +1,32 @@ +import {PushAPI, SignerType} from '@pushprotocol/restapi'; +import { useCallback, useContext } from 'react'; +import { useChatData } from './useChatData'; + +export interface ProfileParams { + signer: SignerType; + account:string; +} + +const useInitializePushUser = () => { + const { env } = useChatData(); + + const initializePushUser = useCallback( + async ({ + signer, + account + }: ProfileParams): Promise => { + try { + const pushUser = await PushAPI.initialize(signer, {env: env,account:account}) + return pushUser; + } catch (error) { + console.log(error); + return; + } + }, + [] + ); + + return { initializePushUser }; +}; + +export default useInitializePushUser; diff --git a/packages/uiweb/src/lib/hooks/chat/usePushChatSocket.ts b/packages/uiweb/src/lib/hooks/chat/usePushChatSocket.ts index 8d2a7a7f0..23377f8f8 100644 --- a/packages/uiweb/src/lib/hooks/chat/usePushChatSocket.ts +++ b/packages/uiweb/src/lib/hooks/chat/usePushChatSocket.ts @@ -1,27 +1,16 @@ -import { createSocketConnection, EVENTS } from '@pushprotocol/socket'; import { useCallback, useEffect, useState } from 'react'; -import { ENV } from '../../config'; import { useChatData } from './useChatData'; -import { SOCKET_TYPE } from '../../types'; -import useGetChatProfile from '../useGetChatProfile'; + import { CONSTANTS } from '@pushprotocol/restapi'; -export type PushChatSocketHookOptions = { - account?: string | null; - env?: ENV; -}; + export const usePushChatSocket = () => { const { - account, - pushChatSocket, - setPushChatSocket, setIsPushChatSocketConnected, isPushChatSocketConnected, - connectedProfile, - setConnectedProfile, env, - alias + pushUser } = useChatData(); const [messagesSinceLastConnection, setMessagesSinceLastConnection] = useState({}); @@ -35,11 +24,8 @@ export const usePushChatSocket = () => { const addSocketEvents = useCallback(async () => { console.log('addSocketEvents'); - // if (!stre/z stream.on(CONSTANTS.STREAM.CONNECT, () => { - // console.log(err,"errr"); - // console.log(connected,"connected");รท - console.log('connecteddddd'); + setIsPushChatSocketConnected(true); }) @@ -57,19 +43,19 @@ export const usePushChatSocket = () => { setAcceptedRequestMessage(chat); } else { // Extract 'from' and 'to' from the 'message' property - const fromDID = chat.from; - const toDID = chat.to.join(', '); // Use the appropriate separator if needed + //shfit to a new func + const fromCAIP10 = chat.from; + const toCAIP10 = chat.to.join(', '); // Use the appropriate separator if needed // Create a new object with modified properties const messageContent = chat.message.content; const modifiedChat = { ...chat, - fromDID: fromDID, - toDID: toDID, + fromCAIP10: fromCAIP10, + toCAIP10: toCAIP10, messageContent: messageContent, }; delete modifiedChat.from; delete modifiedChat.to; - console.log('modifiedChat', modifiedChat); setMessagesSinceLastConnection((chats: any) => { return modifiedChat; @@ -77,19 +63,11 @@ export const usePushChatSocket = () => { } }); - - // pushChatSocket?.on(EVENTS.CHAT_GROUPS, (groupInfo: any) => { - // /** - // * We receive a group creation or updated event. - // */ - // setGroupInformationSinceLastConnection(groupInfo); - // }); - // eslint-disable-next-line react-hooks/exhaustive-deps }, [ messagesSinceLastConnection, env, - alias, + pushUser, stream ]); @@ -107,7 +85,7 @@ export const usePushChatSocket = () => { removeSocketEvents(); } }; - }, [stream, alias]); + }, [stream, pushUser]); /** * Whenever the required params to create a connection object change @@ -118,7 +96,7 @@ export const usePushChatSocket = () => { if (!stream) { const main = async () => { - const stream = await alias.initStream( + const stream = await pushUser.initStream( [ CONSTANTS.STREAM.CHAT, ], @@ -130,10 +108,9 @@ export const usePushChatSocket = () => { }; main().catch((err) => console.error(err)); } - // } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [alias, env, stream]); + }, [pushUser, env, stream]); return { isPushChatSocketConnected, diff --git a/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts b/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts index 617f18583..a51a9f947 100644 --- a/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts +++ b/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts @@ -1,9 +1,6 @@ -import * as PushAPI from '@pushprotocol/restapi'; -import { useCallback, useContext, useState } from 'react'; -import useVerifyAccessControl from './useVerifyAccessControl'; +import { useCallback, useState } from 'react'; import { useChatData } from '..'; -import { ENV } from '../../config'; -import { setAccessControl } from '../../helpers'; + interface SendMessageParams { message: string; @@ -15,14 +12,14 @@ const usePushSendMessage = () => { const [error, setError] = useState(); const [loading, setLoading] = useState(false); - const { env, account, alias } = useChatData(); + const { env, account, pushUser } = useChatData(); const sendMessage = useCallback( async (options: SendMessageParams) => { const { chatId, message, messageType } = options || {}; setLoading(true); try { - const response = await alias.chat.send(chatId, { + const response = await pushUser.chat.send(chatId, { type: messageType, content: message, }) @@ -39,7 +36,7 @@ const usePushSendMessage = () => { return error.message; } }, - [ account, env, alias] + [ account, env, pushUser] ); return { sendMessage, error, loading }; diff --git a/packages/uiweb/src/lib/hooks/chatAndNotification/chat/index.ts b/packages/uiweb/src/lib/hooks/chatAndNotification/chat/index.ts index 49cf9937a..1304a70ad 100644 --- a/packages/uiweb/src/lib/hooks/chatAndNotification/chat/index.ts +++ b/packages/uiweb/src/lib/hooks/chatAndNotification/chat/index.ts @@ -1,7 +1,6 @@ export * from './useFetchChats'; export * from './useFetchRequests'; export * from './useFetchHistoryMessages'; -export * from './useFetchConversationHash'; export * from './useApproveChatRequest'; export * from './useFetchChat'; export * from './usePushSendMessage'; diff --git a/packages/uiweb/src/lib/hooks/useGetChatProfile.ts b/packages/uiweb/src/lib/hooks/useGetChatProfile.ts index 269786eeb..c49f29848 100644 --- a/packages/uiweb/src/lib/hooks/useGetChatProfile.ts +++ b/packages/uiweb/src/lib/hooks/useGetChatProfile.ts @@ -1,35 +1,27 @@ -import { PushAPI, Env } from '@pushprotocol/restapi'; -import { useCallback, useContext, useEffect } from 'react'; -import { ChatAndNotificationPropsContext } from '../context'; -import { useChatData } from './chat'; -import { SignerType } from '../types'; +import * as PushAPI from '@pushprotocol/restapi'; +import { useCallback, useContext } from 'react'; +import { Constants } from '../config'; + export interface GetProfileParams { - profileId?: string; - env: Env, - signer: SignerType + profileId: string; + env:PushAPI.Env } const useGetChatProfile = () => { - const { signer, alias, setAlias, setConnectedProfile } = useChatData(); - const fetchChatProfile = useCallback( async ({ profileId, - signer, - env - }: GetProfileParams): Promise => { + env + }: GetProfileParams): Promise => { try { - console.log('signerrr', signer); - console.log("env", env) - const userAlice = await PushAPI.initialize( - signer!, - { - env: env - }); - return userAlice; + const profile = await PushAPI.user.get({ + env: env, + account: profileId, + }); + return profile; } catch (error) { - console.log("errr", error); + console.log(error); return; } },