-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
281 lines (240 loc) · 7.68 KB
/
app.js
File metadata and controls
281 lines (240 loc) · 7.68 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 1) Firebase config
const firebaseConfig = {
apiKey: "AIzaSyBdcqSSWmjGnn3fsi8eDQTXtFDFUiYbtfU",
authDomain: "devtrackr-149be.firebaseapp.com",
projectId: "devtrackr-149be",
storageBucket: "devtrackr-149be.firebasestorage.app",
messagingSenderId: "936962359530",
appId: "1:936962359530:web:0a27728821a2a02ea91793",
measurementId: "G-VX3H0BDW39",
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();
// 2) Auth elements
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");
const btnSignup = document.getElementById("btn-signup");
const btnLogin = document.getElementById("btn-login");
const authSection = document.getElementById("auth-section");
const appSection = document.getElementById("app-section");
// Sign up and login
btnSignup.onclick = () => {
auth
.createUserWithEmailAndPassword(emailInput.value, passwordInput.value)
.catch(alert);
};
btnLogin.onclick = () => {
auth
.signInWithEmailAndPassword(emailInput.value, passwordInput.value)
.catch(alert);
};
// 3) Show app after login
auth.onAuthStateChanged((user) => {
if (user) {
authSection.style.display = "none";
appSection.style.display = "block";
loadLogs();
loadGithubUsername();
} else {
authSection.style.display = "block";
appSection.style.display = "none";
}
});
// 4) Daily logs
const btnAddLog = document.getElementById("btn-add-log");
const logsList = document.getElementById("logs-list");
const streakText = document.getElementById("streak-text");
btnAddLog.onclick = async () => {
const user = auth.currentUser;
if (!user) return alert("Login first");
const title = document.getElementById("log-title").value;
const desc = document.getElementById("log-desc").value;
const tag = document.getElementById("log-tag").value;
await db.collection("logs").add({
userId: user.uid,
title,
desc,
tag,
date: new Date().toISOString().slice(0, 10), // YYYY-MM-DD
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
});
document.getElementById("log-title").value = "";
document.getElementById("log-desc").value = "";
loadLogs();
};
// 5) GitHub section
const githubUsernameInput = document.getElementById("github-username");
const btnSaveGithub = document.getElementById("btn-save-github");
const btnLoadGithub = document.getElementById("btn-load-github");
const githubReposList = document.getElementById("github-repos");
// save username to Firestore
btnSaveGithub.onclick = async () => {
const user = auth.currentUser;
if (!user) return;
await db
.collection("users")
.doc(user.uid)
.set(
{
githubUsername: githubUsernameInput.value,
},
{ merge: true }
);
alert("GitHub username saved");
};
async function loadGithubUsername() {
const user = auth.currentUser;
if (!user) return;
const docSnap = await db.collection("users").doc(user.uid).get();
if (docSnap.exists && docSnap.data().githubUsername) {
githubUsernameInput.value = docSnap.data().githubUsername;
}
}
btnLoadGithub.onclick = async () => {
const username = githubUsernameInput.value.trim();
if (!username) return alert("Enter username");
const res = await fetch(`https://api.github.com/users/${username}/repos`);
const repos = await res.json();
githubReposList.innerHTML = "";
repos.slice(0, 5).forEach((r) => {
const last = r.pushed_at
? new Date(r.pushed_at).toDateString()
: "N/A";
const li = document.createElement("li");
li.textContent = `${r.name} - Last commit: ${last}`;
githubReposList.appendChild(li);
});
};
// 6) Weekly summary (local)
const btnGenerateSummary = document.getElementById("btn-generate-summary");
const summaryText = document.getElementById("summary-text");
btnGenerateSummary.onclick = async () => {
const user = auth.currentUser;
if (!user) return alert("Login first");
summaryText.textContent = "Generating...";
const snap = await db
.collection("logs")
.where("userId", "==", user.uid)
.orderBy("date", "desc")
.limit(7)
.get();
const items = [];
const tags = {};
snap.forEach((doc) => {
const d = doc.data();
items.push(`• ${d.date}: [${d.tag}] ${d.title}`);
tags[d.tag] = (tags[d.tag] || 0) + 1;
});
if (items.length === 0) {
summaryText.textContent =
"No logs this week. Add some activity to see a summary.";
return;
}
const mainTag = Object.entries(tags).sort((a, b) => b[1] - a[1])[0][0];
summaryText.textContent =
`This week you focused mainly on ${mainTag}. ` +
`You logged ${items.length} coding sessions. ` +
`Highlights:\n` +
items.join("\n") +
`\n\nNext steps: continue this streak, document important learnings, and pick one mini-project to consolidate your skills.`;
};
// 7) Logs + streak + chart
let logsChartInstance = null;
async function loadLogs() {
const user = auth.currentUser;
if (!user) return;
const snap = await db
.collection("logs")
.where("userId", "==", user.uid)
.orderBy("date", "desc")
.limit(30)
.get();
const logs = [];
logsList.innerHTML = "";
snap.forEach((doc) => {
const data = doc.data();
logs.push(data);
const li = document.createElement("li");
li.textContent = `${data.date} - [${data.tag}] ${data.title}`;
logsList.appendChild(li);
});
updateStreak(logs);
drawLogsChart(logs);
}
// compute consecutive streak ending today
function updateStreak(logs) {
const datesSet = new Set(logs.map((l) => l.date));
let streak = 0;
let current = new Date();
while (true) {
const dStr = current.toISOString().slice(0, 10);
if (datesSet.has(dStr)) {
streak++;
current.setDate(current.getDate() - 1);
} else {
break;
}
}
streakText.textContent = `Current streak: ${streak} day(s)`;
}
// Chart: count logs per day
function drawLogsChart(logs) {
const byDate = {};
logs.forEach((l) => {
byDate[l.date] = (byDate[l.date] || 0) + 1;
});
const labels = Object.keys(byDate).sort();
const values = labels.map((d) => byDate[d]);
const ctx = document.getElementById("logsChart").getContext("2d");
if (logsChartInstance) logsChartInstance.destroy();
logsChartInstance = new Chart(ctx, {
type: "bar",
data: {
labels,
datasets: [
{
label: "Logs per day",
data: values,
backgroundColor: "rgba(37, 99, 235, 0.6)",
},
],
},
});
}
// 8) Simple JavaScript playground
const codeInput = document.getElementById("code-input");
const codeStdin = document.getElementById("code-stdin");
const btnRunCode = document.getElementById("btn-run-code");
const codeOutput = document.getElementById("code-output");
btnRunCode.onclick = () => {
const input = codeStdin.value; // user input variable
const code = codeInput.value;
codeOutput.textContent = "";
try {
const result = eval(code);
if (result !== undefined) {
codeOutput.textContent = String(result);
} else if (!codeOutput.textContent) {
codeOutput.textContent = "Code executed (no return value).";
}
} catch (e) {
codeOutput.textContent = "Error: " + e.message;
}
};
// 9) Tabs behavior
const tabButtons = document.querySelectorAll(".tab-button");
const tabContents = {
overview: document.getElementById("tab-overview"),
github: document.getElementById("tab-github"),
playground: document.getElementById("tab-playground"),
};
tabButtons.forEach((btn) => {
btn.onclick = () => {
const tab = btn.dataset.tab;
tabButtons.forEach((b) => b.classList.remove("active"));
Object.values(tabContents).forEach((c) => c.classList.remove("active"));
btn.classList.add("active");
tabContents[tab].classList.add("active");
};
});