@sendbird/uikit-react-native
Version:
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
145 lines (142 loc) • 5.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GroupChannelThreadContextsProvider = exports.GroupChannelThreadContexts = void 0;
var _react = _interopRequireWildcard(require("react"));
var _uikitUtils = require("@sendbird/uikit-utils");
var _ProviderLayout = _interopRequireDefault(require("../../../components/ProviderLayout"));
var _useContext = require("../../../hooks/useContext");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 (const 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); }
const GroupChannelThreadContexts = exports.GroupChannelThreadContexts = {
Fragment: /*#__PURE__*/(0, _react.createContext)({
headerTitle: '',
channel: {},
parentMessage: {},
setMessageToEdit: _uikitUtils.NOOP
}),
PubSub: /*#__PURE__*/(0, _react.createContext)({
publish: _uikitUtils.NOOP,
subscribe: () => _uikitUtils.NOOP
}),
MessageList: /*#__PURE__*/(0, _react.createContext)({
flatListRef: {
current: null
},
scrollToMessage: () => false,
lazyScrollToBottom: () => {
// noop
},
lazyScrollToIndex: () => {
// noop
}
})
};
const GroupChannelThreadContextsProvider = ({
children,
channel,
parentMessage,
keyboardAvoidOffset = 0,
groupChannelThreadPubSub,
threadedMessages
}) => {
if (!channel) throw new Error('GroupChannel is not provided to GroupChannelThreadModule');
const {
STRINGS
} = (0, _useContext.useLocalization)();
const [messageToEdit, setMessageToEdit] = (0, _react.useState)();
const {
flatListRef,
lazyScrollToIndex,
lazyScrollToBottom,
scrollToMessage
} = useScrollActions({
threadedMessages: threadedMessages
});
return /*#__PURE__*/_react.default.createElement(_ProviderLayout.default, null, /*#__PURE__*/_react.default.createElement(GroupChannelThreadContexts.Fragment.Provider, {
value: {
headerTitle: STRINGS.GROUP_CHANNEL_THREAD.HEADER_TITLE,
channel,
parentMessage,
keyboardAvoidOffset,
messageToEdit: messageToEdit,
setMessageToEdit
}
}, /*#__PURE__*/_react.default.createElement(GroupChannelThreadContexts.PubSub.Provider, {
value: groupChannelThreadPubSub
}, /*#__PURE__*/_react.default.createElement(GroupChannelThreadContexts.MessageList.Provider, {
value: {
flatListRef,
scrollToMessage,
lazyScrollToIndex,
lazyScrollToBottom
}
}, children))));
};
exports.GroupChannelThreadContextsProvider = GroupChannelThreadContextsProvider;
const useScrollActions = params => {
const {
threadedMessages
} = params;
const flatListRef = (0, _react.useRef)(null);
// FIXME: Workaround, should run after data has been applied to UI.
const lazyScrollToBottom = (0, _uikitUtils.useFreshCallback)(params => {
if (!flatListRef.current) {
logFlatListRefWarning();
return;
}
setTimeout(() => {
if (flatListRef.current) {
flatListRef.current.scrollToEnd({
animated: (params === null || params === void 0 ? void 0 : params.animated) ?? false
});
}
}, (params === null || params === void 0 ? void 0 : params.timeout) ?? 0);
});
// FIXME: Workaround, should run after data has been applied to UI.
const lazyScrollToIndex = (0, _uikitUtils.useFreshCallback)(params => {
if (!flatListRef.current) {
logFlatListRefWarning();
return;
}
setTimeout(() => {
if (flatListRef.current) {
flatListRef.current.scrollToIndex({
index: (params === null || params === void 0 ? void 0 : params.index) ?? 0,
animated: (params === null || params === void 0 ? void 0 : params.animated) ?? false,
viewPosition: (params === null || params === void 0 ? void 0 : params.viewPosition) ?? 0.5
});
}
}, (params === null || params === void 0 ? void 0 : params.timeout) ?? 0);
});
const scrollToMessage = (0, _uikitUtils.useFreshCallback)((messageId, options) => {
if (!flatListRef.current) {
logFlatListRefWarning();
return false;
}
const foundMessageIndex = threadedMessages.findIndex(it => it.messageId === messageId);
const isIncludedInList = foundMessageIndex > -1;
if (isIncludedInList) {
lazyScrollToIndex({
index: foundMessageIndex,
animated: true,
timeout: 0,
viewPosition: options === null || options === void 0 ? void 0 : options.viewPosition
});
return true;
} else {
return false;
}
});
return {
flatListRef,
lazyScrollToIndex,
lazyScrollToBottom,
scrollToMessage
};
};
const logFlatListRefWarning = () => {
_uikitUtils.Logger.warn('Cannot find flatListRef.current, please render FlatList and pass the flatListRef' + 'or please try again after FlatList has been rendered.');
};
//# sourceMappingURL=moduleContext.js.map