Skip to content

Commit

Permalink
feat: add twemoji support
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Rohloff committed Sep 19, 2024
1 parent 1930671 commit da04831
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/discord/structures/Message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Attachment } from './Attachment';
import { Reaction } from './Reaction';
import { Snowflake } from './Snowflake';
import { IUserData } from './user/BaseUser';

Expand All @@ -9,4 +10,5 @@ export interface Message {
id: Snowflake;
author: IUserData;
channel_id: Snowflake;
reactions?: Reaction[];
}
8 changes: 8 additions & 0 deletions src/discord/structures/Reaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Reaction {
count: number;
emoji: {
id: string | null;
name: string;
};
me: true;
}
1 change: 1 addition & 0 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '@/styles/channel/ChannelButton.css';
import '@/styles/channel/ChannelList.css';
import '@/styles/channel/Message.css';
import '@/styles/channel/MessageAttachment.css';
import '@/styles/channel/MessageReaction.css';

import '@/styles/directmessage/DirectMessageButton.css';
import '@/styles/directmessage/DirectMessageList.css';
Expand Down
14 changes: 14 additions & 0 deletions src/renderer/components/channel/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Message as MessageData } from '@/discord/structures/Message';
import RendererUser from '@/discord/structures/user/RendererUser';
import useGif from '@/hooks/useGif';
import MessageAttachment from './MessageAttachment';
import MessageReaction from './MessageReaction';

type MessageProps = {
message: MessageData;
Expand All @@ -13,6 +14,8 @@ export default function Message({ message }: MessageProps) {
const author = new RendererUser(message.author);
const authorDecorationUrl = author.getAvatarDecorationUrl();

const reactions = message.reactions ?? [];

return (
<div className="Message">
<div className="Message--author-container">
Expand Down Expand Up @@ -49,6 +52,17 @@ export default function Message({ message }: MessageProps) {
);
})}
</div>

<div className="Message--reactions-container">
{reactions.map((reaction) => {
return (
<MessageReaction
key={`Reaction:${reaction.emoji.name}`}
reaction={reaction}
/>
);
})}
</div>
</div>
</div>
);
Expand Down
22 changes: 22 additions & 0 deletions src/renderer/components/channel/MessageReaction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Reaction } from '@/discord/structures/Reaction';
import { grabTheRightIcon } from '@/utils/emojiUtils';

type MessageReactionProps = {
reaction: Reaction;
};

export default function MessageReaction({ reaction }: MessageReactionProps) {
const twemojiHash =
reaction.emoji.id === null ? grabTheRightIcon(reaction.emoji.name) : null;

const url = twemojiHash
? `https://cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/svg/${twemojiHash}.svg`
: undefined;

return (
<div className="MessageReaction">
<img className="MessageReaction-emoji" src={url} alt="" />
<p className="MessageReaction-count">{reaction.count}</p>
</div>
);
}
2 changes: 1 addition & 1 deletion src/renderer/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
content="default-src 'self';
script-src 'self';
font-src 'self' https://fonts.gstatic.com;
img-src 'self' https://cdn.discordapp.com https://*.tenor.com;
img-src 'self' https://cdn.discordapp.com https://*.tenor.com https://cdn.jsdelivr.net;
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;"
/>
<link rel="preconnect" href="https://fonts.googleapis.com" />
Expand Down
18 changes: 15 additions & 3 deletions src/renderer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { createRoot } from 'react-dom/client';
import App from './App';
import { AppConfig } from '../common/appconfig';

const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);
root.render(<App />);
// eslint-disable-next-line import/prefer-default-export, import/no-mutable-exports
export let config: AppConfig | null = null;

window.electron.ipcRenderer
.invoke('app:config')
.then((conf: AppConfig) => {
config = conf;

const container = document.getElementById('root') as HTMLElement;
const root = createRoot(container);
root.render(<App />);
return true;
})
.catch((err) => console.error(err));
8 changes: 8 additions & 0 deletions src/renderer/styles/channel/Message.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@
max-width: 300px;
max-height: 300px;
}

.Message--reactions-container {
margin-top: 6px;
width: 100%;
flex-wrap: wrap;
display: flex;
gap: 6px;
}
15 changes: 15 additions & 0 deletions src/renderer/styles/channel/MessageReaction.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.MessageReaction {
display: flex;
gap: 6px;
min-width: 40px;
height: 20px;
padding: 10px 12px;
background: #4d7cf227;
border-radius: 10px;
border: 2px solid #4d7cf263;
cursor: pointer;
}

.MessageReaction:hover {
transform: scale(1.02);
}
33 changes: 33 additions & 0 deletions src/renderer/utils/emojiUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const UFE0Fg = /\uFE0F/g;

// avoid using a string literal like '\u200D' here because minifiers expand it inline
const U200D = String.fromCharCode(0x200d);

function toCodePoint(unicodeSurrogates: string, sep?: string) {
const r = [];
let c = 0;
let p = 0;
let i = 0;

while (i < unicodeSurrogates.length) {
// eslint-disable-next-line no-plusplus
c = unicodeSurrogates.charCodeAt(i++);
if (p) {
// eslint-disable-next-line no-bitwise
r.push((0x10000 + ((p - 0xd800) << 10) + (c - 0xdc00)).toString(16));
p = 0;
} else if (c >= 0xd800 && c <= 0xdbff) {
p = c;
} else {
r.push(c.toString(16));
}
}
return r.join(sep ?? '-');
}

// eslint-disable-next-line import/prefer-default-export
export function grabTheRightIcon(emoji: string) {
return toCodePoint(
emoji.indexOf(U200D) < 0 ? emoji.replace(UFE0Fg, '') : emoji,
);
}

0 comments on commit da04831

Please sign in to comment.