/
App.js
163 lines (148 loc) · 6.83 KB
/
App.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React from 'react'
import {
Text, View,
TouchableHighlight, ScrollView,
StatusBar, ActivityIndicator, Dimensions,
Platform, Image
} from 'react-native'
// assets
import { color } from 'react-native-material-design-styles'
import logo from './y18.gif'
import styles from './AppStyles'
// utils
import { domainUrl } from './utils'
import moment from 'moment'
// ComponentsmarginTop:
const Card = ({ children }) => <View style={styles.card}>{children}</View>
const cursorStyle = Platform.OS === 'web' ? { cursor: 'pointer' } : {}
const Overlay = ({ children, visible }) => (
(visible) ? (
<View style={{ position: 'absolute', marginTop: 48, zIndex: 1 }}>
<Text style={{ fontSize: 28, marginLeft: Dimensions.get('window').width * 0.9, backgroundColor: 'rgba(52,52,52, 0.0)', color: 'white', zIndex: 3 }}>⏏</Text>
<View style={{ padding: 10, marginTop: -17, width: Dimensions.get('window').width, height: 400, backgroundColor: 'rgba(255,255,255,80)' }}>
{children}
</View>
</View>
) : <View />
)
export default class App extends React.Component {
constructor(props) {
super(props)
this.scrollToTop = this.scrollToTop.bind(this)
}
scrollToTop() {
this.refs._scrollView.scrollTo({ x: 0, y: 0, animated: true })
}
render() {
const { items, errors, loading, filter, overlayVisible, onLoadMore, onLoadItems, onOpenUrl, onToggleOverlay } = this.props
return (
<Card>
{Platform.OS === 'android' ? (
<StatusBar backgroundColor={'#d25500'} />
) : <View />}
<View style={styles.body}>
<Overlay visible={overlayVisible}>
<Text style={{ fontSize: 18 }}>
<Text style={{ fontWeight: 'bold' }}>HAgnostic News</Text> is a simple Hacker News reader for the Web and a React Native app (Android / iOS).
</Text>
<Text style={{ fontSize: 18, marginTop: 20 }}>
Made with ❤ by
<Text
onPress={() => { onOpenUrl('https://grigio.org') }}>
<Text style={[styles.aboutLink, cursorStyle]}>{" "}Luigi Maselli</Text>
</Text>
, source code:
<Text
onPress={() => { onOpenUrl('https://github.com/grigio/HAgnostic-News') }}>
<Text style={[styles.aboutLink, cursorStyle]}>{" "}github.com/grigio/HAgnostic-News</Text>
</Text>
</Text>
</Overlay>
<View style={[styles.column, styles.header,
Platform.OS === 'ios' ? { height: 75, paddingTop: 20 } : {}]}>
<View style={[styles.row, { height: 50 }]}>
<View style={styles.row}>
<Image source={logo} style={{ width: 20 }} />
<Text style={[{ fontWeight: 'bold', paddingLeft: 4 }]}>HAgnostic News</Text>
<Text style={[{ fontSize: 12, paddingLeft: 4 }]}> {Platform.OS}</Text>
</View>
<TouchableHighlight
style={[styles.button, filter === 'Top' ? styles.buttonOrange : null]}
underlayColor={color.paperOrange200.color}
onPress={() => { onLoadItems('Top'); this.scrollToTop() }}>
<View style={styles.row}>
<Text style={{ color: 'white', fontWeight: 'bold', paddingRight: 5 }}>Top</Text>{filter === 'Top' && loading ? <ActivityIndicator /> : null}
</View>
</TouchableHighlight>
<TouchableHighlight
style={[styles.button, filter === 'Latest' ? styles.buttonOrange : null]}
underlayColor={color.paperOrange200.color}
onPress={() => { onLoadItems('Latest'); this.scrollToTop() }}>
<View style={styles.row}>
<Text style={{ color: 'white', fontWeight: 'bold', paddingRight: 5 }}>Latest</Text>{filter === 'Latest' && loading ? <ActivityIndicator /> : null}
</View>
</TouchableHighlight>
<TouchableHighlight
underlayColor={color.paperOrange200.color}
onPress={() => { onToggleOverlay() }}>
<Text style={{ color: 'white', fontWeight: 'bold', padding: 10 }}>?</Text>
</TouchableHighlight>
</View>
{Object.keys(errors).length > 0 ? (
<View style={[styles.row, { flex: 1, backgroundColor: 'red' }]}>
{Object.keys(errors).map((error, i) => (
<Text key={i}>. {errors[error].message}</Text>
)
)}
</View>
) : null}
</View>
<View style={styles.scrollViewContainer}>
<ScrollView
ref='_scrollView'
className='scrollView'
contentContainerStyle={styles.scrollViewContentContainerStyle}
scrollEventThrottle={1} // 1 event per second
style={styles.scrollViewStyle}
>
{items.map((item, i) => (
<View key={i}>
<View style={styles.itemRow}>
<Text style={{ flex: 1 }}>
<Text
style={[{ fontWeight: 'bold', fontSize: 18 }, cursorStyle]}
onPress={() => onOpenUrl(item.url || `https://news.ycombinator.com/item?id=${item.objectID}`)}>{i + 1}. {item.title}</Text>
<Text style={[{ flex: 1, color: '#979797' }, cursorStyle]}> {item.url && domainUrl(item.url)}</Text>
</Text>
</View>
<View style={styles.itemSubRow}>
<Text style={{ padding: 2 }}>{item.points} points </Text>
<Text style={{ padding: 2 }}> by {item.author}</Text>
<Text style={{ padding: 2 }}>| {item.num_comments} c. |</Text>
<Text
onPress={() => onOpenUrl(`https://news.ycombinator.com/item?id=${item.objectID}`)}
style={[{ padding: 2, flex: 1, textDecorationLine: 'underline' }, cursorStyle]}> {moment(item.created_at).fromNow()}</Text>
</View>
</View>
))}
<View style={[styles.itemRow, styles.buttonRow]}>
<TouchableHighlight
style={[styles.button, styles.buttonGray]}
underlayColor={color.paperOrange200.color}
onPress={() => onLoadMore()}>
<View style={[styles.row, { height: 20 }]}>
<Text
style={{ fontWeight: 'bold', fontSize: 16, color: color.paperGrey500.color }}>
{loading ? null : 'Load more'}
</Text>
{loading ? <ActivityIndicator /> : null}
</View>
</TouchableHighlight>
</View>
</ScrollView>
</View>
</View>
</Card>
)
}
}