|
| 1 | +import { |
| 2 | + Notice, |
| 3 | + __experimentalHStack as HStack, |
| 4 | + __experimentalHeading as Heading, |
| 5 | + __experimentalItem as Item, |
| 6 | + __experimentalItemGroup as ItemGroup, |
| 7 | + __experimentalText as Text, |
| 8 | + __experimentalVStack as VStack, |
| 9 | +} from '@wordpress/components'; |
| 10 | +import type { Email } from 'postal-mime'; |
| 11 | +import { useState } from 'react'; |
| 12 | +import { |
| 13 | + getActiveClientInfo, |
| 14 | + useAppSelector, |
| 15 | +} from '../../../lib/state/redux/store'; |
| 16 | +import { formatAddress, getMailSubject } from './mail-display'; |
| 17 | +import { MailPreview } from './mail-preview'; |
| 18 | +import css from './style.module.css'; |
| 19 | + |
| 20 | +export function SiteMailPanel() { |
| 21 | + const emails = useAppSelector(getActiveClientInfo)?.emails ?? []; |
| 22 | + const [selectedEmail, setSelectedEmail] = useState<Email>(); |
| 23 | + const selectedMail = |
| 24 | + selectedEmail && emails.includes(selectedEmail) |
| 25 | + ? selectedEmail |
| 26 | + : emails[0]; |
| 27 | + |
| 28 | + if (!selectedMail) { |
| 29 | + return ( |
| 30 | + <section className={css.mailEmptyState} aria-label="Email"> |
| 31 | + <Notice |
| 32 | + className={css.mailEmptyStateNotice} |
| 33 | + status="info" |
| 34 | + isDismissible={false} |
| 35 | + > |
| 36 | + <VStack spacing={2}> |
| 37 | + <Heading level={2}>No emails yet</Heading> |
| 38 | + <Text> |
| 39 | + Emails sent by this Playground will appear here. |
| 40 | + </Text> |
| 41 | + </VStack> |
| 42 | + </Notice> |
| 43 | + </section> |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <section className={css.mailPanel} aria-label="Email"> |
| 49 | + <aside className={css.mailPanelList} aria-label="Sent emails"> |
| 50 | + <HStack className={css.mailPanelListHeader}> |
| 51 | + <Text as="h2" weight={600}> |
| 52 | + Sent |
| 53 | + </Text> |
| 54 | + <Text variant="muted">{emails.length}</Text> |
| 55 | + </HStack> |
| 56 | + <ItemGroup isSeparated isRounded={false} size="large"> |
| 57 | + {emails.map((message, index) => { |
| 58 | + const isSelected = message === selectedMail; |
| 59 | + const subject = getMailSubject(message); |
| 60 | + |
| 61 | + return ( |
| 62 | + <Item |
| 63 | + key={message.messageId || `${subject}-${index}`} |
| 64 | + onClick={() => setSelectedEmail(message)} |
| 65 | + aria-pressed={isSelected} |
| 66 | + className={css.mailPanelListItem} |
| 67 | + > |
| 68 | + <VStack spacing={1}> |
| 69 | + <Text weight={600} truncate> |
| 70 | + {subject} |
| 71 | + </Text> |
| 72 | + <HStack spacing={2}> |
| 73 | + <Text variant="muted" truncate> |
| 74 | + {message.from |
| 75 | + ? formatAddress(message.from) |
| 76 | + : 'Unknown sender'} |
| 77 | + </Text> |
| 78 | + {message.date && ( |
| 79 | + <Text |
| 80 | + as="time" |
| 81 | + dateTime={message.date} |
| 82 | + variant="muted" |
| 83 | + > |
| 84 | + {formatSentTime(message.date)} |
| 85 | + </Text> |
| 86 | + )} |
| 87 | + </HStack> |
| 88 | + </VStack> |
| 89 | + </Item> |
| 90 | + ); |
| 91 | + })} |
| 92 | + </ItemGroup> |
| 93 | + </aside> |
| 94 | + <MailPreview mail={selectedMail} /> |
| 95 | + </section> |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +function formatSentTime(date: string): string { |
| 100 | + const parsedDate = new Date(date); |
| 101 | + if (Number.isNaN(parsedDate.getTime())) { |
| 102 | + return date; |
| 103 | + } |
| 104 | + return new Intl.DateTimeFormat(undefined, { |
| 105 | + hour: 'numeric', |
| 106 | + minute: '2-digit', |
| 107 | + }).format(parsedDate); |
| 108 | +} |
0 commit comments