Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: broken client-side updates for recent emoji list #33808

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-mice-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fixes client-side updates for recent emoji list when custom emojis are modified.
9 changes: 6 additions & 3 deletions apps/meteor/app/emoji-custom/client/lib/emojiCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { escapeRegExp } from '@rocket.chat/string-helpers';
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';

import { emoji, updateRecent } from '../../../emoji/client';
import { emoji, removeFromRecent, replaceEmojiInRecent } from '../../../emoji/client';
import { CachedCollectionManager } from '../../../ui-cached-collection/client';
import { getURL } from '../../../utils/client';
import { sdk } from '../../../utils/client/lib/SDKClient';
Expand Down Expand Up @@ -49,7 +49,8 @@ export const deleteEmojiCustom = (emojiData: IEmoji) => {
}
}
}
updateRecent(['rocket']);

removeFromRecent(emojiData.name, emoji.packages.base.emojisByCategory.recent);
};

export const updateEmojiCustom = (emojiData: IEmoji) => {
Expand Down Expand Up @@ -94,7 +95,9 @@ export const updateEmojiCustom = (emojiData: IEmoji) => {
}
}

updateRecent(['rocket']);
if (previousExists) {
replaceEmojiInRecent(emojiData.previousName, emojiData.name);
}
};

const customRender = (html: string) => {
Expand Down
13 changes: 11 additions & 2 deletions apps/meteor/app/emoji/client/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ export const getEmojisBySearchTerm = (
return emojis;
};

export const removeFromRecent = (emoji: string, recentEmojis: string[], setRecentEmojis: (emojis: string[]) => void) => {
export const removeFromRecent = (emoji: string, recentEmojis: string[], setRecentEmojis?: (emojis: string[]) => void) => {
const _emoji = emoji.replace(/(^:|:$)/g, '');
const pos = recentEmojis.indexOf(_emoji as never);

if (pos === -1) {
return;
}
recentEmojis.splice(pos, 1);
setRecentEmojis(recentEmojis);
setRecentEmojis?.(recentEmojis);
};

export const updateRecent = (recentList: string[]) => {
Expand All @@ -156,6 +156,15 @@ export const updateRecent = (recentList: string[]) => {
});
};

export const replaceEmojiInRecent = (oldEmoji: string, newEmoji: string) => {
MarcosSpessatto marked this conversation as resolved.
Show resolved Hide resolved
const recentPkgList: string[] = emoji.packages.base.emojisByCategory.recent;
const pos = recentPkgList.indexOf(oldEmoji);
MarcosSpessatto marked this conversation as resolved.
Show resolved Hide resolved

if (pos !== -1) {
recentPkgList[pos] = newEmoji;
}
};

const getEmojiRender = (emojiName: string) => {
const emojiPackageName = emoji.list[emojiName]?.emojiPackage;
const emojiPackage = emoji.packages[emojiPackageName];
Expand Down
51 changes: 51 additions & 0 deletions apps/meteor/tests/unit/app/emoji/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from 'chai';
import { describe, it, beforeEach } from 'mocha';

import { updateRecent, removeFromRecent, replaceEmojiInRecent } from '../../../../app/emoji/client/helpers';
import { emoji } from '../../../../app/emoji/client/lib';

describe('Emoji Client Helpers', () => {
beforeEach(() => {
emoji.packages.base.emojisByCategory.recent = [];
});

describe('updateRecent', () => {
it('should update recent emojis with the provided emojis', () => {
const recentEmojis = ['emoji1', 'emoji2'];
updateRecent(recentEmojis);
expect(emoji.packages.base.emojisByCategory.recent).to.contain('emoji1');
expect(emoji.packages.base.emojisByCategory.recent).to.contain('emoji2');
});
});

describe('removeFromRecent', () => {
it('should remove a specific emoji from recent emojis', () => {
emoji.packages.base.emojisByCategory.recent = ['emoji1', 'emoji2', 'emoji3'];
removeFromRecent('emoji2', emoji.packages.base.emojisByCategory.recent);
expect(emoji.packages.base.emojisByCategory.recent).to.not.include('emoji2');
expect(emoji.packages.base.emojisByCategory.recent).to.deep.equal(['emoji1', 'emoji3']);
});

it('should do nothing if the emoji is not in the recent list', () => {
emoji.packages.base.emojisByCategory.recent = ['emoji1', 'emoji2'];
removeFromRecent('emoji3', emoji.packages.base.emojisByCategory.recent);
expect(emoji.packages.base.emojisByCategory.recent).to.deep.equal(['emoji1', 'emoji2']);
});
});

describe('replaceEmojiInRecent', () => {
it('should replace an existing emoji with a new one in recent emojis', () => {
emoji.packages.base.emojisByCategory.recent = ['emoji1', 'emoji2', 'emoji3'];
replaceEmojiInRecent('emoji2', 'emoji4');
expect(emoji.packages.base.emojisByCategory.recent).to.not.include('emoji2');
expect(emoji.packages.base.emojisByCategory.recent).to.include('emoji4');
expect(emoji.packages.base.emojisByCategory.recent).to.deep.equal(['emoji1', 'emoji4', 'emoji3']);
});

it('should do nothing if the emoji to replace is not in the recent list', () => {
emoji.packages.base.emojisByCategory.recent = ['emoji1', 'emoji2'];
replaceEmojiInRecent('emoji3', 'emoji4');
expect(emoji.packages.base.emojisByCategory.recent).to.deep.equal(['emoji1', 'emoji2']);
});
});
});
Loading