-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview.js
More file actions
258 lines (240 loc) · 7.15 KB
/
view.js
File metadata and controls
258 lines (240 loc) · 7.15 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const MINUTE = 60;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const ago = (created) => {
const d = new Date();
const today = new Date(d.getFullYear(), d.getMonth(), d.getDate());
const elapsedDays = (today - new Date(created * 1e3)) / (86400 * 1e3);
if (elapsedDays < 0) return 'today';
if (elapsedDays < 1) return 'yesterday';
return Math.ceil(elapsedDays) + ' days ago';
};
const count = (comments = []) => comments.reduce(
(total, value) => total + count((value.model || {}).comments),
comments.length
);
const plural = (num, unit) => {
num = num >>> 0;
if (num !== 1) unit += 's';
return `${num} ${unit}`;
};
const selected = (current, story) => current === story ? 'selected' : '';
const scrollTop = () => {
scrollTo({top: 0, left: 0, behavior: 'smooth'});
};
let cleanup = 0;
const content = new Map;
const cache = chunk => {
if (!cleanup)
cleanup = setTimeout(() => { cleanup = 0; content.clear(); }, 10000);
const template = [chunk];
content.set(chunk, template);
return template;
};
const timeBetween = (
a = Date.now(),
b = (Date.now() / 1e3)
) => {
const elapsed = b - a;
if (elapsed < HOUR)
return plural(elapsed / MINUTE, 'minute');
else if (elapsed < DAY)
return plural(elapsed / HOUR, 'hour');
return plural(elapsed / DAY, 'day');
};
// const {head, main, footer} = view(uhtml || ucontent);
export default ({html}) => {
// internal helpers
const comment = ({model = {comments: []}}) => html`
<li
class=${model.id ? '' : 'placeholder'}
.hidden=${!!model.deleted}
>
<small>
<a
onclick=${scrollTop}
href=${`../user/?${model.by}`}
>${model.by || '...'}</a>
${timeBetween(model.time)} ago
<a href="#collapse" data-count=${count(model.comments)} />
</small>
<div>
${html(content.get(model.text || '...') || cache(model.text || '...'))}
</div>
<ul class="comments" .hidden=${!model.comments.length}>
${model.comments.map(comment)}
</ul>
</li>
`;
const goBack = () => html`
<div class="paginator">
<a href="#back"><</a>
<span>go back / share</span>
<a href="#share">📤</a>
</div>
`;
const paginator = (current, page, total) => html`
<div class="paginator">
<a
onclick=${scrollTop}
href=${`../${current}/?${page - 1}`}
class=${page === 1 ? 'hidden' : ''}
>
<
</a>
<span>${page}/${total}</span>
<a
onclick=${scrollTop}
href=${`../${current}/?${page + 1}`}
class=${page === total ? 'hidden' : ''}
>
>
</a>
</div>
`;
return {
// the about section contains only static content
// it is OK in such case to import it on demand, instead of having it
// bundled within the rest of the logic
about: () => import('./about.js').then(({default: about}) => about(html)),
// all other sections are neither too big nor static,
// so these are grouped in here for simplicity
header: ({header: {current, stories}}) => html`
<header>
<nav>
<div class='icon'>📰</div>
<ul>
${stories.map(story => html`
<li>
<a
onclick=${scrollTop}
class=${selected(current, story)} href=${`../${story}/?1`}
>
${story === 'job' ? 'jobs' : story}
</a>
</li>`
)}
<li class='about'>
<a
onclick=${scrollTop}
class=${selected(current, 'about')} href="../about/"
>
about
</a>
</li>
</ul>
</nav>
</header>
`,
footer: () => html`
<footer>
Powered by
<a href="https://github.com/WebReflection/uhtml#readme">µhtml</a>
&
<a href="https://github.com/HackerNews/API">Hacker News API</a>
</footer>
`,
// show all details per story
main: (current, stories, page, total) => html`
<main class="stories">
${paginator(current, page, total)}
${stories.map(({index, model = {}}) => html`
<article class=${model.id ? '' : 'placeholder'}>
<div>${index}</div>
<div>
<h2>
<a
onclick=${model.url ? Object : scrollTop}
href='${model.url || `../item/?${model.id}`}'
rel="noopener"
>${model.title || '...'}
<small>
${(model.hostname || '').replace(/^www\./, '')}
</small>
</a>
</h2>
<p>
${model.score} points by
<a
onclick=${scrollTop}
href='${`../user/?${model.by}`}'
>${model.by}</a>
${timeBetween(model.time)} ago |
<a
class="nowrap"
onclick=${scrollTop} href=${`../item/?${model.id}`}
>${model.descendants || 0} comments</a>
</p>
</div>
</article>
`)}
${paginator(current, page, total)}
</main>
`,
// show all details per item
details: model => html`
<main class="details">
${goBack()}
<article>
<h2>
<a href=${model.url} rel="noopener">
${model.title}
<small>${(model.hostname || '').replace(/^www\./, '')}</small>
</a>
</h2>
<p class="meta">
${model.score} points by
<a
onclick=${scrollTop}
href=${`../user/?${model.by}`}
>${model.by}</a>
${timeBetween(model.time)} ago
</p>
</article>
<h3>
${model.descendants || 0} comments
</h3>
<ul .hidden=${!model.comments.length}>
${model.comments.map(comment)}
</ul>
${goBack()}
</main>
`,
// show all details per user
profile: ({about, created, id, karma}) => html`
<main class="profile">
${goBack()}
<article>
<h1>${id}</h1>
<p>
... joined <strong>${ago(created || 0)}</strong>,
and has <strong>${karma}</strong> karma
</p>
<p>
<a href=${`https://news.ycombinator.com/submitted?id=${id}`}
rel="noopener">submissions</a> /
<a href=${`https://news.ycombinator.com/threads?id=${id}`}
rel="noopener">comments</a> /
<a href=${`https://news.ycombinator.com/favorites?id=${id}`}
rel="noopener">favourites</a>
</p>
<div class='about' .hidden=${!about}>
${html([about])}
</div>
</article>
${goBack()}
</main>
`,
// final fallback to show when something goes wrong
// such as non-existent user/item
notFound: () => html`
<main class="not-found">
${goBack()}
<article>
<h1>Not Found</h1>
<p>The page you are looking for is not here.</p>
</article>
</main>
`
};
};