Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (130 loc) · 4.29 KB
/
Copy pathscript.js
File metadata and controls
165 lines (130 loc) · 4.29 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
const API_URL = "https://api.rotur.dev/v2/posts/df862c5ad9d4ba17970086d6c1039c2d";
const FALLBACK_AVATAR = "https://avatars.rotur.dev/undefined";
const INTERVAL = 5000;
const viewport = document.getElementById("viewport");
const counter = document.getElementById("counter");
const prevButton = document.getElementById("prev");
const nextButton = document.getElementById("next");
let replies = [];
let currentIndex = 0;
let timer = null;
let transitionTimeout = null;
function formatTimestamp(timestamp) {
const date = new Date(timestamp);
if (Number.isNaN(date.getTime())) {
return "Unknown time";
}
return new Intl.DateTimeFormat(undefined, {
dateStyle: "medium",
timeStyle: "short"
}).format(date);
}
function createComment(reply) {
const article = document.createElement("article");
article.className = "comment";
const user = document.createElement("div");
user.className = "user";
const avatar = document.createElement("img");
avatar.className = "avatar";
avatar.src = reply.author?.avatar_url || reply.avatar_url || `https://avatars.rotur.dev/${reply.user}`;
avatar.alt = "";
avatar.referrerPolicy = "no-referrer";
avatar.onerror = () => {
avatar.onerror = null;
avatar.src = FALLBACK_AVATAR;
};
const details = document.createElement("div");
const username = document.createElement("div");
username.className = "username";
username.textContent =
reply.author?.display_name ||
reply.author?.username ||
reply.user ||
"Unknown user";
const timestamp = document.createElement("div");
timestamp.className = "timestamp";
timestamp.textContent = formatTimestamp(reply.timestamp);
details.append(username, timestamp);
user.append(avatar, details);
const content = document.createElement("div");
content.className = "content";
content.textContent = reply.content || "";
article.append(user, content);
return article;
}
function updateControls() {
counter.textContent = replies.length
? `${currentIndex + 1} / ${replies.length}`
: "0 / 0";
prevButton.disabled = replies.length < 2;
nextButton.disabled = replies.length < 2;
}
function showReply(index, animate = true) {
if (!replies.length) {
return;
}
currentIndex = (index + replies.length) % replies.length;
clearTimeout(transitionTimeout);
const existing = viewport.querySelector(".comment");
if (existing && animate) {
existing.classList.add("exit");
transitionTimeout = setTimeout(() => {
renderCurrentReply();
}, 180);
} else {
renderCurrentReply();
}
updateControls();
}
function renderCurrentReply() {
const comment = createComment(replies[currentIndex]);
viewport.replaceChildren(comment);
}
function startSlideshow() {
clearInterval(timer);
if (replies.length > 1) {
timer = setInterval(() => {
showReply(currentIndex + 1);
}, INTERVAL);
}
}
prevButton.addEventListener("click", () => {
showReply(currentIndex - 1);
startSlideshow();
});
nextButton.addEventListener("click", () => {
showReply(currentIndex + 1);
startSlideshow();
});
async function loadReplies() {
try {
const response = await fetch(API_URL);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const post = await response.json();
replies = Array.isArray(post.replies) ? post.replies : [];
if (!replies.length) {
viewport.innerHTML = "";
const status = document.createElement("div");
status.className = "status";
status.textContent = "No replies yet.";
viewport.append(status);
updateControls();
return;
}
showReply(0, false);
startSlideshow();
} catch (error) {
const status = document.createElement("div");
status.className = "status";
status.textContent =
"Unable to load replies. Check the API or CORS settings.";
viewport.replaceChildren(status);
console.error("Failed to fetch replies:", error);
}
}
loadReplies();
document.querySelectorAll('a').forEach(a => {
a.setAttribute('target', '_blank');
});