Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion assets/js/wpr-admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/wpr-admin.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/wpr-admin.min.js.map

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions src/js/global/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,48 @@ document.addEventListener('DOMContentLoaded', function() {
}
}

/**
* Broadcasts global score changes to other tabs via localStorage.
* Uses a timestamp to ensure the storage event fires even with identical data.
*
* @param {Object} scoreData - The global score data to broadcast.
*/
function broadcastGlobalScoreUpdate(scoreData) {
try {
const payload = {
data: scoreData,
timestamp: Date.now()
};
localStorage.setItem('wpr_global_score_update', JSON.stringify(payload));
} catch (e) {
console.error('Failed to broadcast global score update:', e);
}
}

/**
* Updates the global score UI in the current tab.
* Handles both dashboard widget and Rocket Insights table row updates.
*
* @param {Object} newScoreData - The new global score data to display.
*/
function updateGlobalScoreUI(newScoreData) {
// Update local state
globalScoreData = newScoreData;

// Update dashboard widget if visible
if (isOnDashboard()) {
const $widget = $('#wpr_global_score_widget');
if ($widget.length && newScoreData.html) {
$widget.html(newScoreData.html);
}
}

// Update Rocket Insights table row if visible
if (isOnRocketInsights() && newScoreData.row_html) {
updateGlobalScoreRow(newScoreData);
}
}
Comment on lines 434 to 455
Copy link

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function duplicates existing UI update logic found elsewhere in the codebase. The dashboard widget update on lines 441-443 is identical to line 528, and the Rocket Insights update on lines 447-448 is identical to line 591. This creates maintainability issues if the update logic needs to change.

Copilot uses AI. Check for mistakes.

/**
* Updates the global score UI widget or table row based on the selected menu.
* When the dashboard or rocket insights menu is clicked, this function updates
Expand Down Expand Up @@ -478,6 +520,9 @@ document.addEventListener('DOMContentLoaded', function() {
// Update global score data.
globalScoreData = response.data.global_score_data;

// Broadcast to other tabs
broadcastGlobalScoreUpdate(globalScoreData);

// Update global score widget if on dashboard.
if ( isOnDashboard() ) {
$('#wpr_global_score_widget').html(response.data.global_score_data.html);
Expand Down Expand Up @@ -539,6 +584,9 @@ document.addEventListener('DOMContentLoaded', function() {
// Update global score data.
globalScoreData = response.data.global_score_data;

// Broadcast to other tabs
broadcastGlobalScoreUpdate(globalScoreData);

// Update global score row in table if on Rocket Insights page.
updateGlobalScoreRow(globalScoreData);

Expand Down Expand Up @@ -599,6 +647,9 @@ document.addEventListener('DOMContentLoaded', function() {
// Update global score data.
globalScoreData = response.data.global_score_data;

// Broadcast to other tabs
broadcastGlobalScoreUpdate(globalScoreData);

// Update global score row in table if on Rocket Insights page.
updateGlobalScoreRow(globalScoreData);
// Start polling if not already running
Expand All @@ -613,6 +664,20 @@ document.addEventListener('DOMContentLoaded', function() {
}

// ==== Initialization ====
// Listen for global score updates from other tabs
window.addEventListener('storage', function(e) {
if (e.key === 'wpr_global_score_update' && e.newValue) {
try {
const payload = JSON.parse(e.newValue);
if (payload.data) {
updateGlobalScoreUI(payload.data);
}
} catch (error) {
console.error('Failed to parse global score update:', error);
}
}
});

// Bind event
$(document).on( 'click', '#wpr-action-add_page_speed_radar', handleAddPage );
$(document).on( 'click', '.wpr-action-speed_radar_refresh', handleResetPage );
Expand Down
Loading