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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
import React, {useEffect} from 'react'
import {StyleSheet, TouchableOpacity, View} from 'react-native'
import {observer} from 'mobx-react-lite'
import useAppState from 'react-native-appstate-hook'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {useSafeAreaInsets} from 'react-native-safe-area-context'
import {ViewHeader} from '../com/util/ViewHeader'
import {Feed} from '../com/posts/Feed'
import {Text} from '../com/util/text/Text'
import {FAB} from '../com/util/FAB'
import {useStores} from '../../state'
import {ScreenParams} from '../routes'
import {s, colors} from '../lib/styles'
import {useOnMainScroll} from '../lib/hooks/useOnMainScroll'
import {clamp} from 'lodash'
const HITSLOP = {left: 20, top: 20, right: 20, bottom: 20}
export const Home = observer(function Home({
navIdx,
visible,
scrollElRef,
}: ScreenParams) {
const store = useStores()
const onMainScroll = useOnMainScroll(store)
const safeAreaInsets = useSafeAreaInsets()
const [wasVisible, setWasVisible] = React.useState<boolean>(false)
const {appState} = useAppState({
onForeground: () => doPoll(true),
})
const doPoll = React.useCallback(
(knownActive = false) => {
if ((!knownActive && appState !== 'active') || !visible) {
return
}
if (store.me.mainFeed.isLoading) {
return
}
store.log.debug('Polling home feed')
store.me.mainFeed.checkForLatest().catch(e => {
store.log.error('Failed to poll feed', e)
})
},
[appState, visible, store],
)
useEffect(() => {
const pollInterval = setInterval(() => doPoll(), 15e3)
if (!visible) {
setWasVisible(false)
return
} else if (wasVisible) {
return
}
setWasVisible(true)
store.nav.setTitle(navIdx, 'Home')
store.log.debug('Updating home feed')
if (store.me.mainFeed.hasContent) {
store.me.mainFeed.update()
} else {
store.me.mainFeed.setup()
}
return () => {
clearInterval(pollInterval)
}
}, [visible, store, navIdx, doPoll, wasVisible])
const onPressCompose = () => {
store.shell.openComposer({})
}
const onPressTryAgain = () => {
store.me.mainFeed.refresh()
}
const onPressLoadLatest = () => {
store.me.mainFeed.refresh()
scrollElRef?.current?.scrollToOffset({offset: 0})
}
return (
<View style={s.flex1}>
<ViewHeader title="Bluesky" subtitle="Private Beta" canGoBack={false} />
<Feed
testID="homeFeed"
key="default"
feed={store.me.mainFeed}
scrollElRef={scrollElRef}
style={{flex: 1}}
onPressCompose={onPressCompose}
onPressTryAgain={onPressTryAgain}
onScroll={onMainScroll}
/>
{store.me.mainFeed.hasNewLatest && !store.me.mainFeed.isRefreshing ? (
<TouchableOpacity
style={[
styles.loadLatest,
store.shell.minimalShellMode
? {bottom: 50}
: {bottom: 60 + clamp(safeAreaInsets.bottom, 15, 30)},
]}
onPress={onPressLoadLatest}
hitSlop={HITSLOP}>
<FontAwesomeIcon icon="arrow-up" style={{color: colors.white}} />
<Text style={styles.loadLatestText}>Load new posts</Text>
</TouchableOpacity>
) : undefined}
<FAB icon="pen-nib" onPress={onPressCompose} />
</View>
)
})
const styles = StyleSheet.create({
loadLatest: {
flexDirection: 'row',
position: 'absolute',
left: 10,
backgroundColor: colors.pink3,
paddingHorizontal: 12,
paddingVertical: 10,
borderRadius: 30,
shadowColor: '#000',
shadowOpacity: 0.3,
shadowOffset: {width: 0, height: 1},
},
loadLatestLow: {
bottom: 15,
},
loadLatestText: {
color: colors.white,
fontWeight: 'bold',
marginLeft: 5,
fontSize: 16,
},
})
|