diff --git a/src/EmailList.tsx b/src/EmailList.tsx index 69ebd3d..2b3d62e 100644 --- a/src/EmailList.tsx +++ b/src/EmailList.tsx @@ -7,7 +7,7 @@ import EmailSummary from "./EmailSummary"; type EmailListProps = { account: IAccount | null; - client: Client | null; + client: Client; mailbox: IMailbox | null; }; @@ -26,10 +26,9 @@ class EmailList extends React.Component { render() { if ( - this.props.account == null || - this.props.client == null || - this.props.mailbox == null || - this.props.mailbox.emailIds == null + this.props.account === null || + this.props.mailbox === null || + this.props.mailbox.emailIds === null ) { return ; } else { @@ -37,12 +36,12 @@ class EmailList extends React.Component { {this.props.mailbox.emailIds.slice(0, 5).map((e) => ( ))} diff --git a/src/EmailSummary.tsx b/src/EmailSummary.tsx index 0e57c1e..a9514ea 100644 --- a/src/EmailSummary.tsx +++ b/src/EmailSummary.tsx @@ -1,15 +1,16 @@ -import React from "react"; import Placeholder from "react-bootstrap/Placeholder"; +import React from "react"; import Client from "./client/Client"; +import DateTime from "./components/DateTime"; import { IAccount, IEmailStub, IMailbox } from "./client/types"; type EmailSummaryProps = { - account: IAccount | null; - client: Client | null; + account: IAccount; + client: Client; emailId: string; emailStub: IEmailStub | null; - mailbox: IMailbox | null; + mailbox: IMailbox; }; type EmailSummaryState = {}; @@ -25,8 +26,6 @@ class EmailSummary extends React.Component< } ensureData() { - if (this.props.account == null) return; - if (this.props.client == null) return; this.props.client.ensureEmailStub( this.props.account.id, this.props.emailId, @@ -34,13 +33,6 @@ class EmailSummary extends React.Component< } render() { - if (this.props.account == null || this.props.mailbox == null) { - return ( -
- -
- ); - } const href = "#" + this.props.account.id + @@ -49,16 +41,19 @@ class EmailSummary extends React.Component< "/" + this.props.emailId; const stub = this.props.emailStub; + if (stub === null) { + return ; + } return (
- {stub != null - ? stub.receivedAt + - " - " + + + + {" - " + (stub.from == null ? "?" : stub.from[0].name) + " - " + - stub.subject - : this.props.emailId} + stub.subject} +
); diff --git a/src/client/Client.tsx b/src/client/Client.tsx index 08c1af9..2febff8 100644 --- a/src/client/Client.tsx +++ b/src/client/Client.tsx @@ -203,7 +203,11 @@ export default class Client { emailStub(emailId: string): IEmailStub | null { if (this.state.session == null) return null; - return this.state.session.emailStubs[emailId]; + const result = this.state.session.emailStubs[emailId]; + if (result === undefined) { + return null; + } + return result; } mailbox(accountId: string, mailboxId: string): IMailbox | null { diff --git a/src/components/DateTime.tsx b/src/components/DateTime.tsx new file mode 100644 index 0000000..38c2148 --- /dev/null +++ b/src/components/DateTime.tsx @@ -0,0 +1,19 @@ +import React from "react"; + +type DateTimeProps = { + d: string; +}; + +const DateTime: React.FC = ({ d }) => { + const datetime = Date.parse(d); + const now = Date.now(); + const diff = (now - datetime) / 1000; + if (diff < 30) return moments ago; + if (diff < 60) return {diff}s; + if (diff < 60 * 60) return {Math.round(diff / 60)}m; + if (diff < 60 * 60 * 48) return {Math.round(diff / (60 * 60))}h; + if (diff < 60 * 60 * 24 * 365) + return {Math.round(diff / (60 * 60 * 24))}d; + return {Math.round(diff / (60 * 60 * 24 * 365))}y; +}; +export default DateTime;