Email: persist bulk read state to provider

Co-authored-by: ghreprimand <203024559+ghreprimand@users.noreply.github.com>
This commit is contained in:
ghreprimand
2026-06-02 06:28:01 -05:00
committed by GitHub
parent 6c654fb0ef
commit 431b98525b
2 changed files with 53 additions and 6 deletions
+17 -6
View File
@@ -4640,6 +4640,7 @@ function _updateBulkBar() {
async function _bulkAction(action) {
const uids = Array.from(state._selectedUids);
if (uids.length === 0) return;
let failedReadSync = 0;
if (action === 'delete') {
const ok = await styledConfirm(
`Delete ${uids.length} selected email${uids.length === 1 ? '' : 's'}?`,
@@ -4655,11 +4656,19 @@ async function _bulkAction(action) {
} else if (action === 'delete') {
await fetch(`${API_BASE}/api/email/delete/${uid}?folder=${encodeURIComponent(state._libFolder)}${_acct()}`, { method: 'DELETE' });
} else if (action === 'read' || action === 'unread') {
// Local toggle for now (no backend endpoint yet)
const em = state._libEmails.find(e => e.uid === uid);
if (em) em.is_read = (action === 'read');
const endpoint = action === 'read' ? 'mark-read' : 'mark-unread';
const res = await fetch(`${API_BASE}/api/email/${endpoint}/${uid}?folder=${encodeURIComponent(state._libFolder)}${_acct()}`, { method: 'POST' });
let data = null;
try { data = await res.json(); } catch (_) {}
if (!res.ok || data?.success === false) {
throw new Error(data?.error || `HTTP ${res.status}`);
}
_syncEmailReadState(uid, action === 'read');
}
} catch (e) { console.error(`Failed to ${action} ${uid}:`, e); }
} catch (e) {
if (action === 'read' || action === 'unread') failedReadSync += 1;
console.error(`Failed to ${action} ${uid}:`, e);
}
}
if (action === 'archive' || action === 'delete') {
@@ -4671,8 +4680,10 @@ async function _bulkAction(action) {
state._selectMode = false;
_updateBulkBar();
_renderGrid();
// Sync the local mutation (delete/archive, or in-place read/unread
// flag flips on email objects) into the SWR cache so reopen doesn't
if (failedReadSync > 0) {
showToast(`Failed to update ${failedReadSync} email${failedReadSync === 1 ? '' : 's'}`);
}
// Sync successful local mutations into the SWR cache so reopen doesn't
// briefly show the pre-bulk state.
_libCacheWriteBack();
}