UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

266 lines 11 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useChannelActionItems = exports.getChannelActionItems = exports.buildDefaultChannelActionItems = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _useChannelActions = require("./useChannelActions"); var _useIsChannelMuted2 = require("../../components/ChannelPreview/hooks/useIsChannelMuted"); var _useUserMuteActive = require("../../components/Message/hooks/useUserMuteActive"); var _contexts = require("../../contexts"); var _ComponentsContext = require("../../contexts/componentsContext/ComponentsContext"); var _useChannelMembershipState = require("../useChannelMembershipState"); var _useIsDirectChat = require("../useIsDirectChat"); var _useStateStore2 = require("../useStateStore"); var _jsxRuntime = require("react/jsx-runtime"); var _jsxFileName = "/home/runner/work/stream-chat-react-native/stream-chat-react-native/package/src/hooks/actions/useChannelActionItems.tsx"; function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } var cachedDefaultIcons; var getDefaultIcons = () => { if (!cachedDefaultIcons) { cachedDefaultIcons = require("../../contexts/componentsContext/defaultComponents").defaultIcons; } return cachedDefaultIcons; }; var ChannelActionsIcon = ({ Icon, ...rest }) => { var _useTheme = (0, _contexts.useTheme)(), semantics = _useTheme.theme.semantics; return (0, _jsxRuntime.jsx)(Icon, { stroke: semantics.textSecondary, width: 20, height: 20, ...rest }); }; var buildDefaultChannelActionItems = channelActionItemsParams => { var _channelActionItemsPa = channelActionItemsParams.actions, deleteChannel = _channelActionItemsPa.deleteChannel, leave = _channelActionItemsPa.leave, muteChannel = _channelActionItemsPa.muteChannel, unmuteChannel = _channelActionItemsPa.unmuteChannel, muteUser = _channelActionItemsPa.muteUser, unmuteUser = _channelActionItemsPa.unmuteUser, blockUser = _channelActionItemsPa.blockUser, unblockUser = _channelActionItemsPa.unblockUser, pin = _channelActionItemsPa.pin, unpin = _channelActionItemsPa.unpin, channelMuteActive = channelActionItemsParams.channelMuteActive, _channelActionItemsPa2 = channelActionItemsParams.icons, icons = _channelActionItemsPa2 === void 0 ? getDefaultIcons() : _channelActionItemsPa2, isBlocked = channelActionItemsParams.isBlocked, isDirectChat = channelActionItemsParams.isDirectChat, isPinned = channelActionItemsParams.isPinned, surface = channelActionItemsParams.surface, userMuteActive = channelActionItemsParams.userMuteActive, t = channelActionItemsParams.t, channel = channelActionItemsParams.channel; var ownUserId = channel.getClient().userID; var actionItems = [{ action: channelMuteActive ? unmuteChannel : muteChannel, Icon: props => channelMuteActive ? (0, _jsxRuntime.jsx)(icons.Sound, { width: 20, height: 20, ...props }) : (0, _jsxRuntime.jsx)(icons.Mute, { width: 20, height: 20, ...props, stroke: undefined, fill: props.fill ?? props.stroke }), id: 'mute', label: isDirectChat ? channelMuteActive ? t('Unmute Chat') : t('Mute Chat') : channelMuteActive ? t('Unmute Group') : t('Mute Group'), placement: 'swipe', type: 'standard' }]; if (surface !== 'details') { actionItems.push({ action: isPinned ? unpin : pin, Icon: props => (0, _jsxRuntime.jsx)(ChannelActionsIcon, { Icon: isPinned ? icons.Unpin : icons.Pin, ...props }), id: 'pin', label: isDirectChat ? isPinned ? t('Unpin Chat') : t('Pin Chat') : isPinned ? t('Unpin Group') : t('Pin Group'), placement: 'sheet', type: 'standard' }); } if (isDirectChat) { actionItems.push({ action: userMuteActive ? unmuteUser : muteUser, Icon: props => userMuteActive ? (0, _jsxRuntime.jsx)(ChannelActionsIcon, { Icon: icons.Sound, ...props }) : (0, _jsxRuntime.jsx)(ChannelActionsIcon, { Icon: icons.Mute, ...props, fill: props.fill ?? props.stroke, stroke: undefined }), id: 'muteUser', label: userMuteActive ? t('Unmute User') : t('Mute User'), placement: 'sheet', type: 'standard' }); var blockUserWithConfirmation = (...args) => { var otherUser = (0, _useChannelActions.getOtherUserInDirectChannel)(channel)?.user; var name = otherUser?.name || otherUser?.id || ''; _reactNative.Alert.alert(t('Block {{ name }}', { name }), t("They won't be able to message or call you. You can unblock them later."), [{ style: 'cancel', text: t('Cancel') }, { onPress: function () { var _ref = (0, _asyncToGenerator2.default)(function* () { yield blockUser(...args); }); return function onPress() { return _ref.apply(this, arguments); }; }(), style: 'destructive', text: t('Block') }]); }; actionItems.push({ action: isBlocked ? unblockUser : blockUserWithConfirmation, Icon: props => (0, _jsxRuntime.jsx)(ChannelActionsIcon, { Icon: icons.BlockUser, ...props }), id: 'block', label: isBlocked ? t('Unblock User') : t('Block User'), placement: 'sheet', type: isBlocked ? 'standard' : 'destructive' }); } var leaveWithConfirmation = (...args) => { var channelName = channel.data?.name; var name; if (channelName) { name = channelName; } else if (isDirectChat) { var otherUser = (0, _useChannelActions.getOtherUserInDirectChannel)(channel)?.user; name = otherUser?.name || otherUser?.id || ''; } else { name = t('group'); } _reactNative.Alert.alert(isDirectChat ? t('Leave Chat') : t('Leave Group'), t("You'll stop receiving messages from {{ name }}. You can rejoin anytime.", { name }), [{ style: 'cancel', text: t('Cancel') }, { onPress: function () { var _ref2 = (0, _asyncToGenerator2.default)(function* () { yield leave(...args); }); return function onPress() { return _ref2.apply(this, arguments); }; }(), style: 'destructive', text: t('Leave') }]); }; actionItems.push({ action: leaveWithConfirmation, Icon: props => (0, _jsxRuntime.jsx)(ChannelActionsIcon, { Icon: icons.ArrowBoxLeft, ...props }), id: 'leave', label: isDirectChat ? t('Leave Chat') : t('Leave Group'), placement: 'sheet', type: 'destructive' }); if (channel.data?.created_by?.id === ownUserId) { actionItems.push({ action: (...args) => { var title = isDirectChat ? t('Delete chat') : t('Delete group'); var message = isDirectChat ? t("Are you sure you want to delete this chat? This can't be undone.") : t("Are you sure you want to delete this group? This can't be undone."); _reactNative.Alert.alert(title, message, [{ style: 'cancel', text: t('Cancel') }, { onPress: function () { var _ref3 = (0, _asyncToGenerator2.default)(function* () { yield deleteChannel(...args); }); return function onPress() { return _ref3.apply(this, arguments); }; }(), style: 'destructive', text: t('Delete') }]); }, Icon: props => (0, _jsxRuntime.jsx)(ChannelActionsIcon, { Icon: icons.Delete, ...props }), id: 'deleteChannel', label: isDirectChat ? t('Delete Chat') : t('Delete Group'), placement: 'sheet', type: 'destructive' }); } return actionItems; }; exports.buildDefaultChannelActionItems = buildDefaultChannelActionItems; var getChannelActionItems = ({ defaultItems }) => defaultItems; exports.getChannelActionItems = getChannelActionItems; var blockedUsersStateSelector = state => ({ userIds: state.userIds }); var useChannelActionItems = ({ channel, surface, getChannelActionItems: getChannelActionItemsProp = getChannelActionItems }) => { var _useTranslationContex = (0, _contexts.useTranslationContext)(), t = _useTranslationContex.t; var _useComponentsContext = (0, _ComponentsContext.useComponentsContext)(), icons = _useComponentsContext.icons; var membership = (0, _useChannelMembershipState.useChannelMembershipState)(channel); var channelActions = (0, _useChannelActions.useChannelActions)(channel); var isDirectChat = (0, _useIsDirectChat.useIsDirectChat)(channel); var isPinned = Boolean(membership?.pinned_at); var isArchived = Boolean(membership?.archived_at); var _useIsChannelMuted = (0, _useIsChannelMuted2.useIsChannelMuted)(channel), channelMuteActive = _useIsChannelMuted.muted; var otherUser = isDirectChat ? (0, _useChannelActions.getOtherUserInDirectChannel)(channel)?.user : undefined; var userMuteActive = (0, _useUserMuteActive.useUserMuteActive)(otherUser); var _useStateStore = (0, _useStateStore2.useStateStore)(channel.getClient().blockedUsers, blockedUsersStateSelector), blockedUserIds = _useStateStore.userIds; var isBlocked = isDirectChat ? blockedUserIds.includes((0, _useChannelActions.getOtherUserInDirectChannel)(channel)?.user?.id ?? '') : undefined; var channelActionItemsParams = (0, _react.useMemo)(() => ({ actions: channelActions, channel, channelMuteActive, icons, isArchived, isBlocked, isDirectChat, isPinned, surface, t, userMuteActive }), [channel, channelActions, channelMuteActive, icons, isArchived, isBlocked, isDirectChat, isPinned, surface, t, userMuteActive]); var defaultItems = (0, _react.useMemo)(() => buildDefaultChannelActionItems(channelActionItemsParams), [channelActionItemsParams]); return (0, _react.useMemo)(() => getChannelActionItemsProp({ context: channelActionItemsParams, defaultItems }), [channelActionItemsParams, defaultItems, getChannelActionItemsProp]); }; exports.useChannelActionItems = useChannelActionItems; //# sourceMappingURL=useChannelActionItems.js.map