main
js 315 lines 6.02 KB
Raw
1 (function () {
2 'use strict';
3
4 const e = React.createElement;
5
6 function timeAge(time) {
7 const now = new Date().getTime() / 1000;
8 const minutes = (now - time) / 60;
9
10 if (minutes < 60) {
11 return Math.round(minutes) + ' minutes ago';
12 }
13 return Math.round(minutes / 60) + ' hours ago';
14 }
15
16 function getHostUrl(url) {
17 return (url + '')
18 .replace('https://', '')
19 .replace('http://', '')
20 .split('/')[0];
21 }
22
23 function HeaderBar() {
24 return e(
25 'tr',
26 {
27 style: {
28 backgroundColor: '#222',
29 },
30 },
31 e(
32 'table',
33 {
34 style: {
35 padding: 4,
36 },
37 width: '100%',
38 cellSpacing: 0,
39 cellPadding: 0,
40 },
41 e(
42 'tbody',
43 null,
44 e(
45 'tr',
46 null,
47 e(
48 'td',
49 {
50 style: {
51 width: 18,
52 paddingRight: 4,
53 },
54 },
55 e(
56 'a',
57 {
58 href: '#',
59 },
60 e('img', {
61 src: 'logo.png',
62 width: 16,
63 height: 16,
64 style: {
65 border: '1px solid #00d8ff',
66 },
67 })
68 )
69 ),
70 e(
71 'td',
72 {
73 style: {
74 lineHeight: '12pt',
75 },
76 height: 10,
77 },
78 e(
79 'span',
80 {
81 className: 'pagetop',
82 },
83 e('b', {className: 'hnname'}, 'React HN Benchmark'),
84 e('a', {href: '#'}, 'new'),
85 ' | ',
86 e('a', {href: '#'}, 'comments'),
87 ' | ',
88 e('a', {href: '#'}, 'show'),
89 ' | ',
90 e('a', {href: '#'}, 'ask'),
91 ' | ',
92 e('a', {href: '#'}, 'jobs'),
93 ' | ',
94 e('a', {href: '#'}, 'submit')
95 )
96 )
97 )
98 )
99 )
100 );
101 }
102
103 function Story({story, rank}) {
104 return [
105 e(
106 'tr',
107 {
108 className: 'athing',
109 },
110 e(
111 'td',
112 {
113 style: {
114 verticalAlign: 'top',
115 textAlign: 'right',
116 },
117 className: 'title',
118 },
119 e(
120 'span',
121 {
122 className: 'rank',
123 },
124 `${rank}.`
125 )
126 ),
127 e(
128 'td',
129 {
130 className: 'votelinks',
131 style: {
132 verticalAlign: 'top',
133 },
134 },
135 e(
136 'center',
137 null,
138 e(
139 'a',
140 {
141 href: '#',
142 },
143 e('div', {
144 className: 'votearrow',
145 title: 'upvote',
146 })
147 )
148 )
149 ),
150 e(
151 'td',
152 {
153 className: 'title',
154 },
155 e(
156 'a',
157 {
158 href: '#',
159 className: 'storylink',
160 },
161 story.title
162 ),
163 story.url
164 ? e(
165 'span',
166 {
167 className: 'sitebit comhead',
168 },
169 ' (',
170 e(
171 'a',
172 {
173 href: '#',
174 },
175 getHostUrl(story.url)
176 ),
177 ')'
178 )
179 : null
180 )
181 ),
182 e(
183 'tr',
184 null,
185 e('td', {
186 colSpan: 2,
187 }),
188 e(
189 'td',
190 {
191 className: 'subtext',
192 },
193 e(
194 'span',
195 {
196 className: 'score',
197 },
198 `${story.score} points`
199 ),
200 ' by ',
201 e(
202 'a',
203 {
204 href: '#',
205 className: 'hnuser',
206 },
207 story.by
208 ),
209 ' ',
210 e(
211 'span',
212 {
213 className: 'age',
214 },
215 e(
216 'a',
217 {
218 href: '#',
219 },
220 timeAge(story.time)
221 )
222 ),
223 ' | ',
224 e(
225 'a',
226 {
227 href: '#',
228 },
229 'hide'
230 ),
231 ' | ',
232 e(
233 'a',
234 {
235 href: '#',
236 },
237 `${story.descendants || 0} comments`
238 )
239 )
240 ),
241 e('tr', {
242 style: {
243 height: 5,
244 },
245 className: 'spacer',
246 }),
247 ];
248 }
249
250 function StoryList({stories}) {
251 return e(
252 'tr',
253 null,
254 e(
255 'td',
256 null,
257 e(
258 'table',
259 {
260 cellPadding: 0,
261 cellSpacing: 0,
262 classList: 'itemlist',
263 },
264 e(
265 'tbody',
266 null,
267 stories.map((story, i) =>
268 e(Story, {story, rank: ++i, key: story.id})
269 )
270 )
271 )
272 )
273 );
274 }
275
276 function App({stories}) {
277 return e(
278 'center',
279 null,
280 e(
281 'table',
282 {
283 id: 'hnmain',
284 border: 0,
285 cellPadding: 0,
286 cellSpacing: 0,
287 width: '85%',
288 style: {
289 'background-color': '#f6f6ef',
290 },
291 },
292 e(
293 'tbody',
294 null,
295 e(HeaderBar, null),
296 e('tr', {height: 10}),
297 e(StoryList, {
298 stories,
299 })
300 )
301 )
302 );
303 }
304
305 const app = document.getElementById('app');
306
307 window.render = function render() {
308 ReactDOM.render(
309 React.createElement(App, {
310 stories: window.stories,
311 }),
312 app
313 );
314 };
315 })();