Skip to content

Commit

Permalink
Make Sure that the two users in a DM Chat don't have the same color (#…
Browse files Browse the repository at this point in the history
…3546)

* Make Sure that the two users in a DM Chat don't have the same color

* Reformat packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx

Co-authored-by: Getabalew Tesfaye <[email protected]>

* Reformat packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx

Co-authored-by: Getabalew Tesfaye <[email protected]>

* Add Map of UserId to ColorIndex to not update the users Variable #3459

* Use Map.set() and Map.get()

---------

Co-authored-by: Getabalew Tesfaye <[email protected]>
  • Loading branch information
RashulChutani and getusha authored Apr 6, 2023
1 parent fff1664 commit 4808679
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/tamagui-core/src/hooks/chat/useChatsWithMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -34,20 +35,35 @@ export const useChatsWithMetadata = ({

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;

if (haveSameColor(users[userId1], users[userId2])) {
const newColorIndex = (users[userId1].colorIndex + 1) % NEW_COLORS.length;
colorIndexMap.set(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.get(chat.uuid) || users[chat.uuid]?.colorIndex,
parent_message_author_username:
users[chat.parent_message_author_uuid]?.username,
})),
usersMetadata: users,
};
};

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++) {
Expand Down

1 comment on commit 4808679

@vercel
Copy link

@vercel vercel bot commented on 4808679 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.