forked from ShashwatXD/xdverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (169 loc) · 5.71 KB
/
Copy pathscript.js
File metadata and controls
191 lines (169 loc) · 5.71 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
const membersPaths = [
"Shashwat.json",
"newstu.json",
];
const VALIDATION_MODE = "strict";
async function fetchJSON(path) {
try {
const url = new URL(`members/${path}`, document.baseURI).toString();
const res = await fetch(url, { cache: "no-store" });
if (!res.ok) throw new Error(`${res.status} ${res.statusText} @ ${url}`);
return await res.json();
} catch (e) {
console.warn("Fetch failed for", path, e.message);
return null;
}
}
function ghAvatar(handle) {
return `https://github.com/${handle}.png`;
}
function chip(text) {
return `<span class="chip">${text}</span>`;
}
function validateMember(m) {
if (!m) return false;
if (VALIDATION_MODE === "strict") {
return m.name && m.branch && m.githubID && m.url;
}
if (VALIDATION_MODE === "name+github") {
return m.name && m.githubID;
}
return !!m.name;
}
const drawer = document.getElementById("drawer");
const overlay = document.getElementById("overlay");
const drawerTitle = document.getElementById("drawerTitle");
const drawerContent = document.getElementById("drawerContent");
const closeDrawerBtn = document.getElementById("closeDrawer");
function openDrawer(title, html) {
drawerTitle.textContent = title;
drawerContent.innerHTML = html;
drawer.classList.remove("hidden");
overlay.classList.remove("hidden");
requestAnimationFrame(() => {
drawer.classList.add("open");
overlay.classList.add("open");
});
}
function closeDrawer() {
drawer.classList.remove("open");
overlay.classList.remove("open");
setTimeout(() => {
drawer.classList.add("hidden");
overlay.classList.add("hidden");
}, 180);
}
overlay.addEventListener("click", closeDrawer);
closeDrawerBtn.addEventListener("click", closeDrawer);
document.addEventListener("keydown", (e) => { if (e.key === "Escape") closeDrawer(); });
const listEl = document.getElementById("list");
function renderRow(m) {
const li = document.createElement("li");
li.className = "list-item";
li.tabIndex = 0;
const name = m.name || "Unnamed";
const branch = m.branch || "";
const github = m.githubID
? `<a href="https://github.com/${m.githubID}" target="_blank" class="github-link">@${m.githubID} </a>`
: "";
li.innerHTML = `
<div class="row">
<div class="member">
<img class="avatar" src="${m.avatar || ghAvatar(m.githubID || "github")}" alt="${name}" />
<div>
<div class="name">${name}</div>
</div>
</div>
<div>${branch}</div>
<div>${github}</div>
</div>
`;
li.addEventListener("click", () => openDetail(m, name, branch));
li.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
openDetail(m, name, branch);
}
});
return li;
}
function openDetail(m, name, branch) {
const links = m.links || {};
const html = `
<div class="detail-top">
<img class="detail-avatar" src="${m.avatar || ghAvatar(m.githubID || "github")}" alt="${name}" />
<div>
<h3 class="detail-title">${name}</h3>
${branch ? `<div class="detail-meta">${branch}</div>` : ""}
${m.githubID ? `<div class="detail-meta">@${m.githubID}</div>` : ""}
</div>
</div>
${m.about ? `<p>${m.about}</p>` : ""}
<div id="funfact" class="subline">
Preparing a courteous quip…
</div>
<div class="actions">
${m.githubID ? `<a class="link-btn primary" href="https://github.com/${m.githubID}" target="_blank">GitHub ↗</a>` : ""}
${links.linkedin ? `<a class="link-btn blue" href="${links.linkedin}" target="_blank">LinkedIn</a>` : ""}
${links.site ? `<a class="link-btn" href="${links.site}" target="_blank">Website</a>` : ""}
</div>
`;
openDrawer(name, html);
scheduleFunFact({ about: m.about || "", name });
}
let funFactTimer = null;
async function fetchFunFact({ about, name, endpoint = "/api/funfact", stylePrompt = "" }) {
const el = document.getElementById("funfact");
if (!el) return;
try {
const res = await fetch(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ about, name, stylePrompt })
});
const data = await res.json();
el.textContent = data.funFact || "The universe refused to jest at this moment.";
} catch {
el.textContent = "Could not assemble a polite punchline.";
}
}
function scheduleFunFact({ about, name, endpoint, stylePrompt }) {
if (funFactTimer) clearTimeout(funFactTimer);
funFactTimer = setTimeout(() => {
fetchFunFact({ about, name, endpoint, stylePrompt });
}, 2000);
}
function renderList(members) {
listEl.innerHTML = "";
if (!members.length) {
listEl.innerHTML = `<li class="list-item" style="text-align:center;">No valid member profiles found</li>`;
return;
}
members.forEach(m => listEl.appendChild(renderRow(m)));
}
async function loadMembersProgressively() {
const loaded = [];
listEl.innerHTML = "";
for (const path of membersPaths) {
const m = await fetchJSON(path);
if (validateMember(m)) {
loaded.push(m);
listEl.appendChild(renderRow(m));
await new Promise(r => setTimeout(r, 60));
}
}
if (!loaded.length) {
listEl.innerHTML = `<li class="list-item" style="text-align:center;">No valid member profiles found</li>`;
}
return loaded;
}
window.addEventListener("DOMContentLoaded", async () => {
listEl.innerHTML = `<li class="list-item" style="text-align:center;">Loading…</li>`;
const members = await loadMembersProgressively();
document.getElementById("randomBtn")?.addEventListener("click", () => {
if (!members.length) return;
const m = members[Math.floor(Math.random() * members.length)];
const idx = members.indexOf(m);
document.querySelectorAll("#list li")[idx]?.click();
});
});