// Logs Page Logic
async function loadLogs() {
const terminal = document.getElementById('logsTerminal');
if (!terminal) return;
terminal.innerHTML = '
Loading logs...
';
try {
const logs = await window.electronAPI.getRecentLogs(500); // Fetch last 500 lines
if (logs) {
// Escape HTML to prevent XSS and preserve format
const safeLogs = logs.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
terminal.innerHTML = `${safeLogs}`;
// Auto scroll to bottom
terminal.scrollTop = terminal.scrollHeight;
} else {
terminal.innerHTML = 'No logs found.
';
}
} catch (error) {
console.error('Failed to load logs:', error);
terminal.innerHTML = `Error loading logs: ${error.message}
`;
}
}
async function refreshLogs() {
const btn = document.querySelector('button[onclick="refreshLogs()"] i');
if (btn) btn.classList.add('fa-spin');
await loadLogs();
if (btn) setTimeout(() => btn.classList.remove('fa-spin'), 500);
}
async function copyLogs() {
const terminal = document.getElementById('logsTerminal');
if (!terminal) return;
const content = terminal.innerText;
if (!content) return;
try {
await navigator.clipboard.writeText(content);
const btn = document.querySelector('button[onclick="copyLogs()"]');
const originalText = btn.innerHTML;
btn.innerHTML = ' Copied!';
setTimeout(() => {
btn.innerHTML = originalText;
}, 2000);
} catch (err) {
console.error('Failed to copy logs:', err);
}
}
async function openLogsFolder() {
await window.electronAPI.openLogsFolder();
}
async function sendLogs() {
const btn = document.getElementById('sendLogsBtn');
if (!btn || btn.disabled) return;
// Get i18n strings with fallbacks
const i18n = window.i18n || {};
const sendingText = (i18n.settings && i18n.settings.logsSending) || 'Sending...';
const sentText = (i18n.settings && i18n.settings.logsSent) || 'Sent!';
const failedText = (i18n.settings && i18n.settings.logsSendFailed) || 'Failed';
const sendText = (i18n.settings && i18n.settings.logsSend) || 'Send Logs';
const originalHTML = btn.innerHTML;
btn.disabled = true;
btn.innerHTML = ` ${sendingText}`;
try {
const result = await window.electronAPI.sendLogs();
if (result.success) {
btn.innerHTML = ` ${sentText}`;
showLogSubmissionResult(result.id);
} else {
btn.innerHTML = ` ${failedText}`;
console.error('Send logs failed:', result.error);
// Show error notification if available
if (window.LauncherUI && window.LauncherUI.showNotification) {
window.LauncherUI.showNotification(result.error || 'Failed to send logs', 'error');
}
}
} catch (err) {
console.error('Send logs error:', err);
btn.innerHTML = ` ${failedText}`;
}
setTimeout(() => {
btn.disabled = false;
btn.innerHTML = originalHTML;
}, 3000);
}
function showLogSubmissionResult(id) {
// Remove existing popup if any
const existing = document.getElementById('logSubmissionPopup');
if (existing) existing.remove();
const i18n = window.i18n || {};
const idLabel = (i18n.settings && i18n.settings.logsSubmissionId) || 'Submission ID';
const copyText = (i18n.common && i18n.common.copy) || 'Copy';
const closeText = (i18n.common && i18n.common.close) || 'Close';
const shareText = (i18n.settings && i18n.settings.logsShareId) || 'Share this ID with support when reporting issues';
const popup = document.createElement('div');
popup.id = 'logSubmissionPopup';
popup.style.cssText = `
position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
background: rgba(20, 20, 35, 0.98); border: 1px solid rgba(0, 212, 255, 0.3);
border-radius: 12px; padding: 24px 32px; z-index: 10000;
box-shadow: 0 20px 60px rgba(0,0,0,0.5); text-align: center;
min-width: 320px; backdrop-filter: blur(10px);
`;
popup.innerHTML = `
${idLabel}
${id}
${shareText}
`;
document.body.appendChild(popup);
// Auto-close after 30s
setTimeout(() => {
if (document.getElementById('logSubmissionPopup')) {
popup.remove();
}
}, 30000);
}
async function copyLogSubmissionId(id) {
try {
await navigator.clipboard.writeText(id);
const btn = event.target.closest('button');
if (btn) {
const orig = btn.innerHTML;
btn.innerHTML = ' Copied!';
setTimeout(() => { btn.innerHTML = orig; }, 1500);
}
} catch (err) {
console.error('Failed to copy submission ID:', err);
}
}
function openLogs() {
// Navigation is handled by sidebar logic, but we can trigger a refresh
window.LauncherUI.showPage('logs-page');
window.LauncherUI.setActiveNav('logs');
refreshLogs();
}
// Expose functions globally
window.refreshLogs = refreshLogs;
window.copyLogs = copyLogs;
window.openLogsFolder = openLogsFolder;
window.sendLogs = sendLogs;
window.copyLogSubmissionId = copyLogSubmissionId;
window.openLogs = openLogs;
// Auto-load logs when the page becomes active
const logsObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.target.classList.contains('active') && mutation.target.id === 'logs-page') {
loadLogs();
}
});
});
document.addEventListener('DOMContentLoaded', () => {
const logsPage = document.getElementById('logs-page');
if (logsPage) {
logsObserver.observe(logsPage, { attributes: true, attributeFilter: ['class'] });
}
});