2023-01-13 19:32:52 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { ActivityIndicator, StyleSheet, Text } from 'react-native';
|
2023-01-13 19:32:52 +00:00
|
|
|
import { useDispatch } from 'react-redux';
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2023-01-13 19:32:52 +00:00
|
|
|
import { MessageTypeLoad } from '../../../lib/constants';
|
|
|
|
import { MessageType, RoomType } from '../../../definitions';
|
2021-05-26 17:24:54 +00:00
|
|
|
import { useTheme } from '../../../theme';
|
2022-08-17 13:32:21 +00:00
|
|
|
import Touch from '../../../containers/Touch';
|
2021-05-26 17:24:54 +00:00
|
|
|
import sharedStyles from '../../Styles';
|
|
|
|
import I18n from '../../../i18n';
|
2023-01-13 19:32:52 +00:00
|
|
|
import { roomHistoryRequest } from '../../../actions/room';
|
|
|
|
import { useAppSelector } from '../../../lib/hooks';
|
2021-05-26 17:24:54 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
paddingVertical: 16,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-13 19:32:52 +00:00
|
|
|
const LoadMore = React.memo(
|
|
|
|
({
|
|
|
|
rid,
|
|
|
|
t,
|
|
|
|
loaderId,
|
|
|
|
type,
|
|
|
|
runOnRender
|
|
|
|
}: {
|
|
|
|
rid: string;
|
|
|
|
t: RoomType;
|
|
|
|
loaderId: string;
|
|
|
|
type: MessageType;
|
|
|
|
runOnRender: boolean;
|
|
|
|
}): React.ReactElement => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const loading = useAppSelector(state => state.room.historyLoaders.some(historyLoader => historyLoader === loaderId));
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2023-01-13 19:32:52 +00:00
|
|
|
const handleLoad = () => dispatch(roomHistoryRequest({ rid, t, loaderId }));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (runOnRender) {
|
|
|
|
handleLoad();
|
2021-05-26 17:24:54 +00:00
|
|
|
}
|
2023-01-13 19:32:52 +00:00
|
|
|
}, []);
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2023-01-13 19:32:52 +00:00
|
|
|
let text = 'Load_More';
|
|
|
|
if (type === MessageTypeLoad.NEXT_CHUNK) {
|
|
|
|
text = 'Load_Newer';
|
|
|
|
}
|
|
|
|
if (type === MessageTypeLoad.PREVIOUS_CHUNK) {
|
|
|
|
text = 'Load_Older';
|
2021-05-26 17:24:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 19:32:52 +00:00
|
|
|
return (
|
|
|
|
<Touch onPress={handleLoad} style={styles.button} enabled={!loading}>
|
|
|
|
{loading ? (
|
2024-04-18 10:19:54 +00:00
|
|
|
<ActivityIndicator color={colors.fontSecondaryInfo} />
|
2023-01-13 19:32:52 +00:00
|
|
|
) : (
|
2024-04-18 10:19:54 +00:00
|
|
|
<Text style={[styles.text, { color: colors.fontTitlesLabels }]}>{I18n.t(text)}</Text>
|
2023-01-13 19:32:52 +00:00
|
|
|
)}
|
|
|
|
</Touch>
|
|
|
|
);
|
2021-05-26 17:24:54 +00:00
|
|
|
}
|
2023-01-13 19:32:52 +00:00
|
|
|
);
|
2021-05-26 17:24:54 +00:00
|
|
|
|
|
|
|
export default LoadMore;
|