|
| 1 | +/** |
| 2 | + * Created by leon<[email protected]> on 22/05/27. |
| 3 | + */ |
| 4 | +import { Placeholder, Spinner } from '@src/components' |
| 5 | +import { translate } from '@src/i18n' |
| 6 | +import { SylCommon, useTheme } from '@src/theme' |
| 7 | +import { ITheme, V2exObject } from '@src/types' |
| 8 | +import React from 'react' |
| 9 | +import { FlatList, StyleProp, View, ViewStyle } from 'react-native' |
| 10 | +import { BorderLine } from '../common' |
| 11 | +import SimpleProfileInfoCard from './SimpleProfileInfoCard' |
| 12 | + |
| 13 | +export interface ProfileCardListProps { |
| 14 | + /** |
| 15 | + * container style |
| 16 | + */ |
| 17 | + containerStyle?: StyleProp<ViewStyle> |
| 18 | + |
| 19 | + itemContainerStyle?: StyleProp<ViewStyle> |
| 20 | + |
| 21 | + canLoadMoreContent?: boolean |
| 22 | + members?: Array<V2exObject.Member> |
| 23 | + onEndReached?: () => void |
| 24 | + refreshControl?: React.ReactElement |
| 25 | + searchIndicator?: boolean |
| 26 | + refreshCallback?: () => void |
| 27 | + |
| 28 | + useFlatList?: boolean |
| 29 | +} |
| 30 | + |
| 31 | +const ProfileCardList: React.FC<ProfileCardListProps> = ({ |
| 32 | + useFlatList = true, |
| 33 | + containerStyle, |
| 34 | + itemContainerStyle, |
| 35 | + canLoadMoreContent, |
| 36 | + members, |
| 37 | + onEndReached, |
| 38 | + refreshControl, |
| 39 | + searchIndicator, |
| 40 | + refreshCallback |
| 41 | +}: ProfileCardListProps) => { |
| 42 | + const { theme } = useTheme() |
| 43 | + |
| 44 | + const renderItemRow = ({ item }: { item: V2exObject.Member }) => |
| 45 | + !item || item === null ? null : ( |
| 46 | + <SimpleProfileInfoCard |
| 47 | + key={item.id} |
| 48 | + containerStyle={[styles.infoItemContainer(theme), itemContainerStyle]} |
| 49 | + info={item} |
| 50 | + /> |
| 51 | + ) |
| 52 | + |
| 53 | + const renderFooter = () => { |
| 54 | + if (canLoadMoreContent) { |
| 55 | + return <Spinner style={{ padding: theme.spacing.large }} /> |
| 56 | + } else if (members && members.length > 0) { |
| 57 | + return ( |
| 58 | + <Placeholder |
| 59 | + containerStyle={[{ backgroundColor: theme.colors.background }]} |
| 60 | + placeholderText={translate('tips.noMore')} |
| 61 | + /> |
| 62 | + ) |
| 63 | + } |
| 64 | + return null |
| 65 | + } |
| 66 | + |
| 67 | + const renderItemSeparator = () => <BorderLine /> |
| 68 | + |
| 69 | + const renderContent = () => { |
| 70 | + if (!members) { |
| 71 | + return <Spinner style={{ marginTop: 50 }} /> |
| 72 | + } |
| 73 | + |
| 74 | + if (members.length > 0) { |
| 75 | + return useFlatList ? ( |
| 76 | + <FlatList |
| 77 | + refreshControl={refreshControl} |
| 78 | + style={styles.container(theme)} |
| 79 | + data={members} |
| 80 | + renderItem={renderItemRow} |
| 81 | + keyExtractor={(item, index) => index.toString()} |
| 82 | + onEndReached={onEndReached} |
| 83 | + onEndReachedThreshold={0.1} |
| 84 | + ListFooterComponent={renderFooter} |
| 85 | + numColumns={1} |
| 86 | + horizontal={false} |
| 87 | + key={'ONE COLUMN'} |
| 88 | + maxToRenderPerBatch={10} |
| 89 | + initialNumToRender={10} |
| 90 | + ItemSeparatorComponent={renderItemSeparator} |
| 91 | + /> |
| 92 | + ) : ( |
| 93 | + <View style={[styles.container(theme), containerStyle]}>{members.map((v) => renderItemRow({ item: v }))}</View> |
| 94 | + ) |
| 95 | + } |
| 96 | + if (!searchIndicator) { |
| 97 | + return <Placeholder buttonText={translate('button.tryAgain')} buttonPress={refreshCallback} /> |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return <View style={[styles.container(theme), containerStyle]}>{renderContent()}</View> |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * @description styles settings |
| 106 | + */ |
| 107 | +const styles = { |
| 108 | + container: (theme: ITheme): ViewStyle => ({ |
| 109 | + flex: 1 |
| 110 | + }), |
| 111 | + infoItemContainer: (theme: ITheme): ViewStyle => ({ |
| 112 | + ...SylCommon.Card.container(theme), |
| 113 | + paddingTop: theme.spacing.medium |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +export default ProfileCardList |
0 commit comments