@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.
136 lines (134 loc) • 4.26 kB
JavaScript
import React, { createContext, useRef, useState } from 'react';
import { Logger, NOOP, useFreshCallback } from '@sendbird/uikit-utils';
import ProviderLayout from '../../../components/ProviderLayout';
import { useLocalization } from '../../../hooks/useContext';
export const GroupChannelThreadContexts = {
Fragment: /*#__PURE__*/createContext({
headerTitle: '',
channel: {},
parentMessage: {},
setMessageToEdit: NOOP
}),
PubSub: /*#__PURE__*/createContext({
publish: NOOP,
subscribe: () => NOOP
}),
MessageList: /*#__PURE__*/createContext({
flatListRef: {
current: null
},
scrollToMessage: () => false,
lazyScrollToBottom: () => {
// noop
},
lazyScrollToIndex: () => {
// noop
}
})
};
export const GroupChannelThreadContextsProvider = ({
children,
channel,
parentMessage,
keyboardAvoidOffset = 0,
groupChannelThreadPubSub,
threadedMessages
}) => {
if (!channel) throw new Error('GroupChannel is not provided to GroupChannelThreadModule');
const {
STRINGS
} = useLocalization();
const [messageToEdit, setMessageToEdit] = useState();
const {
flatListRef,
lazyScrollToIndex,
lazyScrollToBottom,
scrollToMessage
} = useScrollActions({
threadedMessages: threadedMessages
});
return /*#__PURE__*/React.createElement(ProviderLayout, null, /*#__PURE__*/React.createElement(GroupChannelThreadContexts.Fragment.Provider, {
value: {
headerTitle: STRINGS.GROUP_CHANNEL_THREAD.HEADER_TITLE,
channel,
parentMessage,
keyboardAvoidOffset,
messageToEdit: messageToEdit,
setMessageToEdit
}
}, /*#__PURE__*/React.createElement(GroupChannelThreadContexts.PubSub.Provider, {
value: groupChannelThreadPubSub
}, /*#__PURE__*/React.createElement(GroupChannelThreadContexts.MessageList.Provider, {
value: {
flatListRef,
scrollToMessage,
lazyScrollToIndex,
lazyScrollToBottom
}
}, children))));
};
const useScrollActions = params => {
const {
threadedMessages
} = params;
const flatListRef = useRef(null);
// FIXME: Workaround, should run after data has been applied to UI.
const lazyScrollToBottom = 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 = 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 = 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 = () => {
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