about summary refs log tree commit diff
path: root/src/view/screens/Notifications.tsx
blob: 7e4de497e45559a3ceae05b24121ed1b0b25d204 (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
import React, {useState, useEffect} from 'react'
import {View} from 'react-native'
import {ViewHeader} from '../com/util/ViewHeader'
import {FAB} from '../com/util/FloatingActionButton'
import {Feed} from '../com/notifications/Feed'
import {useStores} from '../../state'
import {NotificationsViewModel} from '../../state/models/notifications-view'
import {ScreenParams} from '../routes'

export const Notifications = ({navIdx, visible}: ScreenParams) => {
  const [hasSetup, setHasSetup] = useState<boolean>(false)
  const [notesView, setNotesView] = useState<
    NotificationsViewModel | undefined
  >()
  const store = useStores()

  useEffect(() => {
    let aborted = false
    if (!visible) {
      return
    }
    store.me.refreshMemberships() // needed for the invite notifications
    if (hasSetup) {
      console.log('Updating notifications feed')
      notesView?.update()
    } else {
      store.nav.setTitle(navIdx, 'Notifications')
      const newNotesView = new NotificationsViewModel(store, {})
      setNotesView(newNotesView)
      newNotesView.setup().then(() => {
        if (aborted) return
        setHasSetup(true)
      })
    }
    return () => {
      aborted = true
    }
  }, [visible, store])

  const onComposePress = () => {
    store.shell.openComposer({})
  }
  const onPressTryAgain = () => {
    notesView?.refresh()
  }

  return (
    <View style={{flex: 1}}>
      <ViewHeader title="Notifications" />
      {notesView && <Feed view={notesView} onPressTryAgain={onPressTryAgain} />}
      <FAB icon="pen-nib" onPress={onComposePress} />
    </View>
  )
}