Add multi account support and fixed functionality of buttons calendar etc
This commit is contained in:
+156
@@ -0,0 +1,156 @@
|
||||
let connectedMailAccount = null;
|
||||
let mailPageOffset = 0;
|
||||
let mailTotal = 0;
|
||||
|
||||
function updateAccountCard(account) {
|
||||
const card = $('#account-settings');
|
||||
if (!card) return;
|
||||
const [name, domain] = account.email.split('@');
|
||||
card.querySelector('.account-avatar').textContent = (name[0] || 'M').toUpperCase();
|
||||
card.querySelector('.account-info strong').textContent = account.email;
|
||||
card.querySelector('.account-info span').textContent = account.imapHost;
|
||||
card.title = 'Mail account settings';
|
||||
card.querySelector('.online-dot').style.background = '#70ad7b';
|
||||
}
|
||||
|
||||
function openMailAccount() {
|
||||
const root = $('#modal-root');
|
||||
const current = {};
|
||||
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal"><div class="modal-head"><span>Add a mailbox</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields"><p class="account-note">Your password is encrypted with this computer’s credential storage. Connections require TLS.</p><label class="compose-line"><span>Email</span><input id="mail-account-email" type="email" autocomplete="username" value="" placeholder="name@example.com"></label><label class="compose-line"><span>Password</span><input id="mail-account-password" type="password" autocomplete="current-password" placeholder="Enter mailbox password"></label><label class="compose-line"><span>IMAP host</span><input id="mail-account-imap" value="" autocomplete="off" placeholder="imap.example.com"></label><label class="compose-line"><span>SMTP host</span><input id="mail-account-smtp" value="" autocomplete="off" placeholder="smtp.example.com"></label><p class="account-note">IMAP uses STARTTLS on port 143 · SMTP uses STARTTLS on port 587</p><p id="mail-account-status" class="account-status" aria-live="polite"></p></div><div class="compose-footer"><button class="discard" id="disconnect-mail">Cancel</button><button class="send-button" id="connect-mail">Test & connect</button></div></div></div>`;
|
||||
const close = () => { root.innerHTML = ''; };
|
||||
$$('.modal-close').forEach(button => button.onclick = close);
|
||||
$('.modal-overlay').addEventListener('click', event => { if (event.target.classList.contains('modal-overlay')) close(); });
|
||||
$('#disconnect-mail').onclick = async () => {
|
||||
close();
|
||||
};
|
||||
$('#connect-mail').onclick = async () => {
|
||||
const button = $('#connect-mail');
|
||||
const status = $('#mail-account-status');
|
||||
const password = $('#mail-account-password').value;
|
||||
if (!password) { status.textContent = 'Enter the mailbox password to connect.'; $('#mail-account-password').focus(); return; }
|
||||
button.disabled = true;
|
||||
button.textContent = 'Checking secure connections…';
|
||||
status.textContent = 'Checking IMAP and SMTP sign-in. This may take a moment.';
|
||||
try {
|
||||
const account = await window.daybreakMail.connect({
|
||||
email: $('#mail-account-email').value,
|
||||
password,
|
||||
imapHost: $('#mail-account-imap').value,
|
||||
smtpHost: $('#mail-account-smtp').value
|
||||
});
|
||||
connectedMailAccount = account;
|
||||
updateAccountCard(account);
|
||||
close();
|
||||
toast('Mailbox connected securely');
|
||||
await syncMailbox(true);
|
||||
} catch (error) {
|
||||
status.textContent = error.message || 'Could not connect. Check the account details and try again.';
|
||||
button.disabled = false;
|
||||
button.textContent = 'Test & connect';
|
||||
}
|
||||
};
|
||||
$('#mail-account-email').focus();
|
||||
}
|
||||
|
||||
async function syncMailbox(showToast, options = {}) {
|
||||
if (!connectedMailAccount || !window.daybreakMail) {
|
||||
openMailAccount();
|
||||
return;
|
||||
}
|
||||
const refresh = $('#refresh');
|
||||
if (refresh) refresh.disabled = true;
|
||||
try {
|
||||
const offset = options.offset ?? (options.append ? mailPageOffset + messages.length : mailPageOffset);
|
||||
const result = await window.daybreakMail.syncFolder(activeFolder, offset);
|
||||
if (options.append) messages.push(...result.messages);
|
||||
else messages.splice(0, messages.length, ...result.messages);
|
||||
messages.forEach(message => {
|
||||
try {
|
||||
const localTags = JSON.parse(localStorage.getItem(`daybreak-message-labels-${message.id}`) || '[]');
|
||||
message.tags = [...new Set([...(message.tags || []), ...localTags])];
|
||||
} catch {}
|
||||
});
|
||||
if (!options.append) mailPageOffset = offset;
|
||||
mailTotal = result.total;
|
||||
activeMessage = messages[0]?.id;
|
||||
selectedIds.clear();
|
||||
const unread = messages.filter(message => message.unread).length;
|
||||
const inboxCount = $('.folder[data-folder="Inbox"] .count');
|
||||
if (inboxCount) inboxCount.textContent = String(unread);
|
||||
const pagination = $('#pagination');
|
||||
if (pagination) pagination.textContent = result.total ? `${options.append ? 1 : result.start}–${options.append ? Math.min(offset + result.messages.length, result.total) : result.end} of ${result.total}` : '0 messages';
|
||||
const loadMore = $('#load-more');
|
||||
if (loadMore) loadMore.classList.toggle('hidden', mailPageOffset + messages.length >= result.total);
|
||||
renderList();
|
||||
if (activeMessage) {
|
||||
openMessage(activeMessage);
|
||||
const current = messages.find(message => message.id === activeMessage);
|
||||
if (current?.uid) window.daybreakMail.updateMessage({ uid: current.uid, folder: activeFolder, action: 'unread', enabled: false }).catch(() => {});
|
||||
}
|
||||
else $('#reader-pane').innerHTML = emptyReader();
|
||||
updateAccountCard(connectedMailAccount);
|
||||
if (showToast) toast(`Synced ${messages.length} messages`);
|
||||
} catch (error) {
|
||||
toast(error.message || 'Could not sync the inbox');
|
||||
} finally {
|
||||
if (refresh) refresh.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (window.daybreakMail) {
|
||||
$('#account-settings').addEventListener('click', openMailAccount);
|
||||
$('#account-settings').addEventListener('keydown', event => {
|
||||
if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); openMailAccount(); }
|
||||
});
|
||||
$('#settings-button').onclick = () => openPreferences();
|
||||
$('#refresh').onclick = () => syncMailbox(true);
|
||||
$$('.folder').forEach(button => button.addEventListener('click', () => {
|
||||
if (['Inbox', 'Sent', 'Archive', 'Trash', 'Starred'].includes(button.dataset.folder)) {
|
||||
mailPageOffset = 0;
|
||||
syncMailbox(false, { offset: 0 });
|
||||
}
|
||||
}));
|
||||
$('#previous-page').onclick = () => {
|
||||
if (mailPageOffset <= 0) return;
|
||||
mailPageOffset = Math.max(0, mailPageOffset - 20);
|
||||
syncMailbox(false, { offset: mailPageOffset });
|
||||
};
|
||||
$('#next-page').onclick = () => {
|
||||
if (mailPageOffset + 20 >= mailTotal) return;
|
||||
mailPageOffset += 20;
|
||||
syncMailbox(false, { offset: mailPageOffset });
|
||||
};
|
||||
$('#load-more').onclick = () => syncMailbox(false, { append: true });
|
||||
document.addEventListener('click', async event => {
|
||||
const sendButton = event.target.closest('#send');
|
||||
if (!sendButton || !connectedMailAccount) return;
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
const recipient = $('#compose-to').value.trim();
|
||||
if (!recipient) { $('#compose-to').focus(); toast('Add a recipient to send your message'); return; }
|
||||
sendButton.disabled = true;
|
||||
sendButton.textContent = 'Sending…';
|
||||
try {
|
||||
const result = await window.daybreakMail.send({
|
||||
to: recipient,
|
||||
cc: $('#compose-cc').value,
|
||||
subject: $('#compose-subject').value.trim(),
|
||||
text: $('#compose-body').value
|
||||
});
|
||||
$('#modal-root').innerHTML = '';
|
||||
toast(result.savedToSent ? 'Message sent and saved in Sent' : 'Message sent, but its Sent copy could not be saved');
|
||||
if (activeFolder === 'Sent') syncMailbox(false, { offset: 0 });
|
||||
} catch (error) {
|
||||
sendButton.disabled = false;
|
||||
sendButton.textContent = 'Send ↗';
|
||||
toast(error.message || 'Message could not be sent');
|
||||
}
|
||||
}, true);
|
||||
|
||||
window.daybreakMail.getAccount().then(account => {
|
||||
if (!account) return;
|
||||
connectedMailAccount = account;
|
||||
updateAccountCard(account);
|
||||
syncMailbox(false);
|
||||
}).catch(() => {});
|
||||
}
|
||||
Reference in New Issue
Block a user