/** * Admin status auto-update functionality */ (function($) { 'use strict'; // Status update interval ID let statusInterval = null; const refreshInterval = 3000; // 3 seconds // Flag to track if we've seen an error let errorDetected = false; /** * Updates the status cards with fresh data */ function updateStatus() { $.ajax({ url: mxchat_status_data.ajax_url, type: 'POST', data: { action: 'mxchat_get_status_updates', nonce: mxchat_status_data.nonce }, success: function(response) { // Check for error states in sitemap or PDF const hasSitemapError = response.sitemap_status && response.sitemap_status.status === 'error'; const hasPdfError = response.pdf_status && response.pdf_status.status === 'error'; // If any error is detected if (hasSitemapError || hasPdfError) { //console.log('Error detected in processing, stopping auto-refresh'); // Update PDF status UI if it has an error if (hasPdfError) { updatePdfErrorStatus(response.pdf_status); } // If this is the first time we're seeing an error if (!errorDetected) { errorDetected = true; // Stop the interval immediately if (statusInterval) { clearInterval(statusInterval); statusInterval = null; } // Show single URL status if available if (response.single_url_status) { $('#mxchat-single-url-status-container').show(); updateSingleUrlStatus(response.single_url_status); } } // Return early to prevent any page reloads return; } // Check if sitemap or PDF is in active processing const isActiveProcessing = (response.sitemap_status && response.sitemap_status.status === 'processing') || (response.pdf_status && response.pdf_status.status === 'processing'); // Handle single URL status if (response.single_url_status && !isActiveProcessing) { $('#mxchat-single-url-status-container').show(); updateSingleUrlStatus(response.single_url_status); } else if (isActiveProcessing) { $('#mxchat-single-url-status-container').hide(); } // Update sitemap status if it exists and is not in error state if (response.sitemap_status && response.sitemap_status.status !== 'error') { // Update progress bar $('.mxchat-status-card:contains("Sitemap Processing") .mxchat-progress-fill') .css('width', response.sitemap_status.percentage + '%'); // Update status text $('.mxchat-status-card:contains("Sitemap Processing") .mxchat-status-details p:first') .text('Progress: ' + response.sitemap_status.processed_urls + ' of ' + response.sitemap_status.total_urls + ' URLs (' + response.sitemap_status.percentage + '%)'); // If complete, reload once if (response.sitemap_status.status === 'complete') { //console.log('Sitemap processing complete, reloading page'); if (statusInterval) { clearInterval(statusInterval); statusInterval = null; } location.reload(); return; } } else if (!response.sitemap_status) { // If no sitemap status, remove the card with animation $('.mxchat-status-card:contains("Sitemap Processing")').fadeOut(300); } // Update PDF status if it exists and is not in error state if (response.pdf_status && response.pdf_status.status !== 'error') { updatePdfStatus(response.pdf_status); // If complete, reload once if (response.pdf_status.status === 'complete') { //console.log('PDF processing complete, reloading page'); if (statusInterval) { clearInterval(statusInterval); statusInterval = null; } location.reload(); return; } } else if (!response.pdf_status) { // If no PDF status, remove the card with animation $('.mxchat-status-card:contains("PDF Processing")').fadeOut(300); } // If nothing is processing anymore and no errors detected, stop checking if (!isActiveProcessing && !errorDetected) { //console.log('No active processing, stopping auto-refresh'); if (statusInterval) { clearInterval(statusInterval); statusInterval = null; } // Show single URL status if it exists if (response.single_url_status) { $('#mxchat-single-url-status-container').show(); updateSingleUrlStatus(response.single_url_status); } else { // Only reload if we have no single URL status location.reload(); } } }, error: function(xhr, status, error) { //console.error('AJAX error:', error); // If AJAX fails, stop the interval if (statusInterval) { clearInterval(statusInterval); statusInterval = null; } } }); } /** * Updates the PDF status card with normal processing info */ function updatePdfStatus(status) { // Update progress bar $('.mxchat-status-card:contains("PDF Processing") .mxchat-progress-fill') .css('width', status.percentage + '%'); // Update status text $('.mxchat-status-card:contains("PDF Processing") .mxchat-status-details p:first') .text('Progress: ' + status.processed_pages + ' of ' + status.total_pages + ' pages (' + status.percentage + '%)'); // Update status and last update let statusText = $('.mxchat-status-card:contains("PDF Processing") .mxchat-status-details p:nth-child(2)'); if (statusText.length) { statusText.text('Status: ' + (status.status.charAt(0).toUpperCase() + status.status.slice(1))); } let lastUpdateText = $('.mxchat-status-card:contains("PDF Processing") .mxchat-status-details p:nth-child(3)'); if (lastUpdateText.length) { lastUpdateText.text('Last update: ' + status.last_update); } } /** * Updates the PDF status card with error information */ function updatePdfErrorStatus(status) { // Find the PDF status card let pdfCard = $('.mxchat-status-card:contains("PDF Processing")'); if (pdfCard.length === 0) { // If card doesn't exist for some reason, we'll create it pdfCard = $('
'); $('.mxchat-import-form').after(pdfCard); // Create header let header = $(''); header.append('Progress: ' + status.processed_pages + ' of ' + status.total_pages + ' pages (' + status.percentage + '%)
'); // Add status info details.append('Status: Error
'); // Add last update info details.append('Last update: ' + status.last_update + '
'); // Add error message if (status.error) { let errorNotice = $(''); errorNotice.append('' + status.error + '
'); details.append(errorNotice); } } else { // Update existing card // Make sure we have the error badge if (pdfCard.find('.mxchat-status-badge.mxchat-status-failed').length === 0) { pdfCard.find('.mxchat-status-header').append('Error'); } // Update progress bar pdfCard.find('.mxchat-progress-fill').css('width', status.percentage + '%'); // Update status text pdfCard.find('.mxchat-status-details p:first') .text('Progress: ' + status.processed_pages + ' of ' + status.total_pages + ' pages (' + status.percentage + '%)'); // Update status and last update let statusText = pdfCard.find('.mxchat-status-details p:nth-child(2)'); if (statusText.length) { statusText.text('Status: Error'); } else { pdfCard.find('.mxchat-status-details').append('Status: Error
'); } let lastUpdateText = pdfCard.find('.mxchat-status-details p:nth-child(3)'); if (lastUpdateText.length) { lastUpdateText.text('Last update: ' + status.last_update); } else { pdfCard.find('.mxchat-status-details').append('Last update: ' + status.last_update + '
'); } // Add or update error message if (status.error) { let errorNotice = pdfCard.find('.mxchat-error-notice'); if (errorNotice.length) { errorNotice.find('p.error').text(status.error); } else { errorNotice = $(''); errorNotice.append('' + status.error + '
'); pdfCard.find('.mxchat-status-details').append(errorNotice); } } } } /** * Updates the single URL status card */ function updateSingleUrlStatus(status) { // Check if the status container exists let container = $('#mxchat-single-url-status-container'); if (container.length === 0) { // Create container if it doesn't exist container = $(''); $('.mxchat-import-form').after(container); } // Generate the HTML for the status card const html = generateSingleUrlStatusHtml(status); // Update the container - replace entire content container.html(html); } /** * Generates HTML for the single URL status card */ function generateSingleUrlStatusHtml(status) { let 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 += '