about summary refs log tree commit diff
path: root/src/view/com/posts/Feed.tsx
blob: dc341ddd5ee1cff7f4c93e8d0097d0bab9cddc17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React, {MutableRefObject} from 'react'
import {observer} from 'mobx-react-lite'
import {Text, View, FlatList, StyleProp, ViewStyle} from 'react-native'
import {FeedModel, FeedItemModel} from '../../../state/models/feed-view'
import {FeedItem} from './FeedItem'

export const Feed = observer(function Feed({
  feed,
  style,
  scrollElRef,
}: {
  feed: FeedModel
  style?: StyleProp<ViewStyle>
  scrollElRef?: MutableRefObject<FlatList<any> | null>
}) {
  // TODO optimize renderItem or FeedItem, we're getting this notice from RN: -prf
  //   VirtualizedList: You have a large list that is slow to update - make sure your
  //   renderItem function renders components that follow React performance best practices
  //   like PureComponent, shouldComponentUpdate, etc
  const renderItem = ({item}: {item: FeedItemModel}) => <FeedItem item={item} />
  const onRefresh = () => {
    feed.refresh().catch(err => console.error('Failed to refresh', err))
  }
  const onEndReached = () => {
    feed.loadMore().catch(err => console.error('Failed to load more', err))
  }
  return (
    <View style={style}>
      {feed.isLoading && !feed.isRefreshing && !feed.hasContent && (
        <Text>Loading...</Text>
      )}
      {feed.hasError && <Text>{feed.error}</Text>}
      {feed.hasContent && (
        <FlatList
          ref={scrollElRef}
          data={feed.feed.slice()}
          keyExtractor={item => item._reactKey}
          renderItem={renderItem}
          refreshing={feed.isRefreshing}
          onRefresh={onRefresh}
          onEndReached={onEndReached}
        />
      )}
      {feed.isEmpty && <Text>This feed is empty!</Text>}
    </View>
  )
})