From 6ce1344af1d37e6f488a92bb1df86dda0a00044f Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Thu, 9 Jun 2022 15:56:19 +0200 Subject: [PATCH] unify reaction type --- .../src/client/community/get-reactions.ts | 32 ++++++------------- .../src/client/community/map-chat-message.ts | 12 +++---- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/packages/status-js/src/client/community/get-reactions.ts b/packages/status-js/src/client/community/get-reactions.ts index 247e6e43..52aaa186 100644 --- a/packages/status-js/src/client/community/get-reactions.ts +++ b/packages/status-js/src/client/community/get-reactions.ts @@ -1,12 +1,6 @@ -import { EmojiReaction } from '../../../protos/emoji-reaction' +import type { EmojiReaction } from '../../../protos/emoji-reaction' -type Reaction = - | 'heart' - | 'thumbs-up' - | 'thumbs-down' - | 'smile' - | 'sad' - | 'angry' +type Reaction = Exclude<`${EmojiReaction.Type}`, 'UNKNOWN_EMOJI_REACTION_TYPE'> export type Reactions = { [key in Reaction]: { @@ -15,38 +9,30 @@ export type Reactions = { } } -const REACTION_MAP: Record = { - [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( reaction: EmojiReaction, reactions: Reactions, isMe: boolean ) { - // fixme!: type - const type = REACTION_MAP[reaction.type] as Reaction - const isRetracted = reaction.retracted + const { type, retracted } = reaction + + if (type === 'UNKNOWN_EMOJI_REACTION_TYPE') { + return reactions + } const _reaction = { count: reactions[type].count, me: reactions[type].me, } - if (isRetracted && _reaction.count !== 0) { + if (retracted && _reaction.count !== 0) { _reaction.count -= 1 } else { _reaction.count += 1 } if (isMe) { - _reaction.me = isRetracted ? false : true + _reaction.me = retracted ? false : true } return { diff --git a/packages/status-js/src/client/community/map-chat-message.ts b/packages/status-js/src/client/community/map-chat-message.ts index 97ca96ea..3819e10c 100644 --- a/packages/status-js/src/client/community/map-chat-message.ts +++ b/packages/status-js/src/client/community/map-chat-message.ts @@ -19,27 +19,27 @@ export function mapChatMessage( channelId, pinned: false, reactions: { - 'thumbs-up': { + THUMBS_UP: { count: 0, me: false, }, - 'thumbs-down': { + THUMBS_DOWN: { count: 0, me: false, }, - heart: { + LOVE: { count: 0, me: false, }, - smile: { + LAUGH: { count: 0, me: false, }, - sad: { + SAD: { count: 0, me: false, }, - angry: { + ANGRY: { count: 0, me: false, },