jQuery(document).ready(function($) { // Track if a form has been submitted to trigger updates let formSubmitted = false; // Global interval ID to manage the polling let updateIntervalId = null; $(document).on('click', '.mxchat-dismiss-button', function() { const $button = $(this); const $card = $button.closest('.mxchat-status-card'); // Determine card type from data attribute or content let cardType = $card.data('card-type'); if (!cardType) { // Fallback: determine from content cardType = $card.find('h4').text().includes('PDF') ? 'pdf' : 'sitemap'; } // Fade out and remove the card $card.fadeOut(300, function() { $(this).remove(); }); // Clear the completed status on the server $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'mxchat_dismiss_completed_status', nonce: mxchatAdmin.status_nonce, card_type: cardType }, success: function(response) { //console.log('MxChat: Completed status dismissed'); }, error: function(xhr, status, error) { console.error('MxChat: Error dismissing status:', error); } }); }); // Check if we're on the right admin page with status cards or import forms if ($('.mxchat-status-card').length > 0 || $('.mxchat-import-options').length > 0) { //console.log('MxChat: Status update script initialized'); // Initialize AJAX status updates initStatusUpdates(); } // Initialize status updates // Initialize status updates function initStatusUpdates() { // Get the refresh interval (default to 2 seconds for more responsive updates) const refreshInterval = parseInt(mxchatAdmin.status_refresh_interval || 2000); // Check if there are active status cards const hasActiveStatus = $('.mxchat-status-card').length > 0; // Set up form submission listeners $('#mxchat-url-form, #mxchat-content-form').on('submit', function() { //console.log('MxChat: Form submitted, will start checking for updates'); formSubmitted = true; // Store submission info in sessionStorage to persist through redirects sessionStorage.setItem('mxchat_form_submitted', 'true'); sessionStorage.setItem('mxchat_form_submitted_time', Date.now()); // Start checking for status updates right away startPolling(refreshInterval); // Create a temporary message if ($('.mxchat-processing-message').length === 0) { const message = $('
'); $('.mxchat-import-section').after(message); // Fade out after 5 seconds setTimeout(function() { message.fadeOut(500, function() { $(this).remove(); }); }, 5000); } }); // Listen for import option clicks $('.mxchat-import-box').on('click', function() { const option = $(this).data('option'); //console.log('MxChat: Import option clicked - ' + option); }); // Check if we recently submitted a form (within last 60 seconds for sitemap processing) if (sessionStorage.getItem('mxchat_form_submitted') === 'true') { const submittedTime = parseInt(sessionStorage.getItem('mxchat_form_submitted_time') || '0'); if (Date.now() - submittedTime < 60000) { // 60 seconds //console.log('MxChat: Detected recent form submission via sessionStorage'); formSubmitted = true; } else { // Clear old submission data sessionStorage.removeItem('mxchat_form_submitted'); sessionStorage.removeItem('mxchat_form_submitted_time'); } } // Attach event listener to stop button to clear the interval $('.mxchat-stop-form').on('submit', function() { //console.log('MxChat: Stop processing requested, clearing update interval'); stopPolling(); sessionStorage.removeItem('mxchat_form_submitted'); sessionStorage.removeItem('mxchat_form_submitted_time'); }); // Start the interval for automatic updates if we have status cards or a form was submitted if (hasActiveStatus || formSubmitted) { //console.log('MxChat: Starting automatic status checks'); startPolling(refreshInterval); } } // Function to start polling function startPolling(interval) { // Clear any existing interval first stopPolling(); // Do an initial fetch immediately fetchStatusUpdates(); // Set up new interval updateIntervalId = setInterval(function() { fetchStatusUpdates(); }, interval); //console.log('MxChat: Polling started with interval', interval); } // Function to stop polling function stopPolling() { if (updateIntervalId !== null) { clearInterval(updateIntervalId); updateIntervalId = null; //console.log('MxChat: Polling stopped'); } } // Fetch status updates from the server function fetchStatusUpdates() { // If user is actively viewing the failed URLs or pages, don't refresh as frequently const $details = $('.mxchat-failed-urls-container details, .mxchat-failed-pages-container details'); const isUserViewing = $details.length > 0 && $details.prop('open'); // If details are open, we'll refresh at a slower rate if (isUserViewing) { // Alternative: Update less frequently when details are open setTimeout(function() { performStatusUpdate(false); // Pass false for normal updates }, 5000); // Slow down updates to every 5 seconds when details are open } else { performStatusUpdate(false); // Pass false for normal updates } } // Perform the actual AJAX request function performStatusUpdate(clearCompleted = false) { //console.log('MxChat: Checking for status updates...'); $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'mxchat_get_status_updates', nonce: mxchatAdmin.status_nonce, clear_completed: clearCompleted ? 'true' : 'false' }, success: function(response) { //console.log('MxChat: Status update received', response); // Log specific status details for debugging if (response.sitemap_status) { //console.log('Sitemap status:', response.sitemap_status.status, 'Processed:', response.sitemap_status.processed_urls, 'Total:', response.sitemap_status.total_urls); } // Check for completion BEFORE updating UI let shouldStopPolling = false; if (response.sitemap_status && response.sitemap_status.status === 'complete') { //console.log('MxChat: Sitemap processing complete'); shouldStopPolling = true; } if (response.pdf_status && response.pdf_status.status === 'complete') { //console.log('MxChat: PDF processing complete'); shouldStopPolling = true; } // Always update UI if ((response && response.is_processing) || formSubmitted || shouldStopPolling) { updateStatusUI(response); } // Show single URL status if available and no active processing if (response.single_url_status && !response.is_processing) { updateSingleUrlStatus(response.single_url_status); } // Handle completion - REMOVE THE AUTOMATIC PAGE RELOAD if (shouldStopPolling) { // Clear session storage sessionStorage.removeItem('mxchat_form_submitted'); sessionStorage.removeItem('mxchat_form_submitted_time'); // Stop polling stopPolling(); // DON'T clear the completed status automatically // DON'T reload the page automatically return; // Exit early } // Reset form submitted flag if no active processing if (!response.is_processing) { formSubmitted = false; sessionStorage.removeItem('mxchat_form_submitted'); sessionStorage.removeItem('mxchat_form_submitted_time'); stopPolling(); } }, error: function(xhr, status, error) { console.error('MxChat: Status update failed:', error); } }); } function addDismissButtonToCompletedCards() { // Add dismiss buttons to completed cards that don't have them $('.mxchat-status-card').each(function() { const $card = $(this); const $badge = $card.find('.mxchat-status-badge'); // Check if this is a completed card and doesn't already have a dismiss button if (($badge.hasClass('mxchat-status-success') || $badge.hasClass('mxchat-status-warning')) && $card.find('.mxchat-dismiss-button').length === 0) { // Look for existing action buttons container, or create one let $actionContainer = $card.find('.mxchat-action-buttons'); if ($actionContainer.length === 0) { // Create the action buttons container if it doesn't exist $actionContainer = $(''); $card.find('.mxchat-status-header').append($actionContainer); } // Add dismiss button WITHOUT inline styles const dismissButton = $(''); dismissButton.on('click', function() { // Fade out and remove the card $card.fadeOut(300, function() { $(this).remove(); }); // Clear the completed status on the server $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'mxchat_dismiss_completed_status', nonce: mxchatAdmin.status_nonce, card_type: $card.find('h4').text().includes('PDF') ? 'pdf' : 'sitemap' }, success: function(response) { //console.log('MxChat: Completed status dismissed'); } }); }); $actionContainer.append(dismissButton); } }); } // Update the UI with status information function updateStatusUI(data) { // Update PDF status if available if (data.pdf_status) { updatePdfStatus(data.pdf_status); } // Update sitemap status if available if (data.sitemap_status) { updateSitemapStatus(data.sitemap_status); } // Handle single URL status if available and no active processing if (data.single_url_status && !data.is_processing) { updateSingleUrlStatus(data.single_url_status); } else if (data.is_processing) { // Hide single URL status while processing $('#mxchat-single-url-status-container').hide(); } // Add dismiss buttons to any completed cards addDismissButtonToCompletedCards(); } // Update PDF status card function updatePdfStatus(status) { // Check if PDF card exists let $pdfCard = $('.mxchat-status-card:contains("PDF Processing")'); // If no card exists but we have status, create it if ($pdfCard.length === 0 && status) { //console.log('MxChat: Creating new PDF status card'); createPdfStatusCard(status); $pdfCard = $('.mxchat-status-card:contains("PDF Processing")'); } // If card exists, update it if ($pdfCard.length > 0) { // Update progress bar $pdfCard.find('.mxchat-progress-fill').css('width', status.percentage + '%'); // Update progress text let progressText = 'Progress: ' + status.processed_pages + ' of ' + status.total_pages + ' pages (' + status.percentage + '%)'; $pdfCard.find('.mxchat-status-details p:first').text(progressText); // Update failed pages count if exists const $failedText = $pdfCard.find('.mxchat-status-details p:contains("Failed pages")'); if (status.failed_pages && status.failed_pages > 0) { if ($failedText.length === 0) { // Add failed pages text after progress $pdfCard.find('.mxchat-status-details p:first').after( 'Failed pages: ' + status.failed_pages + '
' ); } else { $failedText.html('Failed pages: ' + status.failed_pages); } } else if ($failedText.length > 0) { $failedText.remove(); } // Update status text const $statusText = $pdfCard.find('.mxchat-status-details p:contains("Status:")'); if ($statusText.length > 0) { $statusText.text('Status: ' + status.status.charAt(0).toUpperCase() + status.status.slice(1)); } // Update last update text const $lastUpdateText = $pdfCard.find('.mxchat-status-details p:contains("Last update:")'); if ($lastUpdateText.length > 0) { $lastUpdateText.text('Last update: ' + status.last_update); } // Update status badges $pdfCard.find('.mxchat-status-badge').remove(); if (status.status === 'error') { $pdfCard.find('.mxchat-status-header').append('Error'); } else if (status.status === 'complete') { if (status.failed_pages && status.failed_pages > 0) { $pdfCard.find('.mxchat-status-header').append('Completed with ' + status.failed_pages + ' failures'); } else { $pdfCard.find('.mxchat-status-header').append('Complete'); } } // Update or add completion summary if (status.completion_summary) { let $summaryContainer = $pdfCard.find('.mxchat-completion-summary'); if ($summaryContainer.length === 0) { const summaryHtml = 'Total Pages: ' + status.completion_summary.total_pages + '
' + 'Successful: ' + status.completion_summary.successful_pages + '
' + 'Failed: ' + status.completion_summary.failed_pages + '
' + 'Completed: ' + status.completion_summary.completion_time + '
' + 'Progress: ' + status.processed_pages + ' of ' + status.total_pages + ' pages (' + status.percentage + '%)
'; // Show failed pages count if any if (status.failed_pages && status.failed_pages > 0) { html += 'Failed pages: ' + status.failed_pages + '
'; } html += 'Status: ' + status.status.charAt(0).toUpperCase() + status.status.slice(1) + '
'; html += 'Last update: ' + status.last_update + '
'; // Add completion summary if available if (status.completion_summary) { html += 'Total Pages: ' + status.completion_summary.total_pages + '
'; html += 'Successful: ' + status.completion_summary.successful_pages + '
'; html += 'Failed: ' + status.completion_summary.failed_pages + '
'; html += 'Completed: ' + status.completion_summary.completion_time + '
'; html += '' + status.error + '
'; html += 'Failed URLs: ' + status.failed_urls + '
' ); } else { $failedText.html('Failed URLs: ' + status.failed_urls); } } else if ($failedText.length > 0) { $failedText.remove(); } // Update status badges $sitemapCard.find('.mxchat-status-badge').remove(); if (status.status === 'error') { $sitemapCard.find('.mxchat-status-header').append('Error'); } else if (status.status === 'complete') { if (status.failed_urls && status.failed_urls > 0) { $sitemapCard.find('.mxchat-status-header').append('Completed with ' + status.failed_urls + ' failures'); } else { $sitemapCard.find('.mxchat-status-header').append('Complete'); } } // Update or add completion summary if (status.completion_summary) { let $summaryContainer = $sitemapCard.find('.mxchat-completion-summary'); if ($summaryContainer.length === 0) { const summaryHtml = 'Total URLs: ' + status.completion_summary.total_urls + '
' + 'Successful: ' + status.completion_summary.successful_urls + '
' + 'Failed: ' + status.completion_summary.failed_urls + '
' + 'Completed: ' + status.completion_summary.completion_time + '
' + '' + status.error + '
'; } if (status.last_error) { errorHTML += 'Last error: ' + status.last_error + '
'; } // Add failed URLs list if (status.failed_urls_list && status.failed_urls_list.length > 0) { errorHTML += '| URL | Error | Retries | Time |
|---|---|---|---|
| '; errorHTML += ''; errorHTML += truncateUrl(item.url) + ' | '; errorHTML += '' + item.error + ' | '; errorHTML += '' + retries + ' | '; errorHTML += '' + timeAgo + ' | '; errorHTML += '
| Page | Error | Retries | Time |
|---|---|---|---|
| Page ' + item.page + ' | '; html += '' + item.error + ' | '; html += '' + item.retries + ' | '; html += '' + timeAgo + ' | '; html += '
Progress: ' + status.processed_urls + ' of ' + status.total_urls + ' URLs (' + status.percentage + '%)
'; // Add error message if any if ((status.error || status.last_error) && status.status === 'error') { html += '' + status.error + '
'; } if (status.last_error) { html += 'Last error: ' + status.last_error + '
'; } html += 'URL: '; html += ''; // Truncate URL if needed const displayUrl = status.url.length > 60 ? status.url.substring(0, 57) + '...' : status.url; html += displayUrl; html += '
'; html += 'Submitted: ' + status.human_time + '
'; if (status.status === 'failed' && status.error) { html += '' + status.error + '
'; html += 'Content Length: ' + status.content_length + ' characters
'; html += 'Embedding Dimensions: ' + status.embedding_dimensions + '
'; } html += '