/** * MxChat Transcripts Page JavaScript - v3.0 * Split-panel layout with chat list and conversation view */ jQuery(document).ready(function($) { // ========================================================================== // Sidebar Navigation // ========================================================================== // Desktop sidebar navigation $('.mxch-nav-link').on('click', function(e) { e.preventDefault(); const target = $(this).data('target'); // Update active states $('.mxch-nav-link').removeClass('active'); $(this).addClass('active'); // Show target section $('.mxch-section').removeClass('active'); $('#' + target).addClass('active'); // Reset scroll position of content area $('.mxch-content').scrollTop(0); // Also update mobile nav if open $('.mxch-mobile-nav-link').removeClass('active'); $('.mxch-mobile-nav-link[data-target="' + target + '"]').addClass('active'); // Load transcripts when switching to all-chats if (target === 'all-chats' && !transcriptsLoaded) { loadChatList(1, ''); } }); // Mobile menu toggle $('.mxch-mobile-menu-btn').on('click', function() { $('.mxch-mobile-menu').addClass('open'); $('.mxch-mobile-overlay').addClass('open'); }); // Close mobile menu $('.mxch-mobile-menu-close, .mxch-mobile-overlay').on('click', function() { $('.mxch-mobile-menu').removeClass('open'); $('.mxch-mobile-overlay').removeClass('open'); }); // Mobile navigation $('.mxch-mobile-nav-link').on('click', function(e) { e.preventDefault(); const target = $(this).data('target'); $('.mxch-mobile-nav-link').removeClass('active'); $(this).addClass('active'); $('.mxch-section').removeClass('active'); $('#' + target).addClass('active'); // Reset scroll position of content area $('.mxch-content').scrollTop(0); $('.mxch-nav-link').removeClass('active'); $('.mxch-nav-link[data-target="' + target + '"]').addClass('active'); $('.mxch-mobile-menu').removeClass('open'); $('.mxch-mobile-overlay').removeClass('open'); }); // Quick action buttons $('.mxch-quick-action-btn[data-action="view-chats"]').on('click', function() { $('.mxch-nav-link[data-target="all-chats"]').trigger('click'); }); $('.mxch-quick-action-btn[data-action="settings"]').on('click', function() { $('.mxch-nav-link[data-target="notifications"]').trigger('click'); }); // ========================================================================== // Mobile Panel Management // ========================================================================== function isMobile() { return window.innerWidth <= 782; } function showMobileConversationPanel() { if (isMobile()) { $('.mxch-chat-list-panel').addClass('panel-hidden'); $('#mxch-conversation-panel').addClass('panel-active'); $('#mxch-transcript-back-btn').show(); } } function hideMobileConversationPanel() { if (isMobile()) { $('#mxch-conversation-panel').removeClass('panel-active'); $('.mxch-chat-list-panel').removeClass('panel-hidden'); $('#mxch-transcript-back-btn').hide(); } } // Mobile back button handler $('#mxch-transcript-back-btn').on('click', function(e) { e.preventDefault(); hideMobileConversationPanel(); $('.mxch-chat-item').removeClass('active'); currentSessionId = null; }); // Handle window resize $(window).on('resize', function() { if (!isMobile()) { // Reset panel states when switching to desktop $('.mxch-chat-list-panel').removeClass('panel-hidden'); $('#mxch-conversation-panel').removeClass('panel-active'); $('#mxch-transcript-back-btn').hide(); } updateMobileViewportHeight(); }); // Fix for mobile browser address bar - sets CSS custom property for accurate viewport height function updateMobileViewportHeight() { if (isMobile()) { // Use visualViewport if available (most reliable for mobile) const vh = window.visualViewport ? window.visualViewport.height : window.innerHeight; document.documentElement.style.setProperty('--mxch-mobile-vh', vh + 'px'); } } // Update on load and viewport changes updateMobileViewportHeight(); if (window.visualViewport) { window.visualViewport.addEventListener('resize', updateMobileViewportHeight); } // ========================================================================== // Chat List - Split Panel // ========================================================================== let currentPage = 1; const perPage = 50; let totalPages = 1; let currentSessionId = null; let transcriptsLoaded = false; let selectedSessions = new Set(); let currentSortOrder = 'desc'; // newest first // Load chat list on page load loadChatList(1, ''); // Search functionality with debounce let searchTimeout; $('#mxch-search-transcripts').on('input', function() { clearTimeout(searchTimeout); const searchTerm = $(this).val().toLowerCase(); searchTimeout = setTimeout(function() { currentPage = 1; loadChatList(currentPage, searchTerm); }, 300); }); // Refresh button $('#mxch-refresh-list').on('click', function() { const $btn = $(this); $btn.addClass('spinning'); loadChatList(currentPage, $('#mxch-search-transcripts').val()); setTimeout(() => $btn.removeClass('spinning'), 500); }); // ========================================================================== // Bulk Selection & Actions // ========================================================================== // Select all checkbox $('#mxch-select-all').on('change', function() { const isChecked = $(this).is(':checked'); $('.mxch-chat-checkbox').prop('checked', isChecked); if (isChecked) { $('.mxch-chat-item').each(function() { selectedSessions.add($(this).data('session-id')); $(this).addClass('selected'); }); $('#mxch-chat-list').addClass('selection-mode'); } else { selectedSessions.clear(); $('.mxch-chat-item').removeClass('selected'); $('#mxch-chat-list').removeClass('selection-mode'); } updateSelectionUI(); }); // Update selection UI function updateSelectionUI() { const count = selectedSessions.size; const $countEl = $('#mxch-selected-count'); const $deleteBtn = $('#mxch-delete-selected'); if (count > 0) { $countEl.text(count + ' selected').addClass('has-selection'); $deleteBtn.prop('disabled', false); $('#mxch-chat-list').addClass('selection-mode'); } else { $countEl.removeClass('has-selection'); $deleteBtn.prop('disabled', true); $('#mxch-chat-list').removeClass('selection-mode'); } // Update select all checkbox state const totalItems = $('.mxch-chat-checkbox').length; const checkedItems = $('.mxch-chat-checkbox:checked').length; $('#mxch-select-all').prop('checked', totalItems > 0 && checkedItems === totalItems); $('#mxch-select-all').prop('indeterminate', checkedItems > 0 && checkedItems < totalItems); } // Sort button $('#mxch-sort-btn').on('click', function() { currentSortOrder = currentSortOrder === 'desc' ? 'asc' : 'desc'; $(this).find('svg').css('transform', currentSortOrder === 'asc' ? 'rotate(180deg)' : 'rotate(0deg)'); loadChatList(currentPage, $('#mxch-search-transcripts').val()); }); // Delete selected button $('#mxch-delete-selected').on('click', function() { const count = selectedSessions.size; if (count === 0) return; if (!confirm('Are you sure you want to delete ' + count + ' conversation(s)? This action cannot be undone.')) { return; } deleteMultipleSessions(Array.from(selectedSessions)); }); // Delete multiple sessions function deleteMultipleSessions(sessionIds) { $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'mxchat_delete_chat_history', delete_session_ids: sessionIds, security: $('#mxchat_delete_chat_nonce').val() }, success: function(response) { try { const jsonResponse = typeof response === 'object' ? response : JSON.parse(response); if (jsonResponse.success) { // Clear selection selectedSessions.clear(); $('#mxch-select-all').prop('checked', false); updateSelectionUI(); // If current conversation was deleted, reset panel if (sessionIds.includes(currentSessionId)) { currentSessionId = null; $('#mxch-conversation-content').hide(); $('#mxch-conversation-empty').show(); $('#mxch-details-drawer').hide(); } // Reload list loadChatList(currentPage, $('#mxch-search-transcripts').val()); } else if (jsonResponse.error) { alert('Error: ' + jsonResponse.error); } } catch (e) { alert('An error occurred while processing the response.'); } }, error: function() { alert('An error occurred while deleting conversations.'); } }); } // Load chat list function function loadChatList(page, searchTerm) { const $container = $('#mxch-chat-list'); $container.html('
No chats found
Error loading chats