-
Notifications
You must be signed in to change notification settings - Fork 43
/
templates.mjs
152 lines (130 loc) · 3.74 KB
/
templates.mjs
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
/**
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
import {escape, unescape} from 'html-escaper';
import {html} from '@popeindustries/lit-html-server';
import {unsafeHTML} from
'@popeindustries/lit-html-server/directives/unsafe-html';
import {DEFAULT_TAG, SORT_ORDERS} from './constants.mjs';
import {getQuestion} from './urls.mjs';
function formatDate(timestamp) {
return new Date(timestamp * 1000).toLocaleString();
}
function profile({imageUrl, date, profileLink, displayName, anchorLink}) {
return html`
<div class="profile">
<img src="${imageUrl}"
alt="Profile picture"
?crossorigin=${imageUrl && imageUrl.startsWith('https://www.gravatar.com/')}
<a href="${profileLink}">${displayName}</a>
at
<a href="${anchorLink}">${date}</a>
</div>
`;
}
function questionCard({id, title}) {
return html`
<a class="card"
href="/questions/${id}"
data-cache-url="${getQuestion(id)}">
${title}
</a>
`;
}
export function index(tag, items, sort) {
if (!items) {
return html`<p class="error">Unable to list questions for the tag.</p>`;
}
const titleString = (sort === SORT_ORDERS.VOTES ? 'Top' : 'Active') +
` "${tag}" Questions`;
const title = html`
<h3>${titleString}</h3>
`;
const form = html`
<form method="GET">
<label for="tag">Switch to tag:</label>
<input type="text"
name="tag"
placeholder="${DEFAULT_TAG}"></input>
</form>
`;
const questionCards = items.map((item) => questionCard({
id: item.question_id,
title: unescape(item.title),
}));
const questions = html`<div id="questions">${questionCards}</div>`;
const metadataScript = html`
<script>
self._title = ${JSON.stringify(escape(titleString))};
</script>
`;
return html`
${title}
${form}
${questions}
${metadataScript}
`;
}
export function question(item) {
if (!item) {
return html`<p class="error">Unable to load question.</p>`;
}
const ownerProfile = profile({
anchorLink: item.link,
date: formatDate(item.creation_date),
displayName: item.owner.display_name,
imageUrl: item.owner.profile_image,
profileLink: item.owner.link,
});
const title = unescape(item.title);
const question = html`
<h3>${title}</h3>
${ownerProfile}
<div>${unsafeHTML(item.body)}</div>
`;
const answers = item.answers ? item.answers
.sort((a, b) => a.score < b.score)
.map((answer) => {
const answererProfile = profile({
anchorLink: answer.link,
date: formatDate(answer.creation_date),
displayName: answer.owner.display_name,
imageUrl: answer.owner.profile_image,
profileLink: answer.owner.link,
});
return html`
${answererProfile}
<div>${unsafeHTML(answer.body)}</div>
`;
}) : [];
const metadataScript = html`
<script>
self._title = ${JSON.stringify(escape(title))};
</script>
`;
return html`
${question}
<hr>
${answers}
${metadataScript}
`;
}
export function error(message) {
return html`
<p>Sorry, this page couldn't be loaded.</p>
<p>Try a cached page instead.</p>
<pre>${message}</pre>
`;
}