From 4fdb19717fd11969e7b699e856e7ae3ba9e75af9 Mon Sep 17 00:00:00 2001 From: talksik Date: Sun, 29 May 2022 14:29:51 -0500 Subject: [PATCH] formatting label for conversation list --- .../src/components/ConversationList.tsx | 7 ++-- .../src/subcomponents/ConversationLabel.tsx | 37 +++++++++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/desktop/src/components/ConversationList.tsx b/packages/desktop/src/components/ConversationList.tsx index 350e680..2b79748 100644 --- a/packages/desktop/src/components/ConversationList.tsx +++ b/packages/desktop/src/components/ConversationList.tsx @@ -32,6 +32,7 @@ import useTerminal from './Terminal'; import Conversation from '@nirvana/core/src/models/conversation.model'; import useAuth from '../providers/AuthProvider'; import { useImmer } from 'use-immer'; +import ConversationLabel from '../subcomponents/ConversationLabel'; /** * @@ -116,9 +117,9 @@ function ConversationRow({ conversation }: { conversation: Conversation }) { - - {conversationUsers.map((conversationUser) => conversationUser.displayName).join(', ')} - + + + diff --git a/packages/desktop/src/subcomponents/ConversationLabel.tsx b/packages/desktop/src/subcomponents/ConversationLabel.tsx index 58e9e2e..98f5d7a 100644 --- a/packages/desktop/src/subcomponents/ConversationLabel.tsx +++ b/packages/desktop/src/subcomponents/ConversationLabel.tsx @@ -1,15 +1,46 @@ import React from 'react'; import Conversation from '@nirvana/core/src/models/conversation.model'; +import { User } from '@nirvana/core/src/models/user.model'; +import { Typography } from '@mui/material'; +import useAuth from '../providers/AuthProvider'; + +type IConversationLabel = + | { + conversationName?: string; + users: User[]; + } + | { + conversationName: string; + users?: User[]; + }; /** * if there is a name in the conversation, then return that * if not, then return first three names */ -export default function ConversationLabel({ conversation }: { conversation: Conversation }) { - // if (conversation.memberIdsList?.length === 2) { +export default function ConversationLabel({ conversationName, users }: IConversationLabel) { + const { user } = useAuth(); - // } + if (conversationName) { + return {conversationName}; + } + + const filteredUsers = users.filter((convoUser) => convoUser.id !== user.uid); + + if (filteredUsers.length === 1) { + const firstName = filteredUsers[0].displayName.split(' ')[0]; + + return {firstName}; + } + + if (filteredUsers.length > 1) { + const firstNames = filteredUsers.map((filteredUser) => filteredUser.displayName.split(' ')[0]); + + const formattedFirstnames = firstNames.slice(0, -1).join(',') + ' and ' + firstNames.slice(-1); + + return {formattedFirstnames}; + } return; }