Skip to content

Commit a6ff886

Browse files
committed
feat: latest logs on top, oldest logs on bottom
Resolves: ubiquity/ubiquibot#676
1 parent 942de74 commit a6ff886

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

scripts/index.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const openJsonModal = (validJson: string) => {
2020
jsonModal.style.display = "flex";
2121
};
2222

23-
const updateLogTable = (scrollUp = false) => {
23+
const updateLogTable = () => {
2424
const selectedFilter = filterSelect.value;
2525
const filteredLogs = selectedFilter === "all" ? logs : logs.filter((log) => getLevelString(log.level) === selectedFilter);
2626

@@ -51,12 +51,6 @@ const updateLogTable = (scrollUp = false) => {
5151
}
5252
});
5353
}
54-
// scroll to last added data
55-
if (scrollUp) {
56-
logCell[0].scrollIntoView();
57-
} else {
58-
logCell[logCell.length - 1].scrollIntoView();
59-
}
6054
});
6155
};
6256

@@ -68,30 +62,32 @@ const fetchData = async () => {
6862
isLoading = true;
6963

7064
if (logs.length > 0) {
71-
const firstAvailableTimestamp = logs.at(0)?.timestamp;
65+
const firstAvailableTimestamp = logs.at(logs.length-1)?.timestamp;
7266
const { data, error } = await supabaseClient
7367
.from("logs")
7468
.select()
7569
.lt("timestamp", firstAvailableTimestamp)
7670
.order("timestamp", { ascending: false })
7771
.limit(25);
7872
if (data && data.length > 0) {
79-
logs.unshift(...data.reverse());
80-
updateLogTable(true);
73+
logs.push(...data);
74+
updateLogTable();
8175
} else console.log(error);
8276
} else {
8377
const { data, error } = await supabaseClient.from("logs").select().order("timestamp", { ascending: false }).limit(30);
8478
if (data && data.length > 0) {
85-
logs.push(...data.reverse());
86-
updateLogTable(true);
79+
logs.push(...data);
80+
updateLogTable();
8781
} else console.log(error);
8882
}
8983

9084
isLoading = false;
9185
};
9286

9387
const handleScroll = async () => {
94-
if (document.documentElement.scrollTop !== 0 || isLoading) {
88+
const bottom = document.documentElement.scrollHeight - document.documentElement.scrollTop === document.documentElement.clientHeight;
89+
90+
if (!bottom || isLoading) {
9591
return;
9692
}
9793
await fetchData();
@@ -114,7 +110,7 @@ supabaseClient
114110

115111
const handlePayload = (logEntry: any) => {
116112
if (logEntry?.eventType !== "INSERT") return;
117-
logs.push(logEntry.new);
113+
logs.unshift(logEntry.new);
118114
updateLogTable();
119115
};
120116

0 commit comments

Comments
 (0)