-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (67 loc) · 2.09 KB
/
index.js
File metadata and controls
70 lines (67 loc) · 2.09 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
function init(type) {
const list = document.getElementById("list");
const GIT_URL = `https://github.com/Visnalize/brick-1100-${type}/tree/main`;
const GAME_URL = `https://brick1100-${type}.visnalize.com`;
const PREVIEW_URL = "https://brick1100.visnalize.com/#/online/previewer";
const PHONE_FRAME = "https://visnalize.com/assets/phone-v.CTqaLA5O.webp";
const CACHE_KEY = `brick1100-${type}`;
function render(data) {
const items = data
.filter((item) => item.type === "dir")
.map((item) => {
const name = item.name
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
const iframe = m("iframe", {
src: `${PREVIEW_URL}?url=${GAME_URL}/${item.name}`,
frameborder: "0",
});
return m("div.mb-6", [
m("div.frame.mx-auto", [iframe, m(`img[src='${PHONE_FRAME}']`)]),
m(
"a.button.is-text.is-block",
{
href: `${GIT_URL}/${item.name}`,
target: "_blank",
},
m("h2.is-size-4.is-size-3-desktop", name)
),
]);
});
m.render(list, m("div.grid.is-col-min-10.is-gap-4", items));
}
function fetchData() {
fetch(`https://api.github.com/repos/Visnalize/brick-1100-${type}/contents`)
.then((response) => {
if (response.ok && response.status === 200) {
return response.json();
}
m.render(
list,
m("div.cell.has-text-centered", [
m("h2.is-size-3", "Failed to fetch data"),
m("p", "Please try again later"),
])
);
})
.then((data) => {
localStorage.setItem(
CACHE_KEY,
JSON.stringify({ data, timestamp: Date.now() })
);
render(data);
});
}
if (localStorage.getItem(CACHE_KEY)) {
const { data, timestamp } = JSON.parse(localStorage.getItem(CACHE_KEY));
const oneDay = 86400000;
if (Date.now() - timestamp < oneDay) {
render(data);
} else {
fetchData();
}
} else {
fetchData();
}
}