OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / notification / notification.reducer.js

72 lines · javascript ref:
import { createSlice as t } from "@reduxjs/toolkit"
import { NOTIFICATION_TYPE as o } from "./notification.constants"
import { getNotificationId as e } from "./notification.model"
export const NOTIFICATION_STATE_KEY = "notification"
export const notificationSlice = t({
  name: "notification",
  initialState: { notifications: {}, frequencyResetInterval: 5e3 },
  reducers: {
    addNotification: (t, { payload: o }) => {
      const i = e(o),
        a = (t.notifications[i]?.count || 0) + 1
      t.notifications[i] = { ...o, id: i, count: a }
    },
    setNotificationFrequenceResetInterval: (t, { payload: o }) => {
      t.frequencyResetInterval = o
    },
    removeNotification: (t, { payload: o }) => {
      delete t.notifications[o]
    },
  },
})
export const getNotificationState = (t) => t.notification
export const getNotifications = (t) => getNotificationState(t).notifications
export const getNotificationFrequenceResetInterval = (t) =>
  getNotificationState(t).frequencyResetInterval
export const {
  addNotification: addNotification,
  removeNotification: removeNotification,
  setNotificationFrequenceResetInterval: setNotificationFrequenceResetInterval,
} = notificationSlice.actions
export const userSuccessNotification = ({
  title: t = "",
  message: e,
  autoClose: i = !0,
  compactMode: a = !1,
}) => addNotification({ type: o.SUCCESS, title: t, message: e, autoClose: i, compactMode: a })
export const userErrorNotification = ({
  title: t,
  message: e,
  stack: i,
  showDetails: a,
  autoClose: c = !0,
  compactMode: n = !1,
}) =>
  addNotification({
    type: o.ERROR,
    title: t,
    message: e,
    stack: i,
    showDetails: a,
    autoClose: c,
    compactMode: n,
  })
export const userWarnNotification = ({
  title: t = "",
  message: e = "",
  autoClose: i = !0,
  compactMode: a = !1,
  roomId: c,
}) =>
  addNotification({ type: o.WARN, title: t, message: e, autoClose: i, compactMode: a, roomId: c })
export const userInfoNotification = ({
  title: t = "",
  message: e,
  autoClose: i = !0,
  compactMode: a = !1,
}) => addNotification({ type: o.INFO, title: t, message: e, autoClose: i, compactMode: a })
export const userCustomNotification = ({ code: t, title: e, message: i, ...a }) =>
  addNotification({ type: o.CUSTOM, code: t, title: e, message: i, ...a })
export const getNotificationInitialState = notificationSlice.getInitialState
export const notificationReducer = notificationSlice.reducer