Skip to content

Email for professionals and teams

10.6 KB
Raw Blame
"use client";

import { useEffect, useState } from "react";
import { formatUserDate } from "@/lib/time/utils";
import { ChevronsUpDown, Paperclip } from "@/components/icons";
import { ContactAvatar } from "@/components/contacts/contact-avatar";
import { QuotedEmailToggle } from "@/components/messages/quoted-email-toggle";
import { runSingleMessageAction } from "@/components/message-actions/utils";
import { EmailHtmlRenderer } from "./email-html-renderer";
import { useRemoteContent } from "./use-remote-content";
import { getMessageBodyDisplay, resolveInlineAttachmentUrls } from "@/app/(dashboard)/inbox/[messageId]/utils";

import { useLanguage } from "@/components/language-provider";
import { cn } from "@/lib/utils";
import type { ConversationMessageCardProps, ConversationThreadProps } from "./conversation-thread-types";
import { ThreadMessageActions } from "./thread-message-actions";
import { MessageDetailsPanel } from "./message-details-panel";
import { SenderCheckBadge, SenderCheckNotice } from "./sender-check-notice";
import { MessageOpenStatus } from "./message-open-status";
import { MessageFollowUpStatus } from "@/components/follow-ups/message-follow-up-status";
import {
	getConversationRecipients,
	getConversationSender,
	getConversationSenderEmail,
	partitionThread,
} from "./conversation-thread-utils";
import clsx from "clsx";
import { useMessageListVisibility } from "./message-list-visibility";
import { useAssistantOpen } from "../agent/assistant-open-state";

/**
 * The other messages in a conversation, ordered oldest to newest and collapsed
 * until opened. Readers can also expand the full visible portion at once.
 */
export function ConversationThread({
	currentMessageId,
	position,
	messages,
	mailboxId,
	currentAccountName,
	ownAddress,
	ownAddresses,
	latestMessagesFirst,
	expandedAll,
	onExpandedAllChange,
	showFullRecipientAddresses = false,
}: ConversationThreadProps) {
	const { t } = useLanguage();
	const slice = partitionThread(messages, currentMessageId, position, latestMessagesFirst);
	if (slice.length === 0) return null;
	const firstMessage = slice[0];
	const lastMessage = slice.at(-1)!;
	const middleMessages = slice.slice(1, -1);
	const collapsed = !expandedAll && middleMessages.length > 0;
	const collapsedLabel = t(position === "before" ? "thread.messagesCollapsed.older" : "thread.messagesCollapsed.newer", { count: middleMessages.length });

	return (
		<section
			aria-label={position === "before" ? t("thread.earlier") : t("thread.later")}
			className={cn(position === (latestMessagesFirst ? "before" : "after") ? "pb-6" : "")}
		>
			<ol className={cn(!collapsed && "divide-y divide-neutral-200/50", latestMessagesFirst ? "border-t" : "border-b", "border-neutral-200")}>
				<li className={"border-t-0"}>
					<ConversationMessageCard
						message={firstMessage}
						mailboxId={mailboxId}
						currentAccountName={currentAccountName}
						ownAddress={ownAddress}
						ownAddresses={ownAddresses}
						showFullRecipientAddresses={showFullRecipientAddresses}
					/>
				</li>
				{collapsed ? (
					<li className="flex items-center justify-center gap-1 py-2 text-center border-y border-neutral-100 h-px my-4">
						<span className="bg-white px-6 flex flex-row items-center gap-2">
							<span className="text-sm font-medium text-neutral-600">{collapsedLabel}</span>
							<button
								type="button"
								onClick={() => onExpandedAllChange(true)}
								aria-label={t("thread.expand", { label: collapsedLabel })}
								title={t("thread.expand", { label: collapsedLabel })}
								className="inline-flex h-6 w-6 items-center justify-center rounded-full text-neutral-500 hover:bg-neutral-100 hover:text-neutral-900"
							>
								<ChevronsUpDown className="h-4 w-4" />
							</button>
						</span>
					</li>
				) : (
					middleMessages.map((message) => (
						<li key={message.id}>
							<ConversationMessageCard
								message={message}
								mailboxId={mailboxId}
								currentAccountName={currentAccountName}
								ownAddress={ownAddress}
								ownAddresses={ownAddresses}
								showFullRecipientAddresses={showFullRecipientAddresses}
							/>
						</li>
					))
				)}
				{lastMessage.id !== firstMessage.id && (
					<li>
						<ConversationMessageCard
							message={lastMessage}
							mailboxId={mailboxId}
							currentAccountName={currentAccountName}
							ownAddress={ownAddress}
							ownAddresses={ownAddresses}
							showFullRecipientAddresses={showFullRecipientAddresses}
						/>
					</li>
				)}
			</ol>
		</section>
	);
}

export function ConversationMessageCard({
	message,
	mailboxId,
	currentAccountName,
	ownAddress,
	ownAddresses,
	defaultExpanded = false,
	showFullRecipientAddresses = false,
}: ConversationMessageCardProps) {
	const { t } = useLanguage();
	const [locallyExpanded, setLocallyExpanded] = useState(defaultExpanded);
	const [locallyRead, setLocallyRead] = useState(message.read);
	const [detailsOpen, setDetailsOpen] = useState(false);
	const expanded = locallyExpanded;
	const sender = getConversationSender(message, currentAccountName);
	const senderEmail = getConversationSenderEmail(message);
	const remote = useRemoteContent(senderEmail, message.status === "spam");
	const recipients = getConversationRecipients(message, showFullRecipientAddresses ? "full" : "address");
	// const href = `${getMessageBackHref(message.direction, message.status)}/${message.id}`;
	const outbound = message.direction === "outbound";
	const attachments = message.attachments.filter((attachment) => attachment.disposition === "attachment");
	const { visible: messageListVisible } = useMessageListVisibility();
	const isAssistantOpen = useAssistantOpen();
	const isAnyPanelVisible = messageListVisible || isAssistantOpen;

	useEffect(() => setLocallyRead(message.read), [message.read]);

	let body: { html: string | null; text: string; quotedHtml: string | null } | null = null;
	if (expanded) {
		const display = getMessageBodyDisplay(message.textBody, message.htmlBody, message.snippet);
		body = {
			html: resolveInlineAttachmentUrls(display.htmlBody, message.id, message.attachments),
			text: display.latestContent,
			quotedHtml: resolveInlineAttachmentUrls(display.quotedHtml, message.id, message.attachments),
		};
	}


	return (
		<article className={cn("bg-white transition-colors px-3 sm:px-6", !expanded && "hover:bg-neutral-50", !isAnyPanelVisible && "sm:pl-12")}>
			<div className="w-full">
				<div className="flex w-full flex-wrap items-start gap-x-3 py-3 sm:pl-4">
					<button
						type="button"
						onClick={() => {
							const shouldExpand = !locallyExpanded;
							setLocallyExpanded(shouldExpand);
							if (!shouldExpand || locallyRead) return;
							setLocallyRead(true);
							void runSingleMessageAction(message.id, "read").catch(() => setLocallyRead(false));
						}}
						aria-expanded={expanded}
						className="flex min-w-0 flex-1 basis-40 items-center gap-3 text-left cursor-pointer"
					>
						<ContactAvatar
							mailboxId={mailboxId}
							address={message.fromAddr}
							name={sender}
							hasManagedAvatar={message.fromContactHasAvatar}
							managedAvatarUrl={outbound && mailboxId ? `/api/mailboxes/${mailboxId}/avatar` : undefined}
							className={expanded ? "mt-1" : ""}
						/>
						<span className="min-w-0 flex-1">
							<div className="flex flex-col">
								<span className={cn("truncate text-sm font-semibold mt-1", locallyRead || outbound ? "text-neutral-900" : "font-semibold text-neutral-900")}>
									{sender}
									{expanded && <span className="hidden sm:inline text-xs ml-1 opacity-50 font-normal">&lt;{senderEmail}&gt;</span>}
								</span>
								{expanded && recipients && <span className="text-xs font-normal text-neutral-500">{t("thread.to", { recipients })}</span>}
							</div>
							{!expanded && (
								<span className={clsx(!locallyRead ? "font-semibold" : "text-neutral-500", "block truncate text-[13px]")}>{message.snippet || t("thread.noPreview")}</span>
							)}
							{!expanded && <SenderCheckBadge message={message} />}
						</span>
					</button>
					<span className={clsx(!locallyRead ? "font-semibold" : "", "shrink-0 items-center gap-2 text-xs mr-2 mt-2", "hidden sm:flex")}>
						{attachments.length > 0 && <Paperclip className="h-3.5 w-3.5" aria-label={t("thread.attachmentCount", { count: attachments.length })} />}
						{formatUserDate(message.createdAt, { month: "short", day: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit" })}
					</span>
					<ThreadMessageActions
						message={message}
						mailboxId={mailboxId}
						ownAddress={ownAddress}
						ownAddresses={ownAddresses}
						starOnly={!expanded}
						detailsOpen={detailsOpen}
						onToggleDetails={() => setDetailsOpen((open) => !open)}
					/>
					<div className={clsx("sm:hidden flex w-full items-center gap-2 pl-12 text-xs", locallyRead ? "text-neutral-500" : "font-semibold text-neutral-700")}>
						{attachments.length > 0 && <Paperclip className="h-3.5 w-3.5" aria-label={t("thread.attachmentCount", { count: attachments.length })} />}
						<span>{formatUserDate(message.createdAt, { month: "short", day: "2-digit", year: "numeric" })}</span>
					</div>
				</div>
				{expanded && (
					<SenderCheckNotice
						message={message}
						detailsOpen={detailsOpen}
						onViewDetails={() => setDetailsOpen((open) => !open)}
						className="mb-3 sm:ml-16"
					/>
				)}
				{detailsOpen && (
					<div className="mb-3 sm:pl-16">
						<MessageDetailsPanel message={message} onClose={() => setDetailsOpen(false)} />
					</div>
				)}
				{expanded && body && (
					<div className="pb-4 pt-2 sm:pl-16">
						{outbound && <MessageOpenStatus messageId={message.id} />}
						{outbound && message.status !== "draft" && <MessageFollowUpStatus messageId={message.id} />}
						{body.html ? (
							<EmailHtmlRenderer html={body.html} autoAllowRemote={remote.autoAllow} onAllowRemote={remote.allowSender} />
						) : (
							<pre className="whitespace-pre-wrap font-sans text-sm text-neutral-900">{body.text}</pre>
						)}
						{body.quotedHtml && <QuotedEmailToggle html={body.quotedHtml} autoAllowRemote={remote.autoAllow} onAllowRemote={remote.allowSender} />}
						{attachments.length > 0 && (
							<ul className="mt-4 flex flex-wrap gap-2">
								{attachments.map((attachment) => (
									<li key={attachment.id}>
										<a
											href={`/api/messages/${message.id}/attachments/${attachment.id}`}
											className="inline-flex items-center gap-1.5 rounded-md border border-neutral-200 bg-white px-2.5 py-1 text-xs text-neutral-700 hover:bg-neutral-50"
										>
											<Paperclip className="h-3 w-3" />
											<span className="max-w-48 truncate">{attachment.filename}</span>
										</a>
									</li>
								))}
							</ul>
						)}
					</div>
				)}
			</div>
		</article>
	);
}