-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
141 lines (116 loc) · 3.86 KB
/
utils.js
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
// generate random uuid
// https://stackoverflow.com/a/2117523
export function uuidv4() {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c) =>
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
);
}
// generate random hex color
// https://stackoverflow.com/a/5092872
export function getRandomHexColor() {
return "#000000".replace(/0/g, function () {
return (~~(Math.random() * 16)).toString(16);
});
}
// generate random name
export function generateRandomUsername() {
const adjectives = [
"happy",
"clever",
"colorful",
"brave",
"gentle",
"playful",
"silly",
"sleepy",
"funny",
"curious",
"friendly",
];
const nouns = [
"beaver",
"dragon",
"star",
"wizard",
"robot",
"snail",
"bard",
"dog",
"cat",
"fox",
"penguin",
"dinosaur",
"machine",
];
const randomAdjective = adjectives[Math.floor(Math.random() * adjectives.length)];
const randomNoun = nouns[Math.floor(Math.random() * nouns.length)];
return `${randomAdjective} ${randomNoun}`;
}
// return given timestamp in a friendly relative time text format
export function friendlyRelativeTime(timestamp) {
const now = new Date();
const seconds = (now.getTime() - timestamp) / 1000;
if (seconds < 60) {
return `${Math.round(seconds)} seconds ago`;
} else if (seconds < 60 * 60) {
return `${Math.round(seconds / 60)} minutes ago`;
} else if (seconds < 60 * 60 * 24) {
return `${Math.round(seconds / 60 / 60)} hours ago`;
} else {
return `${Math.round(seconds / 60 / 60 / 24)} days ago`;
}
}
const templateUserCard = document.getElementById("template-user-card");
const templateQuestionCard = document.getElementById("template-question-card");
const userListElement = document.getElementById("user-list");
const questionListElement = document.getElementById("question-grid");
export function addUserToList(name, color) {
const newUserCard = templateUserCard.cloneNode(true);
newUserCard.removeAttribute("id");
newUserCard.querySelector(".username-field").innerText = name;
newUserCard.style.setProperty("--user-color", color);
userListElement.appendChild(newUserCard);
}
export function addQuestionToList(id, text, timestamp, userName) {
const newQuestionCard = templateQuestionCard.cloneNode(true);
newQuestionCard.setAttribute("id", id);
newQuestionCard.querySelector(".question-text").innerText = text;
const newTimestampElement = newQuestionCard.querySelector(".question-timestamp");
newTimestampElement.setAttribute("data-timestamp", timestamp);
newTimestampElement.innerText = friendlyRelativeTime(timestamp);
if (userName) {
newQuestionCard.querySelector(".question-name").innerText = userName + " asks:";
} else {
newQuestionCard.querySelector(".question-name").innerText = "\t";
}
questionListElement.appendChild(newQuestionCard);
}
export function setUserNameOnQuestion(id, userName) {
const questionCard = document.getElementById(id);
if (!questionCard) {
return;
}
questionCard.querySelector(".question-name").innerText = userName + " asks:";
}
export function clearUserList() {
userListElement.innerHTML = "";
}
export function clearQuestionList() {
questionListElement.innerHTML = "";
}
// debug layout stuff
// for (let i = 0; i < 20; i++) {
// addUserToList(getRandomUsername(), getRandomHexColor());
// }
// for (let i = 0; i < 20; i++) {
// addQuestionToList("This is a sample question", Date.now() - Math.random() * 10000000000);
// }
// update all timestamps every second
setInterval(() => {
const timestamps = document.querySelectorAll(".question-timestamp");
timestamps.forEach((timestamp) => {
const timestampValue = timestamp.getAttribute("data-timestamp");
const friendlyTimestamp = friendlyRelativeTime(timestampValue);
timestamp.innerText = friendlyTimestamp;
});
}, 1000);