From 5cd26fae73dc8655508797ad5613826ae59c7909 Mon Sep 17 00:00:00 2001 From: PhilReact Date: Thu, 24 Oct 2024 04:48:49 +0300 Subject: [PATCH] initial chat load fix --- src/components/Chat/ChatDirect.tsx | 30 ++++++++++++++++++++++++----- src/components/Chat/ChatGroup.tsx | 31 +++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/components/Chat/ChatDirect.tsx b/src/components/Chat/ChatDirect.tsx index a73e893..916e7ff 100644 --- a/src/components/Chat/ChatDirect.tsx +++ b/src/components/Chat/ChatDirect.tsx @@ -11,7 +11,7 @@ import { LoadingSnackbar } from '../Snackbar/LoadingSnackbar'; import { getNameInfo } from '../Group/Group'; import { Spacer } from '../../common/Spacer'; import { CustomizedSnackbars } from '../Snackbar/Snackbar'; -import { getBaseApiReactSocket, isMobile, pauseAllQueues, resumeAllQueues } from '../../App'; +import { getBaseApiReact, getBaseApiReactSocket, isMobile, pauseAllQueues, resumeAllQueues } from '../../App'; import { getPublicKey } from '../../background'; import { useMessageQueue } from '../../MessageQueueContext'; import { executeEvent, subscribeToEvent, unsubscribeFromEvent } from '../../utils/events'; @@ -77,9 +77,28 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi }, [selectedDirect?.address]) - + const middletierFunc = async (data: any, selectedDirectAddress: string, myAddress: string) => { + try { + if (hasInitialized.current) { + decryptMessages(data, true); + return; + } + hasInitialized.current = true; + const url = `${getBaseApiReact()}/chat/messages?involving=${selectedDirectAddress}&involving=${myAddress}&encoding=BASE64&limit=0&reverse=false`; + const response = await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + const responseData = await response.json(); + decryptMessages(responseData, false); + } catch (error) { + console.error(error); + } + } - const decryptMessages = (encryptedMessages: any[])=> { + const decryptMessages = (encryptedMessages: any[], isInitiated: boolean)=> { try { return new Promise((res, rej)=> { chrome?.runtime?.sendMessage({ action: "decryptDirect", payload: { @@ -92,7 +111,7 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi processWithNewMessages(response, selectedDirect?.address) res(response) - if(hasInitialized.current){ + if(isInitiated){ const formatted = response.map((item: any)=> { return { @@ -169,7 +188,8 @@ export const ChatDirect = ({ myAddress, isNewChat, selectedDirect, setSelectedDi clearTimeout(timeoutIdRef.current); groupSocketTimeoutRef.current = setTimeout(pingWebSocket, 45000); // Ping every 45 seconds } else { - decryptMessages(JSON.parse(e.data)); + middletierFunc(JSON.parse(e.data), selectedDirect?.address, myAddress) + setIsLoading(false); } } catch (error) { diff --git a/src/components/Chat/ChatGroup.tsx b/src/components/Chat/ChatGroup.tsx index 1f611c8..93f3adf 100644 --- a/src/components/Chat/ChatGroup.tsx +++ b/src/components/Chat/ChatGroup.tsx @@ -10,7 +10,7 @@ import Tiptap from './TipTap' import { CustomButton } from '../../App-styles' import CircularProgress from '@mui/material/CircularProgress'; import { LoadingSnackbar } from '../Snackbar/LoadingSnackbar' -import { getBaseApiReactSocket, isMobile, pauseAllQueues, resumeAllQueues } from '../../App' +import { getBaseApiReact, getBaseApiReactSocket, isMobile, pauseAllQueues, resumeAllQueues } from '../../App' import { CustomizedSnackbars } from '../Snackbar/Snackbar' import { PUBLIC_NOTIFICATION_CODE_FIRST_SECRET_KEY } from '../../constants/codes' import { useMessageQueue } from '../../MessageQueueContext' @@ -101,8 +101,29 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey, }) } + const middletierFunc = async (data: any, groupId: string) => { + try { + if (hasInitialized.current) { + decryptMessages(data, true); + return; + } + hasInitialized.current = true; + const url = `${getBaseApiReact()}/chat/messages?txGroupId=${groupId}&encoding=BASE64&limit=0&reverse=false`; + const response = await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + const responseData = await response.json(); + decryptMessages(responseData, false); + } catch (error) { + console.error(error); + } + } + - const decryptMessages = (encryptedMessages: any[])=> { + const decryptMessages = (encryptedMessages: any[], isInitiated: boolean )=> { try { if(!secretKeyRef.current){ checkForFirstSecretKeyNotification(encryptedMessages) @@ -125,7 +146,7 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey, } }), selectedGroup) res(combineUIAndExtensionMsgs) - if(hasInitialized.current){ + if(isInitiated){ const formatted = combineUIAndExtensionMsgs.filter((rawItem)=> !rawItem?.chatReference).map((item: any)=> { return { @@ -216,7 +237,7 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey, } } ) setMessages(formatted) - hasInitialized.current = true + setChatReferences((prev) => { let organizedChatReferences = { ...prev }; @@ -336,7 +357,7 @@ export const ChatGroup = ({selectedGroup, secretKey, setSecretKey, getSecretKey, clearTimeout(timeoutIdRef.current); groupSocketTimeoutRef.current = setTimeout(pingGroupSocket, 45000); // Ping every 45 seconds } else { - decryptMessages(JSON.parse(e.data)) + middletierFunc(JSON.parse(e.data), selectedGroup) setIsLoading(false) } } catch (error) {