Skip to content

Commit

Permalink
fix: requested changed
Browse files Browse the repository at this point in the history
  • Loading branch information
pritipsingh committed Nov 15, 2023
1 parent cc9557e commit fcd8614
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 47 deletions.
7 changes: 5 additions & 2 deletions packages/uiweb/src/lib/components/supportChat/AddressInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export const AddressInfo: React.FC = () => {

useEffect(() => {
const getUser = async () => {
const user = await userAlice.info();
if(userAlice){
const user = await userAlice.info();
setUser(user);
}

};
getUser();
}, [supportAddress]);
}, [supportAddress, userAlice]);

return (
<Container theme={theme}>
Expand Down
29 changes: 21 additions & 8 deletions packages/uiweb/src/lib/components/supportChat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ export type ButtonStyleProps = {
const [toastMessage, setToastMessage] = useState<string>('');
const [toastType, setToastType] = useState<'error' | 'success'>();
const [chats, setChats] = useState<IMessageIPFS[]>([]);
const [accountadd, setAccount] = useState<string | null>(account)
const [accountadd, setAccountadd] = useState<string | null>(account)
const [userAlice, setUserAlice] = useState<PushAPI | null>(null);
const setChatsSorted = (chats: IMessageIPFS[]) => {

const chatsWithNumericTimestamps = chats.map(item => ({
...item,
timestamp: typeof item.timestamp === 'string' ? parseInt(item.timestamp) : item.timestamp
}));

const uniqueChats = [
...new Map(chats.map((item) => [item['timestamp'], item])).values(),
...new Map(chatsWithNumericTimestamps.map((item) => [item.timestamp, item])).values(),
];

uniqueChats.sort((a, b) => {

return a.timestamp! > b.timestamp! ? 1 : -1;
});
setChats(uniqueChats);
Expand All @@ -63,10 +70,12 @@ export type ButtonStyleProps = {
account: accountadd,
env,
apiKey,
userAlice
userAlice,
supportAddress,
signer
});

console.log("chanecing", socketData)

const chatPropsData = {
account : accountadd,
signer,
Expand All @@ -82,20 +91,24 @@ export type ButtonStyleProps = {
useEffect(() => {
(async () => {
if(signer) {
const userAlice = await PushAPI.initialize(signer, {env: env});
setUserAlice(userAlice)
if (!account) {
const address = await getAddressFromSigner(signer);
setAccount(address);
setAccountadd(address);
const userAlice = await PushAPI.initialize(signer, {env: env , account:address});
setUserAlice(userAlice)
}
else{
setAccount(account);
setAccountadd(account);
const userAlice = await PushAPI.initialize(signer, {env: env , account:account});
setUserAlice(userAlice)
}

}
})();
},[signer])



useEffect(() => {
setChats([]);
setConnectedUser(null);
Expand Down
13 changes: 10 additions & 3 deletions packages/uiweb/src/lib/components/supportChat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@ export const ChatInput: React.FC = () => {

});
console.log(sendResponse);
if (!sendResponse) {
setToastMessage(sendResponse);
setToastType('error');
setLoading(false);

}

if (typeof sendResponse !== 'string') {
sendResponse.messageContent = message;
setChatsSorted([...chats, sendResponse]);
// sendResponse.messageContent = message;
// setChatsSorted([...chats, sendResponse]);
setMessage('');
setLoading(false);
} else {
}
else {
setToastMessage(sendResponse);
setToastType('error');
setLoading(false);
Expand Down
4 changes: 2 additions & 2 deletions packages/uiweb/src/lib/components/supportChat/Chats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if (typeof msg.timestamp === "string") {
{(msg.fromCAIP10 === caip10 || msg.from === caip10 ) ? (
<MessageWrapper align="row-reverse">
<SenderMessage theme={theme}>
{console.log("woiiii" , msg.messageContent)}

<TextMessage>{msg.messageContent || msg.message?.content}</TextMessage>
{msg.timestamp !== undefined && <TimeStamp>{date}</TimeStamp>}
</SenderMessage>
Expand All @@ -67,7 +67,7 @@ if (typeof msg.timestamp === "string") {
<MessageWrapper align="row">
<ReceivedMessage theme={theme}>
{msg?.icon && msg.icon}
{console.log("insideee",msg.message?.content)}

<TextMessage>{ msg.message?.content || msg.messageContent}</TextMessage>
{msg.timestamp !== undefined && <TimeStamp>{date}</TimeStamp>}
</ReceivedMessage>
Expand Down
26 changes: 16 additions & 10 deletions packages/uiweb/src/lib/components/supportChat/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const Modal: React.FC = () => {
const { chatsResponse, lastThreadHash, lastListPresent } = await getChats({
account,
userAlice,
pgpPrivateKey: connectedUser.privateKey,
// pgpPrivateKey: connectedUser.privateKey,
supportAddress,
threadHash: lastThreadHashFetched!,
limit: chatsFetchedLimit,
Expand Down Expand Up @@ -119,31 +119,37 @@ export const Modal: React.FC = () => {
}
};

const getUpdatedChats = async (message:IMessageIPFS) =>{

if (message && (supportAddress === pCAIP10ToWallet(message?.from))) {
// const getUpdatedChats = (message:IMessageIPFS) =>{
// console.log(message)
// if (message && (supportAddress === pCAIP10ToWallet(message?.from))) {
// const chat = await decryptChat({ message, connectedUser, env });
// socketData.messagesSinceLastConnection.decrypted = true;

setChatsSorted([...chats, message]);
}
}
// setChatsSorted([...chats, message]);
// }
// }



useEffect(() => {

if(socketData.messagesSinceLastConnection){
getUpdatedChats(socketData.messagesSinceLastConnection);
}
const message: IMessageIPFS = socketData.messagesSinceLastConnection
if (message ) {
setChatsSorted([...chats, message]);
}}
}, [socketData.messagesSinceLastConnection]);

useEffect(() => {

getChatCall();
}, [connectedUser, env, account,signer, supportAddress ]);
}, [connectedUser, env, account,signer, supportAddress, userAlice]);

useEffect(() => {
scrollToBottom();
}, [connectedUser, env, account, socketData]);


return (
<Container theme={theme}>
<HeaderSection>
Expand Down
12 changes: 8 additions & 4 deletions packages/uiweb/src/lib/helpers/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type HandleOnChatIconClickProps = {
setIsModalOpen: (isModalOpen: boolean) => void;
};




type GetChatsType = {
pgpPrivateKey?: string;
supportAddress: string;
Expand All @@ -39,9 +42,10 @@ export const createUserIfNecessary = async (
options: AccountEnvOptionsType
): Promise<IConnectedUser> => {
const { account, signer, env = Constants.ENV.PROD, userAlice } = options || {};
const connectedUser = await userAlice.info();


let connectedUser;
if(userAlice){
connectedUser = await userAlice.info();
}

return { ...connectedUser, privateKey: connectedUser.encryptedPrivateKey };
};
Expand Down Expand Up @@ -247,7 +251,7 @@ export const checkIfSameChat = (
return true;
}
}
console.log(chatId);


return false;
};
33 changes: 15 additions & 18 deletions packages/uiweb/src/lib/hooks/useSDKSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export type SDKSocketHookOptions = {
env?: ENV,
socketType?: 'chat' | 'notification',
apiKey: string,
userAlice: PushAPI | null
userAlice: PushAPI ,
supportAddress: string | null,
signer: string | null
};

export const useSDKSocket =({ account, env = ENV.PROD, socketType = 'chat',apiKey, userAlice }: SDKSocketHookOptions) => {
export const useSDKSocket =({ account, env = ENV.PROD, socketType = 'chat',apiKey, userAlice, supportAddress, signer }: SDKSocketHookOptions) => {

// const [epnsSDKSocket, setEpnsSDKSocket] = useState<any>(null);
const [messagesSinceLastConnection, setMessagesSinceLastConnection] = useState<any>('');
Expand All @@ -24,13 +26,13 @@ export const useSDKSocket =({ account, env = ENV.PROD, socketType = 'chat',apiKe

const addSocketEvents = async () => {
console.warn('\n--> addChatSocketEvents');
stream.on(CONSTANTS.STREAM.CONNECT, (a) => {
console.log('CONNECTED: ');
stream.on(CONSTANTS.STREAM.CONNECT, (err:Error) => {
console.log('CONNECTED: ', err);
setIsSDKSocketConnected(true);
});
await stream.connect();

stream.on(CONSTANTS.STREAM.DISCONNECT, (err:any) => {
stream.on(CONSTANTS.STREAM.DISCONNECT, (err:Error) => {
console.log('DIS-CONNECTED: ',err);
setIsSDKSocketConnected(false);
});
Expand All @@ -39,24 +41,19 @@ export const useSDKSocket =({ account, env = ENV.PROD, socketType = 'chat',apiKe

console.log('\t-->will attach eachMessage event now');
stream.on(CONSTANTS.STREAM.CHAT, (message: any) => {
console.log('Encrypted Message Received');
/**
* We receive a 1 message.
*/
console.log("\n\n\n\neachMessage event: ", message);

// do stuff with data
setMessagesSinceLastConnection((chats: any) => {
return {...message,decrypted:false};
return {...message};
});
// stream.disconnect();
});
};

console.log(messagesSinceLastConnection)


const removeSocketEvents = () => {
stream.disconnect();
stream?.disconnect();
};

useEffect(() => {
Expand All @@ -80,27 +77,27 @@ export const useSDKSocket =({ account, env = ENV.PROD, socketType = 'chat',apiKe
*/
useEffect(() => {
if (account) {
if (stream) {
if (!stream) {
// console.log('=================>>> disconnection in the hook');
stream?.disconnect();
}
const main = async () => {
const stream = await userAlice.initStream(
const newstream = await userAlice.initStream(
[
CONSTANTS.STREAM.CHAT,
],
{}
);

console.warn('new connection object: ', stream);
setStream(stream);
console.warn('new connection object: ', newstream);
setStream(newstream);
// setEpnsSDKSocket(connectionObject);
};
main().catch((err) => console.error(err));
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [account, env]);
}, [account, env, userAlice, supportAddress, signer, isSDKSocketConnected]);

return {
stream,
Expand Down

0 comments on commit fcd8614

Please sign in to comment.