-
Notifications
You must be signed in to change notification settings - Fork 53
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
Support chat class based #843
Changes from 5 commits
9b0a924
e2a9a06
cc9557e
fcd8614
e33870f
697ab6a
f6a3607
fdb5eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 { | ||
|
@@ -40,22 +39,28 @@ 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); | ||
pritipsingh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!sendResponse) { | ||
setToastMessage(sendResponse); | ||
setToastType('error'); | ||
setLoading(false); | ||
|
||
} | ||
|
||
if (typeof sendResponse !== 'string') { | ||
sendResponse.messageContent = message; | ||
setChatsSorted([...chats, sendResponse]); | ||
// sendResponse.messageContent = message; | ||
// setChatsSorted([...chats, sendResponse]); | ||
Comment on lines
+58
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am keeping this incase I want to shift to sorting message like this in the future. But rest every other comments I have removed |
||
setMessage(''); | ||
setLoading(false); | ||
} else { | ||
} | ||
else { | ||
setToastMessage(sendResponse); | ||
setToastType('error'); | ||
setLoading(false); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ export const Modal: React.FC = () => { | |
string | null | ||
>(null); | ||
const [wasLastListPresent, setWasLastListPresent] = useState<boolean>(false); | ||
const { supportAddress, env, account, signer, greetingMsg, theme } = | ||
const { supportAddress, userAlice, env, account, signer, greetingMsg, theme } = | ||
useContext<any>(SupportChatPropsContext); | ||
const { | ||
chats, | ||
|
@@ -42,7 +42,7 @@ export const Modal: React.FC = () => { | |
setToastType, | ||
socketData | ||
} = useContext<any>(SupportChatMainStateContext); | ||
const listInnerRef = useChatScroll(chats.length); | ||
const listInnerRef = useChatScroll(0); | ||
|
||
const greetingMsgObject = { | ||
fromDID: walletToPCAIP10(supportAddress), | ||
|
@@ -63,20 +63,35 @@ export const Modal: React.FC = () => { | |
if (listInnerRef.current) { | ||
const { scrollTop } = listInnerRef.current; | ||
if (scrollTop === 0) { | ||
// This will be triggered after hitting the first element. | ||
// pagination | ||
const content = listInnerRef.current; | ||
const curScrollPos = content.scrollTop; | ||
const oldScroll = content.scrollHeight - content.clientHeight; | ||
|
||
getChatCall(); | ||
|
||
const newScroll = content.scrollHeight - content.clientHeight; | ||
content.scrollTop = curScrollPos + (newScroll - oldScroll); | ||
} | ||
} | ||
}; | ||
const scrollToBottom = () => { | ||
setTimeout(()=>{ | ||
if (listInnerRef.current) { | ||
listInnerRef.current.scrollTop = listInnerRef.current.scrollHeight +100; | ||
|
||
} | ||
},0) | ||
|
||
}; | ||
|
||
const getChatCall = async () => { | ||
if (!connectedUser) return; | ||
if (wasLastListPresent && !lastThreadHashFetched) return; | ||
setLoading(true); | ||
const { chatsResponse, lastThreadHash, lastListPresent } = await getChats({ | ||
account, | ||
pgpPrivateKey: connectedUser.privateKey, | ||
userAlice, | ||
// pgpPrivateKey: connectedUser.privateKey, | ||
supportAddress, | ||
threadHash: lastThreadHashFetched!, | ||
limit: chatsFetchedLimit, | ||
|
@@ -94,7 +109,7 @@ export const Modal: React.FC = () => { | |
if (!socketData.epnsSDKSocket?.connected) { | ||
socketData.epnsSDKSocket?.connect(); | ||
} | ||
const user = await createUserIfNecessary({ account, signer, env }); | ||
const user = await createUserIfNecessary({ account, signer, env, userAlice }); | ||
setConnectedUser(user); | ||
setLoading(false); | ||
} catch (err:any) { | ||
|
@@ -104,23 +119,36 @@ export const Modal: React.FC = () => { | |
} | ||
}; | ||
|
||
const getUpdatedChats = async (message:IMessageIPFS) =>{ | ||
if (message && (supportAddress === pCAIP10ToWallet(message?.fromCAIP10))) { | ||
const chat = await decryptChat({ message, connectedUser, env }); | ||
socketData.messagesSinceLastConnection.decrypted = true; | ||
setChatsSorted([...chats, chat]); | ||
} | ||
} | ||
// 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]); | ||
// } | ||
// } | ||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this section |
||
useEffect(() => { | ||
if(socketData.messagesSinceLastConnection && !socketData.messagesSinceLastConnection.decrypted){ | ||
getUpdatedChats(socketData.messagesSinceLastConnection); | ||
} | ||
|
||
if(socketData.messagesSinceLastConnection){ | ||
const message: IMessageIPFS = socketData.messagesSinceLastConnection | ||
if (message ) { | ||
setChatsSorted([...chats, message]); | ||
}} | ||
}, [socketData.messagesSinceLastConnection]); | ||
|
||
useEffect(() => { | ||
|
||
getChatCall(); | ||
}, [connectedUser]); | ||
}, [connectedUser, env, account,signer, supportAddress, userAlice]); | ||
|
||
useEffect(() => { | ||
scrollToBottom(); | ||
}, [connectedUser, env, account, socketData]); | ||
|
||
|
||
return ( | ||
<Container theme={theme}> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do the initialize in a useEffect, this makes the code redundant?