@gathertown/uikit-react-native
Version:
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
113 lines • 4.62 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { CustomComponentContext } from '@gathertown/uikit-react-native-foundation';
import { NOOP } from '@gathertown/uikit-utils';
import React, { useCallback, useContext, useReducer, useRef, useState } from 'react';
import { ReactionBottomSheets } from '../components/ReactionBottomSheets';
import { LocalizationContext } from '../contexts/LocalizationCtx';
import { SendbirdChatContext } from '../contexts/SendbirdChatCtx';
import { UserProfileContext } from '../contexts/UserProfileCtx';
import { useReaction } from '../hooks/useContext';
export const ReactionContext = /*#__PURE__*/React.createContext(null);
export const ReactionProvider = _ref => {
let {
children
} = _ref;
const [state, setState] = useReducer((prev, next) => ({
...prev,
...next
}), {});
const [reactionUserListFocusIndex, setReactionUserListFocusIndex] = useState(0);
const [reactionListVisible, setReactionListVisible] = useState(false);
const [reactionUserListVisible, setReactionUserListVisible] = useState(false);
const closeResolver = useRef(NOOP);
const createOnCloseWithResolver = callback => {
return () => {
return new Promise(resolve => {
closeResolver.current = resolve;
callback();
});
};
};
const openReactionList = useCallback(params => {
setState(params);
setReactionListVisible(true);
}, []);
const openReactionUserList = useCallback(_ref2 => {
let {
channel,
message,
focusIndex = 0
} = _ref2;
setState({
channel,
message
});
setReactionUserListFocusIndex(focusIndex);
setReactionUserListVisible(true);
}, []);
const updateReactionFocusedItem = useCallback(params => {
if (params) setState(params);else setState({});
}, []);
const onDismiss = useCallback(() => {
var _closeResolver$curren;
setState({});
(_closeResolver$curren = closeResolver.current) === null || _closeResolver$curren === void 0 ? void 0 : _closeResolver$curren.call(closeResolver);
}, [setState]);
const onCloseReactionUserList = createOnCloseWithResolver(() => setReactionUserListVisible(false));
const onCloseReactionList = createOnCloseWithResolver(() => setReactionListVisible(false));
const reactionCtx = {
...state,
openReactionList,
openReactionUserList,
updateReactionFocusedItem,
focusIndex: reactionUserListFocusIndex,
onDismiss,
reactionUserListVisible,
reactionListVisible,
onCloseReactionUserList,
onCloseReactionList
};
return /*#__PURE__*/React.createElement(ReactionContext.Provider, {
value: reactionCtx
}, children);
};
export const ReactionBottomSheetsWrapper = () => {
const chatCtx = useContext(SendbirdChatContext);
const localizationCtx = useContext(LocalizationContext);
const userProfileCtx = useContext(UserProfileContext);
const ctx = useContext(CustomComponentContext);
if (!chatCtx) throw new Error('SendbirdChatContext is not provided');
if (!localizationCtx) throw new Error('LocalizationContext is not provided');
if (!userProfileCtx) throw new Error('UserProfileContext is not provided');
const reactionCtx = useReaction();
const {
onDismiss,
reactionUserListVisible,
reactionListVisible,
focusIndex,
onCloseReactionUserList,
onCloseReactionList
} = reactionCtx;
const sheetProps = {
chatCtx,
reactionCtx,
localizationCtx,
userProfileCtx,
onDismiss
};
return /*#__PURE__*/React.createElement(React.Fragment, null, ctx !== null && ctx !== void 0 && ctx.renderReactionBottomSheetUserListRenderProp ? ctx.renderReactionBottomSheetUserListRenderProp({
message: sheetProps.reactionCtx.message,
onDismiss: sheetProps.onDismiss,
onClose: onCloseReactionUserList,
visible: reactionUserListVisible,
getEmoji: key => chatCtx.emojiManager.allEmojiMap[key],
initialFocusIndex: focusIndex
}) : /*#__PURE__*/React.createElement(ReactionBottomSheets.UserList, _extends({}, sheetProps, {
visible: reactionUserListVisible,
onClose: onCloseReactionUserList
})), /*#__PURE__*/React.createElement(ReactionBottomSheets.ReactionList, _extends({}, sheetProps, {
visible: reactionListVisible,
onClose: onCloseReactionList
})));
};
//# sourceMappingURL=ReactionCtx.js.map