OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / chat / reaction / chat-reaction.reducer.js

62 lines · javascript ref:
import { parseCoreReactions as t, shouldUpdateReactions as e } from "./chat-reaction.model"
import { sharedCoreMessagesUpdate as o } from "../../api/data.action"
import { createMessageId as n } from "../message"
import { setChatReactions as r, setChatReactionMembers as a } from "./chat-reaction.action"
const c = (t, e) => {
    const o = new Map(),
      n = t.reactions
    for (let t = 0; t < n.length; t += 1) {
      const e = n[t]
      o.set(e.text, e)
    }
    return {
      ...e,
      reactions: e.reactions.map((t) => {
        const e = o.get(t.text)
        return e
          ? e.count === t.count
            ? e
            : { text: t.text, count: t.count, latest: e.latest }
          : t
      }),
    }
  },
  s = (t, e) => {
    const o = { ...t.byId }
    for (let t = 0; t < e.length; t += 1) {
      const n = e[t],
        { id: r } = n,
        a = o[r]
      o[r] = a ? c(a, n) : n
    }
    return { ...t, byId: o }
  }
export const chatReactionReducer = (c, i) => {
  switch (i.type) {
    case r.type:
      return s(c, [i.payload])
    case o.type: {
      const { byId: o } = c,
        r = i.payload.coreEvents || [],
        a = []
      for (let c = 0; c < r.length; c += 1) {
        const s = r[c],
          i = n(s),
          d = o[i],
          p = t(s.reactions, i)
        e(d, p) && a.push(p)
      }
      return a.length ? s(c, a) : c
    }
    case a.type: {
      const { messageId: t, id: e, memberIds: o } = i.payload,
        n = c.byId[t]
      if (!n || !n.reactions.find((t) => t.text === e)) return c
      const r = n.reactions.map((t) => (t.text !== e ? t : { ...t, latest: o }))
      return { ...c, byId: { ...c.byId, [t]: { ...n, reactions: r } } }
    }
    default:
      return c
  }
}