feat: improve email selection logic after deletion and archiving
This commit is contained in:
@@ -456,7 +456,6 @@ export default function Home() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await deleteEmail(client, selectedEmail.id);
|
await deleteEmail(client, selectedEmail.id);
|
||||||
selectEmail(null);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to delete email:", error);
|
console.error("Failed to delete email:", error);
|
||||||
}
|
}
|
||||||
@@ -470,7 +469,6 @@ export default function Home() {
|
|||||||
if (archiveMailbox) {
|
if (archiveMailbox) {
|
||||||
try {
|
try {
|
||||||
await moveToMailbox(client, selectedEmail.id, archiveMailbox.id);
|
await moveToMailbox(client, selectedEmail.id, archiveMailbox.id);
|
||||||
selectEmail(null);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to archive email:", error);
|
console.error("Failed to archive email:", error);
|
||||||
}
|
}
|
||||||
@@ -526,9 +524,6 @@ export default function Home() {
|
|||||||
|
|
||||||
const toastInstance = (await import('sonner')).toast;
|
const toastInstance = (await import('sonner')).toast;
|
||||||
toastInstance.success(t('email_viewer.spam.toast_not_spam_success'));
|
toastInstance.success(t('email_viewer.spam.toast_not_spam_success'));
|
||||||
|
|
||||||
// Deselect email after moving it out of junk
|
|
||||||
selectEmail(null);
|
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
console.error("Failed to restore email:", _error);
|
console.error("Failed to restore email:", _error);
|
||||||
const toastInstance = (await import('sonner')).toast;
|
const toastInstance = (await import('sonner')).toast;
|
||||||
|
|||||||
+15
-9
@@ -105,6 +105,17 @@ interface EmailStore {
|
|||||||
loadMockData: () => void;
|
loadMockData: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper: compute the next email to select when removing one from the list
|
||||||
|
function getNextSelectedEmail(state: { emails: Email[]; selectedEmail: Email | null }, removedEmailId: string): Email | null {
|
||||||
|
if (state.selectedEmail?.id !== removedEmailId) return state.selectedEmail;
|
||||||
|
const idx = state.emails.findIndex(e => e.id === removedEmailId);
|
||||||
|
if (idx === -1) return null;
|
||||||
|
// Prefer next email, fall back to previous
|
||||||
|
if (idx < state.emails.length - 1) return state.emails[idx + 1];
|
||||||
|
if (idx > 0) return state.emails[idx - 1];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export const useEmailStore = create<EmailStore>((set, get) => ({
|
export const useEmailStore = create<EmailStore>((set, get) => ({
|
||||||
emails: [],
|
emails: [],
|
||||||
mailboxes: [],
|
mailboxes: [],
|
||||||
@@ -439,7 +450,7 @@ export const useEmailStore = create<EmailStore>((set, get) => ({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
emails: state.emails.filter(e => e.id !== emailId),
|
emails: state.emails.filter(e => e.id !== emailId),
|
||||||
selectedEmail: state.selectedEmail?.id === emailId ? null : state.selectedEmail,
|
selectedEmail: getNextSelectedEmail(state, emailId),
|
||||||
mailboxes: updatedMailboxes
|
mailboxes: updatedMailboxes
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -485,7 +496,7 @@ export const useEmailStore = create<EmailStore>((set, get) => ({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
emails: state.emails.filter(e => e.id !== emailId),
|
emails: state.emails.filter(e => e.id !== emailId),
|
||||||
selectedEmail: state.selectedEmail?.id === emailId ? null : state.selectedEmail,
|
selectedEmail: getNextSelectedEmail(state, emailId),
|
||||||
mailboxes: updatedMailboxes
|
mailboxes: updatedMailboxes
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -625,7 +636,7 @@ export const useEmailStore = create<EmailStore>((set, get) => ({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
emails: state.emails.filter(e => e.id !== emailId),
|
emails: state.emails.filter(e => e.id !== emailId),
|
||||||
selectedEmail: state.selectedEmail?.id === emailId ? null : state.selectedEmail,
|
selectedEmail: getNextSelectedEmail(state, emailId),
|
||||||
mailboxes: updatedMailboxes
|
mailboxes: updatedMailboxes
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -906,13 +917,8 @@ export const useEmailStore = create<EmailStore>((set, get) => ({
|
|||||||
|
|
||||||
set(state => ({
|
set(state => ({
|
||||||
emails: state.emails.filter(e => e.id !== emailId),
|
emails: state.emails.filter(e => e.id !== emailId),
|
||||||
selectedEmail: state.selectedEmail?.id === emailId ? null : state.selectedEmail,
|
selectedEmail: getNextSelectedEmail(state, emailId),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const currentIndex = emails.findIndex(e => e.id === emailId);
|
|
||||||
if (currentIndex >= 0 && currentIndex < emails.length - 1) {
|
|
||||||
set({ selectedEmail: emails[currentIndex + 1] });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to mark as spam:', error);
|
console.error('Failed to mark as spam:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user