Back

Email Compose Card

An AI-powered email composition card with recipient validation, inline editing, and send functionality.

Category
CardReact
CSS
Tailwind

Manual

Create a file and paste the following code into it.

email-compose-card.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
"use client";

import {
	Button,
	Chip,
	Input,
	Modal,
	ModalBody,
	ModalContent,
	ScrollShadow,
	Textarea,
} from "@heroui/react";
import type React from "react";
import { useEffect, useRef, useState } from "react";
import { z } from "zod";
import {
	Cancel01Icon,
	HugeiconsIcon,
	Loading03Icon,
	Mail01Icon,
	PencilEdit01Icon,
	PlusSignIcon,
	Tick02Icon,
} from "@/components/icons";
import { cn } from "@/lib/cn";

// Email validation schema
const emailComposeSchema = z.object({
	to: z
		.array(z.string().email("Invalid email address"))
		.min(1, "At least one recipient is required"),
	subject: z
		.string()
		.min(1, "Subject is required")
		.max(200, "Subject must be under 200 characters"),
	body: z
		.string()
		.min(1, "Email body is required")
		.max(10000, "Email body must be under 10,000 characters"),
});

const emailValidationSchema = z.string().email("Invalid email address");

export interface EmailData {
	to: string[];
	subject: string;
	body: string;
	draft_id?: string;
	thread_id?: string;
	bcc?: string[];
	cc?: string[];
	is_html?: boolean;
}

export interface EmailComposeCardProps {
	emailData: EmailData;
	onSend?: (data: EmailData) => void;
	className?: string;
}

function RecipientSelectionModal({
	isOpen,
	onClose,
	onConfirm,
	suggestions,
	selectedEmails,
	setSelectedEmails,
	customEmailInput,
	setCustomEmailInput,
	customEmailError,
	setCustomEmailError,
	handleAddCustomEmail,
}: {
	isOpen: boolean;
	onClose: () => void;
	onConfirm: () => void;
	suggestions: string[];
	selectedEmails: string[];
	setSelectedEmails: React.Dispatch<React.SetStateAction<string[]>>;
	customEmailInput: string;
	setCustomEmailInput: React.Dispatch<React.SetStateAction<string>>;
	customEmailError: string;
	setCustomEmailError: React.Dispatch<React.SetStateAction<string>>;
	handleAddCustomEmail: () => void;
}) {
	const handleSuggestionToggle = (email: string) => {
		setSelectedEmails((prev) => {
			if (prev.includes(email)) {
				// Remove from selected
				return prev.filter((e) => e !== email);
			} else {
				// Add to selected
				return [...prev, email];
			}
		});
	};

	const handleCustomEmailKeyPress = (e: React.KeyboardEvent) => {
		if (e.key === "Enter") {
			e.preventDefault();
			handleAddCustomEmail();
		}
	};

	return (
		<Modal isOpen={isOpen} onOpenChange={onClose} size="sm">
			<ModalContent>
				<ModalBody>
					<div className="pt-2 text-sm font-medium text-zinc-900 dark:text-zinc-100">
						Email Suggestions
					</div>

					{/* Suggestions */}
					<div className="flex flex-wrap gap-2">
						{suggestions.map((email) => (
							<Chip
								key={email}
								size="sm"
								variant="flat"
								color={selectedEmails.includes(email) ? "primary" : "default"}
								className="cursor-pointer text-xs"
								onClick={() => handleSuggestionToggle(email)}
								endContent={
									selectedEmails.includes(email) ? (
										<HugeiconsIcon icon={Cancel01Icon} size={12} />
									) : null
								}
							>
								{email}
							</Chip>
						))}
					</div>

					<hr className="my-2 border-zinc-200 dark:border-zinc-700" />

					<div className="flex gap-2">
						<Input
							placeholder="Add email..."
							value={customEmailInput}
							onChange={(e) => {
								setCustomEmailInput(e.target.value);
								setCustomEmailError("");
							}}
							onKeyDown={handleCustomEmailKeyPress}
							size="sm"
							isInvalid={!!customEmailError}
							errorMessage={customEmailError}
						/>
						<Button
							size="sm"
							color="primary"
							onPress={handleAddCustomEmail}
							isIconOnly
						>
							<HugeiconsIcon icon={PlusSignIcon} size={16} />
						</Button>
					</div>

					{/* Action Buttons */}
					<div className="mt-4 flex justify-end gap-2">
						<Button variant="light" size="sm" onPress={onClose}>
							Cancel
						</Button>
						<Button
							color="primary"
							size="sm"
							onPress={onConfirm}
							isDisabled={selectedEmails.length === 0}
						>
							Done ({selectedEmails.length})
						</Button>
					</div>
				</ModalBody>
			</ModalContent>
		</Modal>
	);
}

export function EmailComposeCard({
	emailData,
	onSend,
	className,
}: EmailComposeCardProps) {
	const [isRecipientModalOpen, setIsRecipientModalOpen] = useState(false);
	const [isSending, setIsSending] = useState(false);

	// Inline editing states
	const [isEditingSubject, setIsEditingSubject] = useState(false);
	const [isEditingBody, setIsEditingBody] = useState(false);

	const [editData, setEditData] = useState<EmailData>(emailData);

	// Suggestions come from emailData.to - these are resolved email addresses from the agent
	const [suggestions, setSuggestions] = useState<string[]>(emailData.to || []);

	// Selected emails state
	const [selectedEmails, setSelectedEmails] = useState<string[]>(
		emailData.to || [],
	);

	// Custom email input state
	const [customEmailInput, setCustomEmailInput] = useState("");
	const [customEmailError, setCustomEmailError] = useState("");

	// Input refs for auto-focus
	const subjectInputRef = useRef<HTMLInputElement>(null);
	const bodyInputRef = useRef<HTMLTextAreaElement>(null);

	// Initialize with empty emails array - user must select recipients
	// If there's only one email, select it by default
	useEffect(() => {
		const suggestions = emailData.to || [];
		setEditData((prev) => ({ ...prev, to: [] }));
		setSuggestions(suggestions);

		// If there's exactly one email suggestion, select it by default
		if (suggestions.length === 1) setSelectedEmails([suggestions[0]]);
		else setSelectedEmails([]);
	}, [emailData.to]);

	// Auto-focus logic
	useEffect(() => {
		if (isEditingSubject && subjectInputRef.current) {
			subjectInputRef.current.focus();
		}
	}, [isEditingSubject]);

	useEffect(() => {
		if (isEditingBody && bodyInputRef.current) {
			bodyInputRef.current.focus();
		}
	}, [isEditingBody]);

	const validateForm = () => {
		try {
			emailComposeSchema.parse({
				to: selectedEmails,
				subject: editData.subject,
				body: editData.body,
			});

			return true;
		} catch (error) {
			if (error instanceof z.ZodError) {
				const newErrors: Record<string, string> = {};
				error.issues.forEach((err) => {
					if (err.path[0]) {
						newErrors[err.path[0].toString()] = err.message;
					}
				});
			}
			return false;
		}
	};

	const validateCustomEmail = (email: string): boolean => {
		try {
			emailValidationSchema.parse(email);
			setCustomEmailError("");
			return true;
		} catch (error) {
			if (error instanceof z.ZodError) {
				setCustomEmailError(error.issues[0]?.message || "Invalid email");
			}
			return false;
		}
	};

	const handleSend = async () => {
		if (selectedEmails.length === 0) {
			console.error("Please select at least one recipient");
			return;
		}

		if (!validateForm()) {
			console.error("Please fix the validation errors");
			return;
		}

		setIsSending(true);
		try {
			if (onSend) {
				await onSend({ ...editData, to: selectedEmails });
			}
		} catch (error) {
			console.error("Error sending email:", error);
		} finally {
			setIsSending(false);
		}
	};

	// Handle custom email addition
	const handleAddCustomEmail = () => {
		const trimmedEmail = customEmailInput.trim();

		if (!trimmedEmail) {
			setCustomEmailError("Please enter an email address");
			return;
		}

		if (!validateCustomEmail(trimmedEmail)) {
			return;
		}

		if (selectedEmails.includes(trimmedEmail)) {
			setCustomEmailError("Email already selected");
			return;
		}

		// Add to selected emails
		setSelectedEmails((prev) => [...prev, trimmedEmail]);

		// Add to suggestions if not already there
		if (!suggestions.includes(trimmedEmail)) {
			setSuggestions((prev) => [...prev, trimmedEmail]);
		}

		// Clear input
		setCustomEmailInput("");
		setCustomEmailError("");
	};

	const handleConfirmRecipients = () => {
		setEditData((prev) => ({ ...prev, to: selectedEmails }));
		setIsRecipientModalOpen(false);
	};

	return (
		<>
			{/* Main Email Card - Zinc Colors, Flat & Rounded, Light/Dark Mode */}
			<div
				className={cn(
					"w-full max-w-xl overflow-hidden rounded-3xl",
					"bg-zinc-100 dark:bg-zinc-900",
					className,
				)}
			>
				{/* Header with status chip */}
				<div className="flex items-center justify-between px-6 py-1">
					<div className="flex flex-row items-center gap-2 pt-3 pb-2 text-zinc-900 dark:text-zinc-100">
						<HugeiconsIcon icon={Mail01Icon} size={18} />
						<span className="text-sm font-medium">
							{emailData.draft_id ? "Email Draft" : "Compose Email"}
						</span>
						{emailData.thread_id && (
							<Chip size="sm" variant="flat" color="primary">
								Reply
							</Chip>
						)}
					</div>
				</div>
				<div className="flex flex-col gap-1 px-6">
					<div className="flex items-center gap-2 text-sm text-zinc-500 dark:text-zinc-400">
						<span>To:</span>
						<span className="flex w-full items-center justify-between font-medium text-zinc-900 dark:text-zinc-200">
							{selectedEmails.join(", ") || ""}
							<Button
								size="sm"
								onPress={() => setIsRecipientModalOpen(true)}
								variant={selectedEmails.length === 0 ? "flat" : "light"}
								isIconOnly={selectedEmails.length !== 0}
								className={cn(
									selectedEmails.length === 0
										? ""
										: "text-zinc-500 dark:text-zinc-400",
								)}
								endContent={
									selectedEmails.length === 0 ? (
										""
									) : (
										<HugeiconsIcon icon={PencilEdit01Icon} size={20} />
									)
								}
							>
								{selectedEmails.length === 0 ? "Add Recipients" : ``}
							</Button>
						</span>
					</div>
					<div className="my-1.5 h-px bg-zinc-200 dark:bg-zinc-800" />
					<div className="flex w-full items-center justify-between text-sm text-zinc-500 dark:text-zinc-400">
						<div className="flex items-center gap-2 w-full">
							<span className="flex-shrink-0">Subject:</span>
							{isEditingSubject ? (
								<Input
									ref={subjectInputRef}
									value={editData.subject}
									onChange={(e) =>
										setEditData({ ...editData, subject: e.target.value })
									}
									onBlur={() => setIsEditingSubject(false)}
									onKeyDown={(e) => {
										if (e.key === "Enter") setIsEditingSubject(false);
									}}
									size="sm"
									classNames={{
										input:
											"text-base font-medium text-zinc-900 dark:text-zinc-200",
										inputWrapper:
											"h-7 min-h-7 bg-transparent shadow-none border-none px-0",
									}}
								/>
							) : (
								<span className="font-medium text-zinc-900 dark:text-zinc-200 truncate flex-1">
									{editData.subject}
								</span>
							)}
						</div>

						<Button
							variant="light"
							size="sm"
							isIconOnly
							onPress={() => setIsEditingSubject(!isEditingSubject)}
							className="text-zinc-500 dark:text-zinc-400"
						>
							{isEditingSubject ? (
								<HugeiconsIcon icon={Tick02Icon} size={20} />
							) : (
								<HugeiconsIcon icon={PencilEdit01Icon} size={20} />
							)}
						</Button>
					</div>
					<div className="my-1.5 h-px bg-zinc-200 dark:bg-zinc-800" />

					<ScrollShadow className="relative z-[1] max-h-46 min-h-[150px] overflow-y-auto pb-5 text-sm leading-relaxed whitespace-pre-line text-zinc-800 dark:text-zinc-200">
						<div className="absolute top-0 right-0 z-[2] flex w-full justify-end">
							<Button
								variant="light"
								size="sm"
								isIconOnly
								onPress={() => setIsEditingBody(!isEditingBody)}
								className="text-zinc-500 dark:text-zinc-400"
							>
								{isEditingBody ? (
									<HugeiconsIcon icon={Tick02Icon} size={20} />
								) : (
									<HugeiconsIcon icon={PencilEdit01Icon} size={20} />
								)}
							</Button>
						</div>

						{isEditingBody ? (
							<Textarea
								ref={bodyInputRef}
								value={editData.body}
								onChange={(e) =>
									setEditData({ ...editData, body: e.target.value })
								}
								onBlur={() => setIsEditingBody(false)}
								minRows={6}
								classNames={{
									input:
										"text-sm text-zinc-900 dark:text-zinc-200 placeholder:text-zinc-400 dark:placeholder:text-zinc-500",
									inputWrapper: "bg-transparent shadow-none border-none p-0",
								}}
							/>
						) : (
							editData.body
						)}
					</ScrollShadow>
				</div>
				<div className="flex justify-end px-6 pb-5">
					<Button
						color="primary"
						onPress={handleSend}
						isLoading={isSending}
						isDisabled={selectedEmails.length === 0}
						radius="full"
						className="font-medium"
					>
						{isSending ? (
							<>
								<HugeiconsIcon
									icon={Loading03Icon}
									size={16}
									className="animate-spin"
								/>
								Sending...
							</>
						) : emailData.draft_id ? (
							"Send Draft"
						) : (
							"Send"
						)}
					</Button>
				</div>
			</div>

			<RecipientSelectionModal
				isOpen={isRecipientModalOpen}
				onClose={() => setIsRecipientModalOpen(false)}
				onConfirm={handleConfirmRecipients}
				suggestions={suggestions}
				selectedEmails={selectedEmails}
				setSelectedEmails={setSelectedEmails}
				customEmailInput={customEmailInput}
				setCustomEmailInput={setCustomEmailInput}
				customEmailError={customEmailError}
				setCustomEmailError={setCustomEmailError}
				handleAddCustomEmail={handleAddCustomEmail}
			/>
		</>
	);
}

Update the import paths to match your project setup.

Similar screens