From c184e9dcbaa443e614cfea74228b2d20c3d63666 Mon Sep 17 00:00:00 2001 From: Kirat Date: Wed, 5 Apr 2023 18:39:02 +0530 Subject: [PATCH] Fixed document err, message (#3611) --- .../Unlocked/Chat/ChatDetailScreen.tsx | 20 ++++-- .../db/src/api/LocalImageManager.native.ts | 66 +++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 packages/db/src/api/LocalImageManager.native.ts diff --git a/packages/app-mobile/src/screens/Unlocked/Chat/ChatDetailScreen.tsx b/packages/app-mobile/src/screens/Unlocked/Chat/ChatDetailScreen.tsx index 4286f9b92..7482b4bd5 100644 --- a/packages/app-mobile/src/screens/Unlocked/Chat/ChatDetailScreen.tsx +++ b/packages/app-mobile/src/screens/Unlocked/Chat/ChatDetailScreen.tsx @@ -61,6 +61,7 @@ export function ChatDetailScreen({ const { roomType, roomId, remoteUserId, remoteUsername } = route.params; const user = useUser(); const avatarUrl = useAvatarUrl(); + const existingChatRef = useRef(); const [isLoadingEarlier, setIsLoadingEarlier] = useState(false); const { chats } = useChatsWithMetadata({ @@ -81,6 +82,17 @@ export function ChatDetailScreen({ const [messages, setMessages] = useState([]); useEffect(() => { + if ( + existingChatRef.current && + JSON.stringify(chats) === JSON.stringify(existingChatRef.current) + ) { + return; + } + if (chats && chats.length) { + SignalingManager.getInstance().debouncedUpdateLastRead( + chats[chats.length - 1] + ); + } const _messages = chats .map((x) => { return { @@ -104,10 +116,10 @@ export function ChatDetailScreen({ }) .reverse(); + existingChatRef.current = chats; setMessages(_messages); - }, []); + }, [chats]); - // TODO(kirat) not sure this is doing anything const onSend = useCallback( async (messages = []) => { const [message] = messages; @@ -150,10 +162,6 @@ export function ChatDetailScreen({ room: roomId.toString(), }, }); - - setMessages((previousMessages) => { - GiftedChat.append(previousMessages, messages); - }); }, [chats.length, roomId, roomType, user.uuid, remoteUserId, remoteUsername] ); diff --git a/packages/db/src/api/LocalImageManager.native.ts b/packages/db/src/api/LocalImageManager.native.ts new file mode 100644 index 000000000..c087a7954 --- /dev/null +++ b/packages/db/src/api/LocalImageManager.native.ts @@ -0,0 +1,66 @@ +import { getImage, putImage } from "../db/images"; + +export class LocalImageManager { + static instance: LocalImageManager; + queue: { image: string; timestamp: number; fullImage?: boolean }[] = []; + + private constructor() { + this.process(); + } + + async process() { + this.queue = this.queue + .filter((x) => (new Date().getTime() - x.timestamp) / 1000 > 3600) + .sort((a, b) => (a.timestamp < b.timestamp ? 1 : -1)); + const nextEl = this.queue.pop(); + if (nextEl) { + await this.storeImageInLocalStorage(nextEl.image, nextEl.fullImage); + } + await this.sleep(15); + this.process(); + } + + async sleep(timer) { + await new Promise((resolve) => setTimeout(resolve, timer * 1000)); + } + + bulkAddToQueue(elements: { image: string }[]) { + elements.forEach((el) => this.addToQueue(el)); + } + + async addToQueue({ image }: { image: string }) { + if (this.queue.find((x) => x.image === image)) { + return; + } + + try { + const parsedEl = await getImage("images", `image-${image}`); + if (parsedEl) { + this.queue.push({ + image, + timestamp: parsedEl.timestamp, + fullImage: parsedEl.fullImage || false, + }); + } else { + this.queue.push({ image, timestamp: 0 }); + } + } catch (e) { + this.queue.push({ image, timestamp: 0 }); + } + } + + static getInstance() { + if (!this.instance) { + this.instance = new LocalImageManager(); + } + return this.instance; + } + + storeImageInLocalStorage( + url: string, + fullImage?: boolean, + overridenUrl?: string + ) { + return new Promise((resolve, reject) => {}); + } +}