-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from ZickZenni/reactions
feat: add message reactions
- Loading branch information
Showing
16 changed files
with
213 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface AppConfig { | ||
assetsPath: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Snowflake } from './Snowflake'; | ||
|
||
export interface Emoji { | ||
animated: boolean; | ||
available: boolean; | ||
id: Snowflake; | ||
managed: boolean; | ||
name: string; | ||
require_colons: boolean; | ||
roles: any[]; | ||
version: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface Reaction { | ||
count: number; | ||
emoji: { | ||
id: string | null; | ||
name: string; | ||
animated?: boolean; | ||
}; | ||
me: true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { app } from 'electron'; | ||
import path from 'path'; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const resourcesPath = app.isPackaged | ||
? path.join(process.resourcesPath, 'assets') | ||
: path.join(__dirname, '../../assets'); | ||
|
||
export const assets = `${resourcesPath}/`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Reaction } from '@/discord/structures/Reaction'; | ||
import { Snowflake } from '@/discord/structures/Snowflake'; | ||
import { grabTheRightIcon } from '@/utils/emojiUtils'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
type MessageReactionProps = { | ||
messageId: Snowflake; | ||
reaction: Reaction; | ||
}; | ||
|
||
export default function MessageReaction({ | ||
messageId, | ||
reaction, | ||
}: MessageReactionProps) { | ||
const [url, setUrl] = useState<string | undefined>(undefined); | ||
const [color, setColor] = useState<string>('rgb(22,22,22)'); | ||
const [borderColor, setBorderColor] = useState<string>('rgb(22,22,22)'); | ||
|
||
useEffect(() => { | ||
const newId = | ||
Number(reaction.emoji.id) + Number(messageId) / (256 * 256 * 256); | ||
const str = newId.toString(); | ||
|
||
const r = Number(str.slice(0, 3)) % 255; | ||
const g = Number(str.slice(4, 7)) % 255; | ||
const b = Number(str.slice(8, 11)) % 255; | ||
|
||
setColor(`rgb(${r},${g},${b})`); | ||
setBorderColor(`rgb(${r * 0.7},${g * 0.7},${b * 0.7})`); | ||
|
||
if (reaction.emoji.id === null) { | ||
setUrl( | ||
`https://cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/svg/${grabTheRightIcon(reaction.emoji.name)}.svg`, | ||
); | ||
return; | ||
} | ||
|
||
setUrl( | ||
`https://cdn.discordapp.com/emojis/${reaction.emoji.id}.${reaction.emoji.animated ? 'gif' : 'png'}`, | ||
); | ||
}, [messageId, reaction]); | ||
|
||
return ( | ||
<div | ||
className={`MessageReaction ${reaction.me && 'MessageReaction--clicked'}`} | ||
style={{ background: color, borderColor }} | ||
role="presentation" | ||
> | ||
<img className="MessageReaction--emoji" src={url} alt="" /> | ||
<p className="MessageReaction--count">{reaction.count}</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.MessageReaction { | ||
display: flex; | ||
gap: 6px; | ||
min-width: 40px; | ||
height: 20px; | ||
padding: 10px 12px; | ||
border-radius: 10px; | ||
border: 2px solid #111; | ||
cursor: pointer; | ||
} | ||
|
||
.MessageReaction--clicked { | ||
border: 2px solid #2bff13 !important; | ||
} | ||
|
||
.MessageReaction:hover { | ||
transform: scale(1.02); | ||
} | ||
|
||
.MessageReaction--count { | ||
filter: drop-shadow(0px 1px 2px rgb(0, 0, 0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |