-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.mjs
145 lines (127 loc) · 4.95 KB
/
global.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
import { THONLY, AUTH, FRONTEND_COURSE, CAMPUS, TRILOGY, HOME, GAME, DEVICE } from "https://thonly.org/global.mjs";
export { THONLY, AUTH, FRONTEND_COURSE as ORIGIN, CAMPUS, TRILOGY, HOME, GAME, DEVICE };
// current
export const YEAR_BEGAN = 2022;
export const YEAR = 2023;
export const TERM = "semester-summer";
export const PASSING = 0.70;
export const BACKGROUND = (() => {
switch(TRILOGY[0]) {
case "frontend":
return "linear-gradient(90deg, rgba(5,117,230,1) 0%, rgba(2,27,121,1) 100%)";
case "backend":
return "linear-gradient(to left, #2E5339, #495F41)";
case "ios":
return "linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8))";
}
})();
export async function getGitHub() {
const github = JSON.parse(localStorage.getItem('github')) || {};
if (github.login) {
const students = await getData('students');
github.student = students[github.login];
}
return github;
}
export function getEmoji(cohort) {
switch (cohort.type) {
case "student":
switch (cohort.status) {
case "current":
return "✏️ ";
case "pass":
return "🎓 ";
case "fail":
return "🆘 ";
}
case "tutor":
return "🧑🏻🏫 ";
}
}
export function getYear(github) {
return Number(localStorage.getItem('year')) || (github.student ? github.student.cohorts[0].year : YEAR);
}
export function getTerm(github) {
const term = localStorage.getItem('term') || (github.student ? `${github.student.cohorts[0].system}-${github.student.cohorts[0].season}` : TERM);
return [term, ...term.split('-')];
}
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
export function getWeeks(system, y, m, d, w) {
const date = new Date(y, m, d);
const start = new Date(date);
if (system === 'quarter') start.setDate(date.getDate() + 7*(w-1))
else start.setDate(date.getDate() + 7*(w-1)*2);
const end = new Date(start);
if (system === 'quarter') end.setDate(start.getDate() + 6)
else end.setDate(start.getDate() + 7*2 - 1);
return `${months[start.getMonth()]} ${start.getDate()} - ${months[end.getMonth()]} ${end.getDate()}`;
}
export function getUnit(units, c) {
for (let u = 0; u < units.length; u++) {
if (units[u].from <= c && c <= units[u].to) return u + 1;
}
}
export function getWeek(weeks, c) {
for (let w = 0; w < weeks.length; w++) {
if (weeks[w].from <= c && c <= weeks[w].to) return w + 1;
}
}
export async function getData(filename, y=null, options={}) {
let url, system, season, c, w;
switch (filename) {
case "students":
url = `https://raw.githubusercontent.com/SiliconWat/${TRILOGY[0]}.siliconwat.dev/main/Students.json`;
break;
case "syllabus":
url = `https://raw.githubusercontent.com/SiliconWat/${TRILOGY[0]}.siliconwat.dev/main/${y}/Syllabus.json`;
break;
case "groups":
({ system, season, w } = options);
url = `https://raw.githubusercontent.com/SiliconWat/${TRILOGY[0]}.siliconwat.dev/main/${y}/${system === 'semester' ? "Semesters" : "Quarters"}/${season.capitalize()}/Weeks/${w}/Groups.json`;
break;
case "gradebook":
({ system, season, c } = options);
url = `https://raw.githubusercontent.com/SiliconWat/${TRILOGY[0]}.siliconwat.dev/main/${y}/${system === 'semester' ? "Semesters" : "Quarters"}/${season.capitalize()}/Chapters/${c}/Gradebook.json`;
break;
}
let cache = localStorage.getItem(url);
if (cache) {
return JSON.parse(cache);
} else {
try {
cache = await (await fetch(url, { cache: "no-store" })).json();
} catch(error) {
cache = await getBackup(filename, y);
}
localStorage.setItem(url, JSON.stringify(cache))
return cache;
}
}
async function getBackup(filename, y) {
let backup;
switch (filename) {
case "students": // deprecated
backup = y === YEAR + 1 ? {} : `/docs/students.mjs`;
break;
case "syllabus":
backup = (await fetch(`https://raw.githubusercontent.com/SiliconWat/${TRILOGY[0]}.siliconwat.dev/main/${YEAR_BEGAN}/Syllabus.json`, { cache: "no-store" })).json(); // `${TRILOGY[2]}/docs/syllabus.mjs`;
break;
case "groups":
backup = []; // "/docs/groups.mjs";
break;
case "gradebook":
backup = {}; // "/docs/gradebook.mjs";
break;
}
return typeof backup === 'string' ? (await import(backup)).default : backup;
}
export async function getFile(url) {
try {
await fetch(url);
return url;
} catch(error) {
return `https://github.com/SiliconWat/${TRILOGY[0]}.siliconwat.dev/blob/main/404.md`;
}
}
// admin only
import "/admin.mjs";