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) { function displayResults(data) {
const resultsElement = document.getElementById("results"); const resultsElement = document.getElementById("results");
const downloadContainer = document.getElementById("download-container"); const downloadContainer = document.getElementById("download-container");
// Apply time conversion if enabled // Apply time conversion if enabled
const convertTime = document.getElementById('convertTimeOption').checked; const convertTime = document.getElementById('convertTimeOption').checked;
const replaceKeys = document.getElementById('replaceKeysOption').checked; const replaceKeys = document.getElementById('replaceKeysOption').checked;
let displayData = data; let displayData = data;
if (convertTime || replaceKeys) { if (convertTime || replaceKeys) {
const timezone = document.getElementById('timezoneSelect').value; 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 // Format the data
let formattedData = ''; let formattedData = '';
if (outputFormat === 'yaml') { if (outputFormat === 'yaml') {
@ -342,7 +343,7 @@ function displayResults(data) {
formattedData = JSON.stringify(displayData, null, 2); formattedData = JSON.stringify(displayData, null, 2);
document.getElementById("downloadJson").textContent = "Download JSON Data"; document.getElementById("downloadJson").textContent = "Download JSON Data";
} }
resultsElement.textContent = formattedData; resultsElement.textContent = formattedData;
resultsElement.style.display = "block"; resultsElement.style.display = "block";
downloadContainer.style.display = "block"; downloadContainer.style.display = "block";
@ -369,6 +370,7 @@ function displayError(message) {
} }
} }
/*
function addEnterKeyListeners() { function addEnterKeyListeners() {
document.getElementById("ssoToken").addEventListener("keypress", function(event) { document.getElementById("ssoToken").addEventListener("keypress", function(event) {
if (event.key === "Enter") { if (event.key === "Enter") {
@ -400,6 +402,38 @@ function addEnterKeyListeners() {
document.getElementById("fuzzySearch").click(); 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() { function triggerActiveTabButton() {