From bcff8af32cdf9dffd49592b4b18cb8f6137f7015 Mon Sep 17 00:00:00 2001 From: Rashul Chutani Date: Sun, 2 Apr 2023 11:30:40 +0530 Subject: [PATCH 1/5] Make Sure that the two users in a DM Chat don't have the same color --- .../src/hooks/chat/useChatsWithMetadata.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx index f6c045e4f..a53db5606 100644 --- a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx +++ b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx @@ -3,6 +3,7 @@ import type { SubscriptionType, UserMetadata, } from "@coral-xyz/common"; +import { NEW_COLORS } from "@coral-xyz/common"; import { useChats, useUser } from "@coral-xyz/recoil"; import { useUsersMetadata } from "./useUsersMetadata"; @@ -30,10 +31,22 @@ export const useChatsWithMetadata = ({ const uniqueUserIds = userIds .filter((x, index) => userIds.indexOf(x) === index) .filter((x) => x); - const users = useUsersMetadata({ remoteUserIds: uniqueUserIds }); + let users = useUsersMetadata({ remoteUserIds: uniqueUserIds }); console.log("debug2", { chats, userIds, uniqueUserIds }); - + + // Make Sure that both the users in a DM chat do not have the same color + if (uniqueUserIds.length === 2) { + const userId1 = uniqueUserIds[0]; + const userId2 = uniqueUserIds[1]; + if (users[userId1] && users[userId2]) { + if (users[userId1].colorIndex === users[userId2].colorIndex) { + users[userId2].colorIndex = + (users[userId1].colorIndex + 1) % NEW_COLORS.length; + } + } + } + return { chats: chats.map((chat) => ({ ...chat, From 1a54edbc326d33dc7555b7f3cc330b5395434fbd Mon Sep 17 00:00:00 2001 From: Rashul Chutani Date: Sun, 2 Apr 2023 21:39:39 +0530 Subject: [PATCH 2/5] Reformat packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx Co-authored-by: Getabalew Tesfaye <75031127+getusha@users.noreply.github.com> --- packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx index a53db5606..eca89c36c 100644 --- a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx +++ b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx @@ -37,8 +37,7 @@ export const useChatsWithMetadata = ({ // Make Sure that both the users in a DM chat do not have the same color if (uniqueUserIds.length === 2) { - const userId1 = uniqueUserIds[0]; - const userId2 = uniqueUserIds[1]; + const [userId1, userId2] = uniqueUserIds; if (users[userId1] && users[userId2]) { if (users[userId1].colorIndex === users[userId2].colorIndex) { users[userId2].colorIndex = From e0c0afe9bfbfa9c93ce4ecd0c41c3da96eca3c19 Mon Sep 17 00:00:00 2001 From: Rashul Chutani Date: Sun, 2 Apr 2023 21:40:14 +0530 Subject: [PATCH 3/5] Reformat packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx Co-authored-by: Getabalew Tesfaye <75031127+getusha@users.noreply.github.com> --- .../src/hooks/chat/useChatsWithMetadata.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx index eca89c36c..eabfb6867 100644 --- a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx +++ b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx @@ -38,11 +38,14 @@ export const useChatsWithMetadata = ({ // Make Sure that both the users in a DM chat do not have the same color if (uniqueUserIds.length === 2) { const [userId1, userId2] = uniqueUserIds; - if (users[userId1] && users[userId2]) { - if (users[userId1].colorIndex === users[userId2].colorIndex) { - users[userId2].colorIndex = - (users[userId1].colorIndex + 1) % NEW_COLORS.length; - } + + function haveSameColor(user1, user2) { + return (user1 && user2) && user1.colorIndex === user2.colorIndex; + } + + if (haveSameColor(users[userId1], users[userId2])) { + const newColorIndex = (users[userId1].colorIndex + 1) % NEW_COLORS.length; + users[userId2].colorIndex = newColorIndex; } } From 23be86576a6bd7515eb933790689c911b3b23624 Mon Sep 17 00:00:00 2001 From: RashulChutani Date: Tue, 4 Apr 2023 01:03:26 +0530 Subject: [PATCH 4/5] Add Map of UserId to ColorIndex to not update the users Variable #3459 --- .../src/hooks/chat/useChatsWithMetadata.tsx | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx index eabfb6867..0a3d2b611 100644 --- a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx +++ b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx @@ -31,31 +31,28 @@ export const useChatsWithMetadata = ({ const uniqueUserIds = userIds .filter((x, index) => userIds.indexOf(x) === index) .filter((x) => x); - let users = useUsersMetadata({ remoteUserIds: uniqueUserIds }); + const users = useUsersMetadata({ remoteUserIds: uniqueUserIds }); console.log("debug2", { chats, userIds, uniqueUserIds }); - + // Make Sure that both the users in a DM chat do not have the same color + const colorIndexMap = new Map(); if (uniqueUserIds.length === 2) { const [userId1, userId2] = uniqueUserIds; - function haveSameColor(user1, user2) { - return (user1 && user2) && user1.colorIndex === user2.colorIndex; - } - if (haveSameColor(users[userId1], users[userId2])) { - const newColorIndex = (users[userId1].colorIndex + 1) % NEW_COLORS.length; - users[userId2].colorIndex = newColorIndex; + const newColorIndex = (users[userId1].colorIndex + 1) % NEW_COLORS.length; + colorIndexMap[userId2] = newColorIndex; } } - + return { chats: chats.map((chat) => ({ ...chat, image: users[chat.uuid]?.image || "", username: users[chat.uuid]?.username || "", color: users[chat.uuid]?.color, - colorIndex: users[chat.uuid]?.colorIndex, + colorIndex: colorIndexMap[chat.uuid] || users[chat.uuid]?.colorIndex, parent_message_author_username: users[chat.parent_message_author_uuid]?.username, })), @@ -63,6 +60,10 @@ export const useChatsWithMetadata = ({ }; }; +function haveSameColor(user1, user2) { + return user1 && user2 && user1.colorIndex === user2.colorIndex; +} + function getAllUserIdsInMessage(message) { const userIds: string[] = []; for (let i = 0; i < message.length; i++) { From 76cb63f74a2387875af07a73a0de239cdeba7c53 Mon Sep 17 00:00:00 2001 From: RashulChutani Date: Thu, 6 Apr 2023 13:07:50 +0530 Subject: [PATCH 5/5] Use Map.set() and Map.get() --- packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx index 0a3d2b611..cd60818fe 100644 --- a/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx +++ b/packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx @@ -42,7 +42,7 @@ export const useChatsWithMetadata = ({ if (haveSameColor(users[userId1], users[userId2])) { const newColorIndex = (users[userId1].colorIndex + 1) % NEW_COLORS.length; - colorIndexMap[userId2] = newColorIndex; + colorIndexMap.set(userId2, newColorIndex); } } @@ -52,7 +52,7 @@ export const useChatsWithMetadata = ({ image: users[chat.uuid]?.image || "", username: users[chat.uuid]?.username || "", color: users[chat.uuid]?.color, - colorIndex: colorIndexMap[chat.uuid] || users[chat.uuid]?.colorIndex, + colorIndex: colorIndexMap.get(chat.uuid) || users[chat.uuid]?.colorIndex, parent_message_author_username: users[chat.parent_message_author_uuid]?.username, })),