chore(backend.js): use deep cloning over JSON.parse(JSON.stringify()), use event delegation to reduce redundant EventListeners

This commit is contained in:
Rim 2025-03-30 23:59:13 -04:00
parent 83030bf030
commit e6ce48723f

View File

@ -322,17 +322,18 @@ async function fetchData(endpoint, requestData) {
function displayResults(data) {
const resultsElement = document.getElementById("results");
const downloadContainer = document.getElementById("download-container");
// Apply time conversion if enabled
const convertTime = document.getElementById('convertTimeOption').checked;
const replaceKeys = document.getElementById('replaceKeysOption').checked;
let displayData = data;
if (convertTime || replaceKeys) {
const timezone = document.getElementById('timezoneSelect').value;
displayData = processTimestamps(JSON.parse(JSON.stringify(data)), timezone);
displayData = processTimestamps(structuredClone(data), timezone); // Use structured clone API instead of JSON.parse/stringify
// displayData = processTimestamps(JSON.parse(JSON.stringify(data)), timezone);
}
// Format the data
let formattedData = '';
if (outputFormat === 'yaml') {
@ -342,7 +343,7 @@ function displayResults(data) {
formattedData = JSON.stringify(displayData, null, 2);
document.getElementById("downloadJson").textContent = "Download JSON Data";
}
resultsElement.textContent = formattedData;
resultsElement.style.display = "block";
downloadContainer.style.display = "block";
@ -369,6 +370,7 @@ function displayError(message) {
}
}
/*
function addEnterKeyListeners() {
document.getElementById("ssoToken").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
@ -400,6 +402,38 @@ function addEnterKeyListeners() {
document.getElementById("fuzzySearch").click();
}
});
} */
function addEnterKeyListeners() {
// Use event delegation for handling Enter key press
document.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
// Get the active element
const activeElement = document.activeElement;
if (!activeElement || !activeElement.id) return;
// Mapping of input fields to their submit buttons
const inputToButtonMapping = {
"ssoToken": null, // Will trigger active tab button
"username": null, // Will trigger active tab button
"matchUsername": "fetchMatches",
"matchId": "fetchMatchInfo",
"userUsername": "fetchUserInfo",
"searchUsername": "fuzzySearch"
};
if (activeElement.id in inputToButtonMapping) {
if (inputToButtonMapping[activeElement.id]) {
// Click the specific button
document.getElementById(inputToButtonMapping[activeElement.id]).click();
} else {
// Trigger the active tab button
triggerActiveTabButton();
}
}
}
});
}
function triggerActiveTabButton() {