From ee872f6985b6ead7029447ab73f4858e52681a4c Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 28 Aug 2024 00:58:32 -0700 Subject: [PATCH] Select a mailbox on click, show its email IDs We'll eventually want to populate them. --- src/App.tsx | 11 +++++++++-- src/EmailList.tsx | 38 +++++++++++++++++++++++++++----------- src/MailboxList.tsx | 3 ++- src/client/Client.tsx | 42 +++++++++++++++++++++++++++++++++++++++--- src/client/types.tsx | 5 +++-- 5 files changed, 80 insertions(+), 19 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index abcaf8c..25ca753 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -59,7 +59,12 @@ class App extends React.Component { } onMailboxSelect(mailboxId: string) { - console.log("Mailbox", mailboxId); + if (!this.state.account) return; + this.client.emailList(this.state.account.id, mailboxId, []); + this.setState({ + ...this.state, + mailbox: this.client.mailbox(this.state.account.id, mailboxId), + }); } // Load up auth credentials from the local store @@ -113,7 +118,9 @@ class App extends React.Component { accounts={this.state.accounts} client={this.client} mailbox={this.state.mailbox} - onMailboxSelect={this.onMailboxSelect} + onMailboxSelect={(m) => { + this.onMailboxSelect(m); + }} /> { } render() { - return !(this.props.account && this.props.mailbox) ? ( - - ) : ( - - {this.props.mailbox.emails.map((m) => ( -
- {m.subject} -
- ))} -
- ); + if ( + this.props.account == null || + this.props.mailbox == null || + this.props.mailbox.emailIds == null + ) { + return ; + } else if (this.props.mailbox.emails == null) { + return ( + + {this.props.mailbox.emailIds.map((e) => ( +
+ Email {e} +
+ ))} +
+ ); + } else { + return ( + + {this.props.mailbox.emails.map((m) => ( +
+ {m.subject} +
+ ))} +
+ ); + } } } export default EmailList; diff --git a/src/MailboxList.tsx b/src/MailboxList.tsx index bbc8771..f42a241 100644 --- a/src/MailboxList.tsx +++ b/src/MailboxList.tsx @@ -25,7 +25,8 @@ class MailboxList extends React.Component { } render() { - return this.props.account == null ? ( + return this.props.account == null || + this.props.account.mailboxes == null ? ( ) : ( diff --git a/src/client/Client.tsx b/src/client/Client.tsx index 7bc3f97..d002d1c 100644 --- a/src/client/Client.tsx +++ b/src/client/Client.tsx @@ -65,10 +65,45 @@ export default class Client { return; } - emailList(accountId: string, mailboxId: string, ids: Array) {} + emailList(accountId: string, mailboxId: string, ids: Array) { + if (this.jclient == null) return; + /*this.jclient.email_get({ + accountId: accountId, + ids: [], + properties: ["threadId"] + });*/ + this.jclient + .email_query({ + accountId: accountId, + filter: { inMailbox: mailboxId }, + }) + .then((response) => { + const mailbox = this.mailbox(accountId, mailboxId); + if (mailbox == null) return; + mailbox.emailIds = response.ids; + this._triggerChange(); + }) + .catch(() => { + console.error("OH NOES"); + }); + } + mailbox(accountId: string, mailboxId: string): IMailbox | null { + if (this.state.session == null) return null; + const account = this.state.session.accounts[accountId]; + if (account.mailboxes == null) return null; + for (let i = 0; i < account.mailboxes.length; i++) { + if (account.mailboxes[i].id === mailboxId) { + return account.mailboxes[i]; + } + } + return null; + } mailboxList(accountId: string, ids: Array) { if (this.jclient == null) return; + if (this.state.session == null) return; + // We already populated the list + if (this.state.session.accounts[accountId].mailboxes != null) return; this.jclient .mailbox_get({ accountId: accountId, @@ -81,7 +116,8 @@ export default class Client { response.list.forEach((m) => { mailboxes.push({ ...m, - emails: [], + emailIds: null, + emails: null, }); }); account.mailboxes = mailboxes; @@ -106,7 +142,7 @@ export default class Client { accounts: Object.fromEntries( Object.entries(session.accounts).map(([key, account]) => [ key, - { ...account, id: key.toString(), mailboxes: [] }, + { ...account, id: key.toString(), mailboxes: null }, ]), ), }; diff --git a/src/client/types.tsx b/src/client/types.tsx index 45be0f7..a4a6b3f 100644 --- a/src/client/types.tsx +++ b/src/client/types.tsx @@ -1,12 +1,13 @@ import client from "jmap-client-ts/lib/types"; export interface IMailbox extends client.IMailboxProperties { - emails: Array; + emailIds: Array | null; + emails: Array | null; } export interface IAccount extends client.IAccount { id: string; - mailboxes: Array; + mailboxes: Array | null; } export type AccountIdMap = { [accountId: string]: IAccount };