{"version":3,"file":"useMuteHandler.mjs","names":[],"sources":["../../../../../src/components/Message/hooks/useMuteHandler.ts"],"sourcesContent":["import { isUserMuted, validateAndGetMessage } from '../utils';\n\nimport { useChatContext } from '../../../context/ChatContext';\nimport { useTranslationContext } from '../../../context/TranslationContext';\n\nimport type { LocalMessage, UserResponse } from 'stream-chat';\n\nimport type { ReactEventHandler } from '../types';\n\nexport const missingUseMuteHandlerParamsWarning =\n  'useMuteHandler was called but it is missing one or more necessary parameter.';\n\nexport type MuteUserNotifications = {\n  getErrorNotification?: (user: UserResponse) => string;\n  getSuccessNotification?: (user: UserResponse) => string;\n  notify?: (notificationText: string, type: 'success' | 'error') => void;\n};\n\nexport const useMuteHandler = (\n  message?: LocalMessage,\n  notifications: MuteUserNotifications = {},\n): ReactEventHandler => {\n  const { client } = useChatContext();\n  const { t } = useTranslationContext();\n\n  return async (event) => {\n    event.preventDefault();\n\n    const { getErrorNotification, getSuccessNotification, notify } = notifications;\n\n    // The action runs even without `notify` (`Message` passes none); it only reports the outcome.\n    if (!message?.user || !client) {\n      throw new Error(missingUseMuteHandlerParamsWarning);\n    }\n    const mutes = client.mutedUsersStore.getLatestValue().mutedUsers ?? [];\n\n    if (!isUserMuted(message, mutes)) {\n      try {\n        await client.moderation.mute({ target_ids: [message.user.id] });\n\n        if (!notify) return;\n        const successMessage =\n          (getSuccessNotification &&\n            validateAndGetMessage(getSuccessNotification, [message.user])) ||\n          t('common.muted.label', '{{ user }} has been muted', {\n            user: message.user.name || message.user.id,\n          });\n\n        if (typeof successMessage === 'string') notify(successMessage, 'success');\n      } catch (e) {\n        if (notify) {\n          const errorMessage =\n            (getErrorNotification &&\n              validateAndGetMessage(getErrorNotification, [message.user])) ||\n            t('common.errorMutingUser.label', 'Error muting a user ...');\n\n          if (typeof errorMessage === 'string') notify(errorMessage, 'error');\n        }\n        throw e;\n      }\n    } else {\n      try {\n        await client.moderation.unmute({ target_ids: [message.user.id] });\n\n        if (!notify) return;\n        const successMessage =\n          (getSuccessNotification &&\n            validateAndGetMessage(getSuccessNotification, [message.user])) ||\n          t('common.unmuted.label', '{{ user }} has been unmuted', {\n            user: message.user.name || message.user.id,\n          });\n\n        if (typeof successMessage === 'string') notify(successMessage, 'success');\n      } catch (e) {\n        if (notify) {\n          const errorMessage =\n            (getErrorNotification &&\n              validateAndGetMessage(getErrorNotification, [message.user])) ||\n            t('common.errorUnmutingUser.label', 'Error unmuting a user ...');\n\n          if (typeof errorMessage === 'string') notify(errorMessage, 'error');\n        }\n        throw e;\n      }\n    }\n  };\n};\n"],"mappings":";;;;AASA,IAAa,qCACX;AAQF,IAAa,kBACX,SACA,gBAAuC,CAAC,MAClB;CACtB,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,EAAE,MAAM,sBAAsB;CAEpC,OAAO,OAAO,UAAU;EACtB,MAAM,eAAe;EAErB,MAAM,EAAE,sBAAsB,wBAAwB,WAAW;EAGjE,IAAI,CAAC,SAAS,QAAQ,CAAC,QACrB,MAAM,IAAI,MAAM,kCAAkC;EAIpD,IAAI,CAAC,YAAY,SAFH,OAAO,gBAAgB,eAAe,CAAC,CAAC,cAAc,CAAC,CAEtC,GAC7B,IAAI;GACF,MAAM,OAAO,WAAW,KAAK,EAAE,YAAY,CAAC,QAAQ,KAAK,EAAE,EAAE,CAAC;GAE9D,IAAI,CAAC,QAAQ;GACb,MAAM,iBACH,0BACC,sBAAsB,wBAAwB,CAAC,QAAQ,IAAI,CAAC,KAC9D,EAAE,sBAAsB,6BAA6B,EACnD,MAAM,QAAQ,KAAK,QAAQ,QAAQ,KAAK,GAC1C,CAAC;GAEH,IAAI,OAAO,mBAAmB,UAAU,OAAO,gBAAgB,SAAS;EAC1E,SAAS,GAAG;GACV,IAAI,QAAQ;IACV,MAAM,eACH,wBACC,sBAAsB,sBAAsB,CAAC,QAAQ,IAAI,CAAC,KAC5D,EAAE,gCAAgC,yBAAyB;IAE7D,IAAI,OAAO,iBAAiB,UAAU,OAAO,cAAc,OAAO;GACpE;G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