Skip to content

Commit

Permalink
fix: removed unn consolelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
pritipsingh committed Nov 15, 2023
1 parent e2a9a06 commit cc9557e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 60 deletions.
5 changes: 2 additions & 3 deletions packages/uiweb/src/lib/components/supportChat/AddressInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import React, { useContext, useEffect, useState } from 'react';
import styled from 'styled-components';
import { SupportChatPropsContext } from '../../context';
import * as PushAPI from '@pushprotocol/restapi';
import { Constants } from '../../config';
import { copyToClipboard, pCAIP10ToWallet } from '../../helpers';
import { CopySvg } from '../../icons/CopySvg';

export const AddressInfo: React.FC = () => {
const { supportAddress, env, theme } = useContext<any>(SupportChatPropsContext);
const { supportAddress, env, theme, userAlice } = useContext<any>(SupportChatPropsContext);
const [ensName, setEnsName] = useState<string>('');
const [user, setUser] = useState<any>({});
const [isCopied, setIsCopied] = useState<boolean>(false);
const walletAddress = pCAIP10ToWallet(supportAddress);

useEffect(() => {
const getUser = async () => {
const user = await PushAPI.user.get({ account: walletAddress, env });
const user = await userAlice.info();
setUser(user);
};
getUser();
Expand Down
1 change: 1 addition & 0 deletions packages/uiweb/src/lib/components/supportChat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type ButtonStyleProps = {
userAlice
});

console.log("chanecing", socketData)
const chatPropsData = {
account : accountadd,
signer,
Expand Down
18 changes: 8 additions & 10 deletions packages/uiweb/src/lib/components/supportChat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { SendIconSvg } from '../../icons/SendIconSvg';
import SmileyIcon from '../../icons/smiley.svg';
import AttachmentIcon from '../../icons/attachment.svg';
import styled from 'styled-components';
import * as PushAPI from '@pushprotocol/restapi';
import { SupportChatMainStateContext, SupportChatPropsContext } from '../../context';
import { Spinner } from './spinner/Spinner';
// import Picker from 'emoji-picker-react';
Expand All @@ -14,7 +13,7 @@ export const ChatInput: React.FC = () => {
const fileInputRef = useRef<HTMLInputElement>(null);
const [filesUploading, setFileUploading] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const { account, env, supportAddress, apiKey, theme } =
const { account, env, supportAddress, apiKey, theme, userAlice } =
useContext<any>(SupportChatPropsContext);

const {
Expand All @@ -40,15 +39,14 @@ export const ChatInput: React.FC = () => {
e.preventDefault();
setLoading(true);
if (message.trim() !== '' && connectedUser) {
const sendResponse = await PushAPI.chat.send({
messageContent: message,
messageType: 'Text',
receiverAddress: supportAddress,
account: account,
pgpPrivateKey: connectedUser?.privateKey,
apiKey,
env,

const sendResponse = await userAlice.chat.send(supportAddress ,{

type: 'Text',
content: message,

});
console.log(sendResponse);

if (typeof sendResponse !== 'string') {
sendResponse.messageContent = message;
Expand Down
42 changes: 31 additions & 11 deletions packages/uiweb/src/lib/components/supportChat/Chats.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import React, { useContext, useState } from 'react';
import styled from 'styled-components';
import { SupportChatMainStateContext, SupportChatPropsContext } from '../../context';
Expand Down Expand Up @@ -30,29 +31,48 @@ export const Chats: React.FC<ChatsPropType> = ({
const { connectedUser } = useContext<any>(SupportChatMainStateContext);
const [showImageModal, setShowImageModal] = useState<boolean>(false);
const [imageUrl, setImageUrl] = useState<string>('');
const time: Date = new Date(msg.timestamp!);

let date;
let timestamp;

if (typeof msg.timestamp === "string") {
timestamp = parseInt(msg.timestamp);
}else{
timestamp = msg.timestamp
}
const time = new Date(timestamp!);

if (!isNaN(time.getTime())){

const time1 = time.toLocaleTimeString('en-US');
const date = time1.slice(0, -6) + time1.slice(-2);

date = time1.slice(0, -6) + time1.slice(-2);
}


return (
<Container>
<>
{msg.messageType === 'Text' ? (
{msg.messageType === 'Text' || msg.message?.type ? (
<>
{msg.fromCAIP10 === caip10 ? (
{(msg.fromCAIP10 === caip10 || msg.from === caip10 ) ? (
<MessageWrapper align="row-reverse">
<SenderMessage theme={theme}>
<TextMessage>{msg.messageContent}</TextMessage>
{console.log("woiiii" , msg.messageContent)}
<TextMessage>{msg.messageContent || msg.message?.content}</TextMessage>
{msg.timestamp !== undefined && <TimeStamp>{date}</TimeStamp>}
</SenderMessage>
</MessageWrapper>
) : (
<MessageWrapper align="row">
<ReceivedMessage theme={theme}>
{msg?.icon && msg.icon}
<TextMessage>{msg.messageContent}</TextMessage>
{msg.timestamp !== undefined && <TimeStamp>{date}</TimeStamp>}
</ReceivedMessage>
</MessageWrapper>
<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>
</MessageWrapper>

)}
</>
) : // : msg.messageType === 'Image' ? (
Expand Down
11 changes: 7 additions & 4 deletions packages/uiweb/src/lib/components/supportChat/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ export const Modal: React.FC = () => {
};

const getUpdatedChats = async (message:IMessageIPFS) =>{
if (message && (supportAddress === pCAIP10ToWallet(message?.fromCAIP10))) {

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

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

useEffect(() => {
if(socketData.messagesSinceLastConnection && !socketData.messagesSinceLastConnection.decrypted){

if(socketData.messagesSinceLastConnection){
getUpdatedChats(socketData.messagesSinceLastConnection);
}
}, [socketData.messagesSinceLastConnection]);
Expand Down
71 changes: 39 additions & 32 deletions packages/uiweb/src/lib/hooks/useSDKSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,73 @@ import {
EVENTS
} from '@pushprotocol/socket';
import { ENV } from '../config';
import { PushAPI } from '@pushprotocol/restapi';
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi';


export type SDKSocketHookOptions = {
account?: string | null,
env?: ENV,
socketType?: 'chat' | 'notification',
apiKey: string,
userAlice: PushAPI
userAlice: PushAPI | null
};

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

const [epnsSDKSocket, setEpnsSDKSocket] = useState<any>(null);
// const [epnsSDKSocket, setEpnsSDKSocket] = useState<any>(null);
const [messagesSinceLastConnection, setMessagesSinceLastConnection] = useState<any>('');
const [isSDKSocketConnected, setIsSDKSocketConnected] = useState(epnsSDKSocket?.connected);

const addSocketEvents = () => {
console.warn('\n--> addSocketEvents');
epnsSDKSocket?.on(EVENTS.CONNECT, () => {
const [isSDKSocketConnected, setIsSDKSocketConnected] = useState<boolean>(false);
const [stream, setStream] = useState<any>(null);

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

epnsSDKSocket?.on(EVENTS.DISCONNECT, (err:any) => {
stream.on(CONSTANTS.STREAM.DISCONNECT, (err:any) => {
console.log('DIS-CONNECTED: ',err);
setIsSDKSocketConnected(false);
});



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

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

console.log(messagesSinceLastConnection)

const removeSocketEvents = () => {
console.warn('\n--> removeSocketEvents');
epnsSDKSocket?.off(EVENTS.CONNECT);
epnsSDKSocket?.off(EVENTS.DISCONNECT);
stream.disconnect();
};

useEffect(() => {
if (epnsSDKSocket) {
if (stream) {
addSocketEvents();
}

return () => {
if (epnsSDKSocket) {
if (stream) {
removeSocketEvents();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [epnsSDKSocket]);
}, [stream]);


/**
Expand All @@ -73,21 +80,21 @@ export const useSDKSocket = ({ account, env = ENV.PROD, socketType = 'chat',apiK
*/
useEffect(() => {
if (account) {
if (epnsSDKSocket) {
if (stream) {
// console.log('=================>>> disconnection in the hook');
epnsSDKSocket?.disconnect();
stream?.disconnect();
}
const main = async () => {
const connectionObject = createSocketConnection({
user: account,
env,
apiKey,
socketType,
socketOptions: { autoConnect: true , reconnectionAttempts: 3}
});
console.warn('new connection object: ', connectionObject);

setEpnsSDKSocket(connectionObject);
const stream = await userAlice.initStream(
[
CONSTANTS.STREAM.CHAT,
],
{}
);

console.warn('new connection object: ', stream);
setStream(stream);
// setEpnsSDKSocket(connectionObject);
};
main().catch((err) => console.error(err));
}
Expand All @@ -96,7 +103,7 @@ export const useSDKSocket = ({ account, env = ENV.PROD, socketType = 'chat',apiK
}, [account, env]);

return {
epnsSDKSocket,
stream,
isSDKSocketConnected,
messagesSinceLastConnection
}
Expand Down
13 changes: 13 additions & 0 deletions packages/uiweb/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { Bytes, TypedDataDomain, TypedDataField, providers } from 'ethers';

export interface IMessageIPFS {
cid? : string;
chatId? :string;
event? :string;
from?:string;
message?: IMessage;
meta?: any;
origin?:string;
reference? :string;
to?: string[];
fromCAIP10: string;
toCAIP10: string;
fromDID: string;
Expand All @@ -20,6 +28,11 @@ export interface IMessageIPFS {
icon?: ReactElement<string | any>;
}

interface IMessage {
content: string | undefined;
type: string | undefined;
}

export interface AccountEnvOptionsType {
env?: ENV;
account: string;
Expand Down

0 comments on commit cc9557e

Please sign in to comment.