UNPKG

stream-chat-react-native-core

Version:

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

186 lines 6.43 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useFetchReactions = exports.upsertReactionInList = exports.removeReactionFromList = exports.reconcileUpdatedReactionInList = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = require("react"); var _ChatContext = require("../../../contexts/chatContext/ChatContext"); var _TranslationContext = require("../../../contexts/translationContext/TranslationContext"); var _Notifications = require("../../Notifications"); var isSameReaction = (left, right) => left.user_id === right.user_id && left.type === right.type; var upsertReactionInList = ({ prevReactions, reaction, reactionType }) => { if (!reactionType) { return [reaction, ...prevReactions.filter(currentReaction => !isSameReaction(currentReaction, reaction))]; } if (reaction.type !== reactionType) { return prevReactions; } return [reaction, ...prevReactions.filter(currentReaction => currentReaction.user_id !== reaction.user_id)]; }; exports.upsertReactionInList = upsertReactionInList; var reconcileUpdatedReactionInList = ({ prevReactions, reaction, reactionType }) => { if (!reactionType) { return [reaction, ...prevReactions.filter(currentReaction => !isSameReaction(currentReaction, reaction))]; } if (reaction.type !== reactionType) { return prevReactions.filter(currentReaction => currentReaction.user_id !== reaction.user_id); } return [reaction, ...prevReactions.filter(currentReaction => currentReaction.user_id !== reaction.user_id)]; }; exports.reconcileUpdatedReactionInList = reconcileUpdatedReactionInList; var removeReactionFromList = ({ prevReactions, reaction, reactionType }) => { if (!reactionType) { return prevReactions.filter(currentReaction => !isSameReaction(currentReaction, reaction)); } if (reaction.type !== reactionType) { return prevReactions; } return prevReactions.filter(currentReaction => currentReaction.user_id !== reaction.user_id); }; exports.removeReactionFromList = removeReactionFromList; var useFetchReactions = ({ limit = 25, message, reactionType, sort }) => { var _useState = (0, _react.useState)([]), _useState2 = (0, _slicedToArray2.default)(_useState, 2), reactions = _useState2[0], setReactions = _useState2[1]; var _useState3 = (0, _react.useState)(true), _useState4 = (0, _slicedToArray2.default)(_useState3, 2), loading = _useState4[0], setLoading = _useState4[1]; var _useState5 = (0, _react.useState)(undefined), _useState6 = (0, _slicedToArray2.default)(_useState5, 2), next = _useState6[0], setNext = _useState6[1]; var messageId = message == null ? void 0 : message.id; var _useChatContext = (0, _ChatContext.useChatContext)(), client = _useChatContext.client; var _useTranslationContex = (0, _TranslationContext.useTranslationContext)(), t = _useTranslationContex.t; var _useNotificationApi = (0, _Notifications.useNotificationApi)(), addNotification = _useNotificationApi.addNotification; var sortString = (0, _react.useMemo)(() => JSON.stringify(sort), [sort]); var fetchReactions = (0, _react.useCallback)(function () { var _ref = (0, _asyncToGenerator2.default)(function* (next) { if (!messageId) { return; } try { var response = yield client.queryReactions(messageId, reactionType ? { type: reactionType } : {}, sort, { limit, next }); if (response) { setNext(response.next); setReactions(prevReactions => next ? [...prevReactions, ...response.reactions] : response.reactions); setLoading(false); } } catch (error) { addNotification({ message: t('Error fetching reactions'), options: { ...(error instanceof Error ? { originalError: error } : {}), severity: 'error', type: 'api:message:reactions:fetch:failed' }, origin: { ...(message ? { context: { message } } : {}), emitter: 'Reactions' } }); } }); return function (_x) { return _ref.apply(this, arguments); }; }(), [addNotification, client, limit, message, messageId, reactionType, sort, t]); var loadNextPage = (0, _react.useCallback)((0, _asyncToGenerator2.default)(function* () { if (next) { yield fetchReactions(next); } }), [fetchReactions, next]); (0, _react.useEffect)(() => { setReactions([]); setNext(undefined); fetchReactions(undefined); }, [fetchReactions, messageId, reactionType, sortString]); (0, _react.useEffect)(() => { var listeners = []; listeners.push(client.on('offline_reactions.queried', event => { var offlineReactions = event.offlineReactions; if (offlineReactions) { setReactions(offlineReactions); setLoading(false); setNext(undefined); } })); listeners.push(client.on('reaction.new', event => { var reaction = event.reaction; if (reaction) { setReactions(prevReactions => upsertReactionInList({ prevReactions, reaction, reactionType })); } })); listeners.push(client.on('reaction.updated', event => { var reaction = event.reaction; if (reaction) { setReactions(prevReactions => reconcileUpdatedReactionInList({ prevReactions, reaction, reactionType })); } })); listeners.push(client.on('reaction.deleted', event => { var reaction = event.reaction; if (reaction) { setReactions(prevReactions => removeReactionFromList({ prevReactions, reaction, reactionType })); } })); return () => { listeners.forEach(listener => { listener == null || listener.unsubscribe(); }); }; }, [client, reactionType]); return { loading, loadNextPage, reactions }; }; exports.useFetchReactions = useFetchReactions; //# sourceMappingURL=useFetchReactions.js.map