From 45a8d673626e2be9485d0dc3942d6a076acdbc4d Mon Sep 17 00:00:00 2001 From: Deco Vander Date: Fri, 4 Jul 2025 13:14:27 -0400 Subject: [PATCH] Refactor: Extract UTC parsing logic to parseUTCDate helper function - Add parseUTCDate helper function to handle timestamp UTC parsing consistently - Eliminates code duplication across getTimeAgo, getTimeRemaining, and expiry functions - Applied to admin.js, app-mapbox.js, app-google.js, and app.js - Ensures consistent UTC timezone handling throughout all frontend JavaScript - Addresses Copilot AI feedback for better code maintainability and DRY principles The parseUTCDate function handles the logic: timestamp.includes('T') ? timestamp : timestamp + 'Z' This ensures all timestamp parsing uses the same UTC interpretation logic. --- public/admin.js | 7 ++++++- public/app-google.js | 8 +++++++- public/app-mapbox.js | 11 ++++++++--- public/app.js | 8 +++++++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/public/admin.js b/public/admin.js index 378c82f..5a20dce 100644 --- a/public/admin.js +++ b/public/admin.js @@ -495,7 +495,7 @@ document.addEventListener('DOMContentLoaded', () => { function getTimeAgo(timestamp) { const now = new Date(); // Ensure timestamp is treated as UTC if it doesn't have timezone info - const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + const reportTime = parseUTCDate(timestamp); const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60)); if (diffInMinutes < 1) return 'just now'; @@ -510,6 +510,11 @@ document.addEventListener('DOMContentLoaded', () => { return reportTime.toLocaleDateString(); } + // Helper function to parse UTC date + function parseUTCDate(timestamp) { + return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + } + // Profanity management functions async function loadProfanityWords() { if (!profanityTableBody) return; diff --git a/public/app-google.js b/public/app-google.js index 89012ef..1db2981 100644 --- a/public/app-google.js +++ b/public/app-google.js @@ -82,9 +82,15 @@ document.addEventListener('DOMContentLoaded', async () => { }); }; + // Helper function to parse UTC date + const parseUTCDate = (timestamp) => { + return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + }; + const getTimeAgo = (timestamp) => { const now = new Date(); - const reportTime = new Date(timestamp); + // Ensure timestamp is treated as UTC if it doesn't have timezone info + const reportTime = parseUTCDate(timestamp); const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60)); if (diffInMinutes < 1) return 'just now'; diff --git a/public/app-mapbox.js b/public/app-mapbox.js index 6723788..dcff11f 100644 --- a/public/app-mapbox.js +++ b/public/app-mapbox.js @@ -93,10 +93,15 @@ document.addEventListener('DOMContentLoaded', async () => { }); }; + // Helper function to parse UTC date + const parseUTCDate = (timestamp) => { + return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + }; + const getTimeAgo = (timestamp) => { const now = new Date(); // Ensure timestamp is treated as UTC if it doesn't have timezone info - const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + const reportTime = parseUTCDate(timestamp); const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60)); if (diffInMinutes < 1) return 'just now'; @@ -174,7 +179,7 @@ document.addEventListener('DOMContentLoaded', async () => { const now = new Date(); // Ensure timestamp is treated as UTC if it doesn't have timezone info - const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + const reportTime = parseUTCDate(timestamp); const expirationTime = new Date(reportTime.getTime() + 48 * 60 * 60 * 1000); const remaining = expirationTime - now; @@ -198,7 +203,7 @@ document.addEventListener('DOMContentLoaded', async () => { const now = new Date(); // Ensure timestamp is treated as UTC if it doesn't have timezone info - const reportTime = new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + const reportTime = parseUTCDate(timestamp); const expirationTime = new Date(reportTime.getTime() + 48 * 60 * 60 * 1000); const remaining = expirationTime - now; const hoursRemaining = remaining / (1000 * 60 * 60); diff --git a/public/app.js b/public/app.js index 421bfdb..84d0933 100644 --- a/public/app.js +++ b/public/app.js @@ -74,9 +74,15 @@ document.addEventListener('DOMContentLoaded', () => { }); }; + // Helper function to parse UTC date + const parseUTCDate = (timestamp) => { + return new Date(timestamp.includes('T') ? timestamp : timestamp + 'Z'); + }; + const getTimeAgo = (timestamp) => { const now = new Date(); - const reportTime = new Date(timestamp); + // Ensure timestamp is treated as UTC if it doesn't have timezone info + const reportTime = parseUTCDate(timestamp); const diffInMinutes = Math.floor((now - reportTime) / (1000 * 60)); if (diffInMinutes < 1) return 'just now';