about summary refs log tree commit diff
path: root/src/view/com/notifications/Feed.tsx
blob: 55e958c2b4fd2e06cd73a92e51997fb6593acd05 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React from 'react'
import {observer} from 'mobx-react-lite'
import {StyleSheet, View} from 'react-native'
import {CenteredView, FlatList} from '../util/Views'
import {NotificationsViewModel} from '../../../state/models/notifications-view'
import {FeedItem} from './FeedItem'
import {NotificationFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
import {ErrorMessage} from '../util/error/ErrorMessage'
import {EmptyState} from '../util/EmptyState'
import {OnScrollCb} from '../../lib/hooks/useOnMainScroll'
import {s} from '../../lib/styles'

const EMPTY_FEED_ITEM = {_reactKey: '__empty__'}

export const Feed = observer(function Feed({
  view,
  onPressTryAgain,
  onScroll,
}: {
  view: NotificationsViewModel
  onPressTryAgain?: () => void
  onScroll?: OnScrollCb
}) {
  // 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: any}) => {
    if (item === EMPTY_FEED_ITEM) {
      return (
        <EmptyState
          icon="bell"
          message="No notifications yet!"
          style={styles.emptyState}
        />
      )
    }
    return <FeedItem item={item} />
  }
  const onRefresh = () => {
    view
      .refresh()
      .catch(err =>
        view.rootStore.log.error('Failed to refresh notifications feed', err),
      )
  }
  const onEndReached = () => {
    view
      .loadMore()
      .catch(err =>
        view.rootStore.log.error('Failed to load more notifications', err),
      )
  }
  let data
  if (view.hasLoaded) {
    if (view.isEmpty) {
      data = [EMPTY_FEED_ITEM]
    } else {
      data = view.notifications
    }
  }
  return (
    <View style={s.h100pct}>
      <CenteredView>
        {view.isLoading && !data && <NotificationFeedLoadingPlaceholder />}
        {view.hasError && (
          <ErrorMessage
            message={view.error}
            onPressTryAgain={onPressTryAgain}
          />
        )}
      </CenteredView>
      {data && (
        <FlatList
          data={data}
          keyExtractor={item => item._reactKey}
          renderItem={renderItem}
          refreshing={view.isRefreshing}
          onRefresh={onRefresh}
          onEndReached={onEndReached}
          onScroll={onScroll}
          contentContainerStyle={s.contentContainer}
        />
      )}
    </View>
  )
})

const styles = StyleSheet.create({
  emptyState: {paddingVertical: 40},
})