Create shared utility module to eliminate function duplication
- Create public/utils.js with shared frontend utility functions - Extract parseUTCDate, getTimeAgo, getTimeRemaining, getRemainingClass to utils.js - Remove duplicate functions from admin.js, app-mapbox.js, app-google.js, and app.js - Add utils.js script import to index.html and admin.html - Add comprehensive JSDoc documentation for all utility functions - Ensure consistent UTC timestamp parsing across all frontend scripts This addresses Copilot AI feedback about function duplication across multiple frontend scripts. Now all timestamp and time calculation logic is centralized in one maintainable module. Benefits: - Single source of truth for time-related utilities - Easier maintenance and updates - Consistent behavior across all frontend components - Better code organization and documentation - Reduced bundle size through deduplication
This commit is contained in:
parent
c0dc1f3c6d
commit
a063d5a2c9
7 changed files with 103 additions and 120 deletions
|
@ -74,25 +74,7 @@ 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();
|
||||
// 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';
|
||||
if (diffInMinutes < 60) return `${diffInMinutes} minute${diffInMinutes !== 1 ? 's' : ''} ago`;
|
||||
|
||||
const diffInHours = Math.floor(diffInMinutes / 60);
|
||||
if (diffInHours < 24) return `${diffInHours} hour${diffInHours !== 1 ? 's' : ''} ago`;
|
||||
|
||||
return 'over a day ago';
|
||||
};
|
||||
// getTimeAgo function is now available from utils.js
|
||||
|
||||
const refreshLocations = () => {
|
||||
fetch('/api/locations')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue