Skip to content

Commit

Permalink
refactor(conversation-item): remove prefix for one on one conversatio…
Browse files Browse the repository at this point in the history
…ns (#2522)

* refactor(app-bar): re-order app icons

* refactor(conversation-item): remove prefix for one on one conversations
  • Loading branch information
domw30 authored Dec 16, 2024
1 parent 360436a commit 0f5298c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/messenger/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ function addLastMessageMeta(state: RootState): any {
return {
...conversation,
mostRecentMessage,
messagePreview: getMessagePreview(mostRecentMessage, state),
messagePreview: getMessagePreview(mostRecentMessage, state, conversation.isOneOnOne),
previewDisplayDate: previewDisplayDate(mostRecentMessage?.createdAt),
};
};
Expand Down
24 changes: 24 additions & 0 deletions src/lib/chat/chat-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,30 @@ describe(getMessagePreview, () => {

expect(preview).toEqual('Admin: System update or change occurred');
});

it('does not add prefix for one-on-one conversations', function () {
const state = new StoreBuilder().withCurrentUser({ id: 'current-user' }).build();

const preview = getMessagePreview(
{ message: 'some message', sender: { userId: 'another-user', firstName: 'Jack' } } as Message,
state,
true // isOneOnOne
);

expect(preview).toEqual('some message');
});

it('adds prefix for group conversations', function () {
const state = new StoreBuilder().withCurrentUser({ id: 'current-user' }).build();

const preview = getMessagePreview(
{ message: 'some message', sender: { userId: 'another-user', firstName: 'Jack' } } as Message,
state,
false // isOneOnOne
);

expect(preview).toEqual('Jack: some message');
});
});

describe(previewDisplayDate, () => {
Expand Down
20 changes: 13 additions & 7 deletions src/lib/chat/chat-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,35 @@ export function previewDisplayDate(timestamp: number, currentDate = moment()) {
return messageDate.format('MMM D, YYYY');
}

export function getMessagePreview(message: Message, state: RootState) {
export function getMessagePreview(message: Message, state: RootState, isOneOnOne: boolean = false) {
if (!message) {
return 'Admin: System update or change occurred';
}

if (message.sendStatus === MessageSendStatus.FAILED) {
return 'You: Failed to send';
return isOneOnOne ? 'Failed to send' : 'You: Failed to send';
}

if (message.isAdmin) {
return adminMessageText(message, state);
}

if (message.isPost) {
let prefix = previewPrefix(message.sender, state);
return `${prefix}: shared a new post`;
let prefix = previewPrefix(message.sender, state, isOneOnOne);
return prefix ? `${prefix}: shared a new post` : 'shared a new post';
}

let prefix = previewPrefix(message.sender, state);
return `${prefix}: ${message.message || getMediaPreview(message)}`;
let prefix = previewPrefix(message.sender, state, isOneOnOne);
return prefix
? `${prefix}: ${message.message || getMediaPreview(message)}`
: `${message.message || getMediaPreview(message)}`;
}

function previewPrefix(sender: Message['sender'], state: RootState) {
function previewPrefix(sender: Message['sender'], state: RootState, isOneOnOne: boolean) {
if (isOneOnOne) {
return '';
}

const user = currentUserSelector()(state);
return sender.userId === user.id ? 'You' : sender.firstName;
}
Expand Down

0 comments on commit 0f5298c

Please sign in to comment.