unify reaction type

This commit is contained in:
Pavel Prichodko 2022-06-09 15:56:19 +02:00 committed by Felicio Mununga
parent 54020b4be6
commit 6ce1344af1
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
2 changed files with 15 additions and 29 deletions

View File

@ -1,12 +1,6 @@
import { EmojiReaction } from '../../../protos/emoji-reaction' import type { EmojiReaction } from '../../../protos/emoji-reaction'
type Reaction = type Reaction = Exclude<`${EmojiReaction.Type}`, 'UNKNOWN_EMOJI_REACTION_TYPE'>
| 'heart'
| 'thumbs-up'
| 'thumbs-down'
| 'smile'
| 'sad'
| 'angry'
export type Reactions = { export type Reactions = {
[key in Reaction]: { [key in Reaction]: {
@ -15,38 +9,30 @@ export type Reactions = {
} }
} }
const REACTION_MAP: Record<EmojiReaction.Type, string> = {
[EmojiReaction.Type.LOVE]: 'heart',
[EmojiReaction.Type.THUMBS_UP]: 'thumbs-up',
[EmojiReaction.Type.THUMBS_DOWN]: 'thumbs-down',
[EmojiReaction.Type.LAUGH]: 'smile',
[EmojiReaction.Type.SAD]: 'sad',
[EmojiReaction.Type.ANGRY]: 'angry',
[EmojiReaction.Type.UNKNOWN_EMOJI_REACTION_TYPE]: 'unknown',
}
export function getReactions( export function getReactions(
reaction: EmojiReaction, reaction: EmojiReaction,
reactions: Reactions, reactions: Reactions,
isMe: boolean isMe: boolean
) { ) {
// fixme!: type const { type, retracted } = reaction
const type = REACTION_MAP[reaction.type] as Reaction
const isRetracted = reaction.retracted if (type === 'UNKNOWN_EMOJI_REACTION_TYPE') {
return reactions
}
const _reaction = { const _reaction = {
count: reactions[type].count, count: reactions[type].count,
me: reactions[type].me, me: reactions[type].me,
} }
if (isRetracted && _reaction.count !== 0) { if (retracted && _reaction.count !== 0) {
_reaction.count -= 1 _reaction.count -= 1
} else { } else {
_reaction.count += 1 _reaction.count += 1
} }
if (isMe) { if (isMe) {
_reaction.me = isRetracted ? false : true _reaction.me = retracted ? false : true
} }
return { return {

View File

@ -19,27 +19,27 @@ export function mapChatMessage(
channelId, channelId,
pinned: false, pinned: false,
reactions: { reactions: {
'thumbs-up': { THUMBS_UP: {
count: 0, count: 0,
me: false, me: false,
}, },
'thumbs-down': { THUMBS_DOWN: {
count: 0, count: 0,
me: false, me: false,
}, },
heart: { LOVE: {
count: 0, count: 0,
me: false, me: false,
}, },
smile: { LAUGH: {
count: 0, count: 0,
me: false, me: false,
}, },
sad: { SAD: {
count: 0, count: 0,
me: false, me: false,
}, },
angry: { ANGRY: {
count: 0, count: 0,
me: false, me: false,
}, },