# mxchat-basic/2.1.7/js/admin-status.js

MxChat – AI Chatbot &amp; Content Generation for WordPress, version 2.1.7. 366 lines.

- Page: https://pluginprobe.com/plugins/mxchat-basic/2.1.7/code/js/admin-status.js
- Raw: https://pluginprobe.com/plugins/mxchat-basic/2.1.7/raw/js/admin-status.js
- Modified: 2025-04-27T02:19:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/mxchat-basic/2.1.7/code/js/admin-status.js#L10-L20`.

```javascript
/**
 * 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 = $('<div class="mxchat-status-card"></div>');
            $('.mxchat-import-form').after(pdfCard);

            // Create header
            let header = $('<div class="mxchat-status-header"></div>');
            header.append('<h4>PDF Processing Status</h4>');
            header.append('<span class="mxchat-status-badge mxchat-status-failed">Error</span>');
            pdfCard.append(header);

            // Create progress bar
            let progressBar = $('<div class="mxchat-progress-bar"></div>');
            progressBar.append('<div class="mxchat-progress-fill" style="width: ' + status.percentage + '%"></div>');
            pdfCard.append(progressBar);

            // Create details section
            let details = $('<div class="mxchat-status-details"></div>');
            pdfCard.append(details);

            // Add progress info
            details.append('<p>Progress: ' + status.processed_pages + ' of ' + 
                status.total_pages + ' pages (' + status.percentage + '%)</p>');

            // Add status info
            details.append('<p>Status: Error</p>');

            // Add last update info
            details.append('<p>Last update: ' + status.last_update + '</p>');

            // Add error message
            if (status.error) {
                let errorNotice = $('<div class="mxchat-error-notice"></div>');
                errorNotice.append('<p class="error">' + status.error + '</p>');
                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('<span class="mxchat-status-badge mxchat-status-failed">Error</span>');
            }

            // 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('<p>Status: Error</p>');
            }

            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('<p>Last update: ' + status.last_update + '</p>');
            }

            // 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 = $('<div class="mxchat-error-notice"></div>');
                    errorNotice.append('<p class="error">' + status.error + '</p>');
                    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 = $('<div id="mxchat-single-url-status-container"></div>');
            $('.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 = '<div class="mxchat-status-card">';
        html += '<div class="mxchat-status-header">';
        html += '<h4>Last URL Submission</h4>';

        if (status.status === 'failed') {
            html += '<span class="mxchat-status-badge mxchat-status-failed">Failed</span>';
        } else {
            html += '<span class="mxchat-status-badge mxchat-status-success">Success</span>';
        }

        html += '</div>'; // End header

        html += '<div class="mxchat-status-details">';
        html += '<p><strong>URL:</strong> ';
        html += '<a href="' + status.url + '" target="_blank">';

        // Truncate URL if needed
        const displayUrl = status.url.length > 60 ? status.url.substring(0, 57) + '...' : status.url;
        html += displayUrl;

        html += '</a></p>';
        html += '<p><strong>Submitted:</strong> ' + status.human_time + '</p>';

        if (status.status === 'failed' && status.error) {
            html += '<div class="mxchat-error-notice">';
            html += '<p class="error">' + status.error + '</p>';
            html += '</div>';
        }

        if (status.status === 'complete') {
            html += '<p><strong>Content Length:</strong> ' + status.content_length + ' characters</p>';
            html += '<p><strong>Embedding Dimensions:</strong> ' + status.embedding_dimensions + '</p>';
        }

        html += '</div>'; // End details
        html += '</div>'; // End card

        return html;
    }

    // Initialize when the document is ready
    $(document).ready(function() {
        // Reset error detection flag
        errorDetected = false;

        // Change the text in the status headers
        $('.mxchat-status-header h4:contains("Refresh for update")').each(function() {
            $(this).text($(this).text().replace('(Refresh for update)', '(Auto-updating)'));
        });

        // Check if we already have error messages displayed
        if ($('.mxchat-error-notice').length > 0) {
            //console.log('Error notices already present, not starting auto-refresh');
            errorDetected = true;
            return; // Don't start auto-refresh if errors are already shown
        }

        // Start auto-updating if we have status cards and no errors
        if ($('.mxchat-status-card').length > 0 && !errorDetected) {
           //console.log('Starting auto-refresh for status updates');
            updateStatus(); // Run once immediately
            statusInterval = setInterval(updateStatus, refreshInterval);
        }

        // Monitor for form submissions to start the updates
        $('#mxchat-sitemap-form').on('submit', function() {
            //console.log('Form submitted, resetting error detection');
            // Reset error detection on new submission
            errorDetected = false;

            // Start updating after submission if not already running
            if (statusInterval === null) {
                //console.log('Starting auto-refresh after form submission');
                statusInterval = setInterval(updateStatus, refreshInterval);
            }
        });
    });

})(jQuery);

```
