2025-08-11 14:46:35 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
|
<title>Email Alerts</title>
|
2025-08-13 14:49:23 +01:00
|
|
|
<link rel="stylesheet" href="{{ url_for('static', path='/styles.css') }}" />
|
2025-08-11 14:46:35 +01:00
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<header>
|
|
|
|
|
<div class="inner">
|
|
|
|
|
<h1>Email Alerts</h1>
|
|
|
|
|
<nav>
|
|
|
|
|
<a href="/">Threads</a>
|
2025-08-13 14:49:23 +01:00
|
|
|
<a href="/config">Config</a>
|
|
|
|
|
<button id="processEmailsBtn" style="margin-left:12px;">Process Emails</button>
|
|
|
|
|
|
2025-08-12 10:41:47 +01:00
|
|
|
<form action="/process" method="post" style="display:inline-block; margin-left:12px;">
|
|
|
|
|
<button type="submit">Process Alerts</button>
|
|
|
|
|
</form>
|
2025-08-11 14:46:35 +01:00
|
|
|
</nav>
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
2025-08-13 14:49:23 +01:00
|
|
|
<!-- Notification banner -->
|
|
|
|
|
<div id="notify-root" class="notify-banner" aria-live="polite" aria-atomic="false"></div>
|
2025-08-11 14:46:35 +01:00
|
|
|
<main class="container">
|
|
|
|
|
{% block content %}{% endblock %}
|
|
|
|
|
</main>
|
|
|
|
|
</body>
|
2025-08-13 14:49:23 +01:00
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
const wsScheme = location.protocol === 'https:' ? 'wss' : 'ws';
|
|
|
|
|
const socket = new WebSocket(`${wsScheme}://${location.host}/ws`);
|
|
|
|
|
|
|
|
|
|
// One-time reload guard after successful sync
|
|
|
|
|
let reloadedAfterSync = false;
|
|
|
|
|
|
|
|
|
|
function maybeRefreshFromMessage(message) {
|
|
|
|
|
try {
|
|
|
|
|
const msg = String(message || '').toLowerCase();
|
|
|
|
|
if (!reloadedAfterSync && msg.includes('email synced successfully')) {
|
|
|
|
|
reloadedAfterSync = true;
|
|
|
|
|
// Give users a moment to read the success banner, then refresh
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
window.location.reload();
|
|
|
|
|
}, 800);
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Simple single-banner notification system
|
|
|
|
|
function pushNotification(message, level = 'info') {
|
|
|
|
|
try {
|
|
|
|
|
const root = document.getElementById('notify-root');
|
|
|
|
|
if (!root) return;
|
|
|
|
|
|
|
|
|
|
const levels = new Set(['info', 'success', 'warn', 'danger']);
|
|
|
|
|
const cls = levels.has(String(level)) ? String(level) : 'info';
|
|
|
|
|
|
|
|
|
|
// Ensure only a single banner is visible at a time
|
|
|
|
|
root.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
const item = document.createElement('div');
|
|
|
|
|
item.className = `notify ${cls}`;
|
|
|
|
|
item.setAttribute('role', 'status');
|
|
|
|
|
|
|
|
|
|
const content = document.createElement('div');
|
|
|
|
|
content.className = 'notify-content';
|
|
|
|
|
content.textContent = String(message ?? '');
|
|
|
|
|
|
|
|
|
|
const closeBtn = document.createElement('button');
|
|
|
|
|
closeBtn.className = 'notify-close';
|
|
|
|
|
closeBtn.setAttribute('aria-label', 'Close notification');
|
|
|
|
|
closeBtn.innerHTML = '×';
|
|
|
|
|
closeBtn.addEventListener('click', () => {
|
|
|
|
|
// Clear the banner on close
|
|
|
|
|
root.innerHTML = '';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
item.appendChild(content);
|
|
|
|
|
item.appendChild(closeBtn);
|
|
|
|
|
root.appendChild(item);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Failed to render notification:', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.onmessage = function(event) {
|
|
|
|
|
try {
|
|
|
|
|
let data = null;
|
|
|
|
|
try { data = JSON.parse(event.data); } catch { /* may be plain text */ }
|
|
|
|
|
|
|
|
|
|
if (data && typeof data === 'object') {
|
|
|
|
|
const msg = data.message ?? data.msg ?? data.text ?? JSON.stringify(data);
|
|
|
|
|
const level = (data.level || data.type || data.status || 'info').toString().toLowerCase();
|
|
|
|
|
pushNotification(msg, level);
|
|
|
|
|
console.log('Received sync status update:', msg);
|
|
|
|
|
maybeRefreshFromMessage(msg);
|
|
|
|
|
} else {
|
|
|
|
|
const text = String(event.data);
|
|
|
|
|
pushNotification(text);
|
|
|
|
|
console.log('Received sync status update:', text);
|
|
|
|
|
maybeRefreshFromMessage(text);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error handling websocket message:', err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.getElementById("processEmailsBtn").addEventListener("click", function () {
|
|
|
|
|
fetch("/sync_emails", { method: "POST" })
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
const status = data?.status ?? 'ok';
|
|
|
|
|
pushNotification(`Email sync triggered (${status})`, status === 'ok' ? 'success' : 'info');
|
|
|
|
|
console.log("Sync triggered:", status);
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
pushNotification(`Error starting sync: ${err?.message || err}`, 'danger');
|
|
|
|
|
console.error("Error starting sync:", err)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2025-08-11 14:46:35 +01:00
|
|
|
</html>
|