Skip to content

Commit 6437f54

Browse files
authored
fix: emoji not getting rendered as avatar (#6382)
1 parent 05a0b49 commit 6437f54

File tree

5 files changed

+1700
-5
lines changed

5 files changed

+1700
-5
lines changed

app/containers/Avatar/Avatar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ const Avatar = React.memo(
5454
value={{
5555
getCustomEmoji
5656
}}>
57-
<Emoji block={{ type: 'EMOJI', value: { type: 'PLAIN_TEXT', value: emoji }, shortCode: emoji }} style={avatarStyle} />
57+
<Emoji
58+
block={{ type: 'EMOJI', value: { type: 'PLAIN_TEXT', value: emoji }, shortCode: emoji }}
59+
style={avatarStyle}
60+
isAvatar={true}
61+
/>
5862
</MarkdownContext.Provider>
5963
);
6064
} else {

app/containers/markdown/__snapshots__/Markdown.test.tsx.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ exports[`Story Snapshots: Emoji should match snapshot 1`] = `
608608
"textAlign": "left",
609609
},
610610
{},
611+
false,
611612
]
612613
}
613614
>
@@ -646,6 +647,7 @@ exports[`Story Snapshots: Emoji should match snapshot 1`] = `
646647
"textAlign": "left",
647648
},
648649
{},
650+
false,
649651
]
650652
}
651653
>
@@ -888,6 +890,7 @@ exports[`Story Snapshots: Emoji should match snapshot 1`] = `
888890
"textAlign": "left",
889891
},
890892
{},
893+
false,
891894
]
892895
}
893896
>

app/containers/markdown/components/emoji/Emoji.tsx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,44 @@ import CustomEmoji from '../../../EmojiPicker/CustomEmoji';
1010
import MarkdownContext from '../../contexts/MarkdownContext';
1111
import { useAppSelector } from '../../../../lib/hooks';
1212
import { getUserSelector } from '../../../../selectors/login';
13+
import { useResponsiveLayout } from '../../../../lib/hooks/useResponsiveLayout/useResponsiveLayout';
1314

1415
interface IEmojiProps {
1516
block: EmojiProps;
1617
isBigEmoji?: boolean;
1718
style?: object;
1819
index?: number;
20+
isAvatar?: boolean;
1921
}
2022

21-
const Emoji = ({ block, isBigEmoji, style = {}, index }: IEmojiProps) => {
23+
function getEmojiToken(block: EmojiProps, isAvatar: boolean) {
24+
if ('unicode' in block) {
25+
return block.unicode;
26+
}
27+
28+
if (isAvatar) {
29+
return block.value?.value;
30+
}
31+
32+
return block?.shortCode ? `:${block.shortCode}:` : `:${block.value?.value}:`;
33+
}
34+
35+
const Emoji = ({ block, isBigEmoji, style = {}, index, isAvatar = false }: IEmojiProps) => {
2236
const { colors } = useTheme();
2337
const { getCustomEmoji } = useContext(MarkdownContext);
2438
const { fontScale } = useWindowDimensions();
39+
const { fontScaleLimited } = useResponsiveLayout();
2540
const { formatShortnameToUnicode } = useShortnameToUnicode();
2641
const spaceLeft = index && index > 0 ? ' ' : '';
2742
const convertAsciiEmoji = useAppSelector(state => getUserSelector(state)?.settings?.preferences?.convertAsciiEmoji);
43+
2844
if ('unicode' in block) {
2945
return <Text style={[{ color: colors.fontDefault }, isBigEmoji ? styles.textBig : styles.text]}>{block.unicode}</Text>;
3046
}
3147

32-
const emojiToken = block?.shortCode ? `:${block.shortCode}:` : `:${block.value?.value}:`;
48+
const emojiToken = getEmojiToken(block, isAvatar);
3349
const emojiUnicode = formatShortnameToUnicode(emojiToken);
34-
const emoji = getCustomEmoji?.(block.value?.value);
50+
const emoji = getCustomEmoji?.(block.value?.value.replace(/\:/g, ''));
3551
const isAsciiEmoji = !!block?.shortCode && block.value?.value !== block?.shortCode;
3652
const displayAsciiEmoji = !convertAsciiEmoji && isAsciiEmoji && !!block.value;
3753
const customEmojiSize = {
@@ -44,12 +60,25 @@ const Emoji = ({ block, isBigEmoji, style = {}, index }: IEmojiProps) => {
4460
height: 30 * fontScale
4561
};
4662

63+
const avatarStyle = {
64+
fontSize: 30 * fontScaleLimited,
65+
lineHeight: 30 * fontScaleLimited,
66+
textAlign: 'center',
67+
textAlignVertical: 'center'
68+
};
69+
4770
if (emoji) {
4871
return <CustomEmoji style={[isBigEmoji ? customEmojiBigSize : customEmojiSize, style]} emoji={emoji} />;
4972
}
73+
5074
return (
5175
<Text
52-
style={[{ color: colors.fontDefault }, isBigEmoji && emojiToken !== emojiUnicode ? styles.textBig : styles.text, style]}>
76+
style={[
77+
{ color: colors.fontDefault },
78+
isBigEmoji && emojiToken !== emojiUnicode ? styles.textBig : styles.text,
79+
style,
80+
isAvatar && avatarStyle
81+
]}>
5382
{spaceLeft}
5483
{displayAsciiEmoji ? <Plain value={block.value!.value} /> : emojiUnicode}
5584
</Text>

app/containers/message/Message.stories.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,16 @@ export const LongNameUser = () => (
19201920
</>
19211921
);
19221922

1923+
export const Avatar = () => (
1924+
<>
1925+
<Message msg={'This message has no avatar.'} author={{ username: 'no.avatar', name: 'No Avatar' }} />
1926+
<Message msg={"This message uses the user's profile picture as the avatar."} author={author} />
1927+
<Message msg={'This message uses a Unicode emoji as the avatar.'} author={author} emoji={':smile:'} />
1928+
<Message msg={'This message uses a custom animated emoji as the avatar.'} author={author} emoji={':marioparty:'} />
1929+
<Message msg={'This message uses a custom static emoji as the avatar.'} author={author} emoji={':react_rocket:'} />
1930+
</>
1931+
);
1932+
19231933
export const LongNameUserLargeFont = () => (
19241934
<>
19251935
<MessageLargeFont msg={'this is a normal message'} author={longNameAuthor} />

0 commit comments

Comments
 (0)