forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
99 lines (84 loc) · 3.14 KB
/
Copy pathindex.ts
File metadata and controls
99 lines (84 loc) · 3.14 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
import { containsValidJson, createGitHubCommentURL, generateRandomId, getLevelString } from "./helpers/utils";
import { Logs } from "./types/log";
import { createClient } from "@supabase/supabase-js";
import { SUPABASE_URL, SUPABASE_KEY } from "./constants/index";
const filterSelect = document.getElementById("filter") as unknown as HTMLSelectElement;
const clearButton = document.getElementById("clear") as HTMLButtonElement;
const logBody = document.getElementById("log-body") as HTMLDivElement;
const jsonModal = document.getElementById("json-modal") as HTMLDivElement;
const closeModalButton = document.getElementById("close-modal") as HTMLButtonElement;
const jsonContent = document.getElementById("json-content") as HTMLDivElement;
const openJsonModal = (validJson) => {
jsonContent.textContent = validJson;
jsonModal.style.display = "flex";
};
const updateLogTable = () => {
const selectedFilter = filterSelect.value;
const filteredLogs = selectedFilter === "all" ? logs : logs.filter((log) => getLevelString(log.level) === selectedFilter);
logBody.innerHTML = "";
filteredLogs.forEach((log) => {
const classId = generateRandomId(10);
const commentUrl = createGitHubCommentURL(log.org_name, log.repo_name, log.issue_number, log.comment_id);
const row = document.createElement("tr");
const [validJson, match, beforeText] = containsValidJson(log.log_message);
row.innerHTML = `
${
validJson
? `<td class="log-cell">${beforeText} - <button id="button_${classId}">Show JSON</button></td>`
: `<td class="log-cell">${log.log_message}</td>`
}
<td>${getLevelString(log.level)}</td>
<td>${log.timestamp}</td>
<td><a href="${commentUrl}">Comment - ${log.comment_id}</a></td>
`;
logBody.appendChild(row);
let logCell = document.getElementsByClassName("log-cell");
if (validJson) {
// show modal button for valid json row
const showMoreButton = document.getElementById(`button_${classId}`) as HTMLButtonElement;
showMoreButton.addEventListener("click", () => {
if (validJson) {
openJsonModal(JSON.stringify(JSON.parse(match), null, 2)); // properly formatted json
}
});
}
// scroll to last added data
logCell[logCell.length - 1].scrollIntoView();
});
};
let logs: Logs[] = [];
const supabaseClient = createClient(SUPABASE_URL, SUPABASE_KEY);
const channel = supabaseClient
.channel("table-db-changes")
.on(
"postgres_changes",
{
event: "INSERT",
schema: "public",
table: "logs",
},
(payload) => handlePayload(payload)
)
.subscribe();
const handlePayload = (logEntry) => {
if (logEntry?.eventType !== "INSERT") return;
logs.push(logEntry.new);
updateLogTable();
};
filterSelect.addEventListener("change", () => {
updateLogTable();
});
clearButton.addEventListener("click", () => {
logs = [];
updateLogTable();
});
closeModalButton.addEventListener("click", () => {
jsonModal.style.display = "none";
});
window.addEventListener("click", (event) => {
if (event.target === jsonModal) {
jsonModal.style.display = "none";
}
});
// Initial update
updateLogTable();