feat: Add runtime config, trusted senders, JMAP identities, and UI improvements

- Runtime environment variables for Docker-friendly configuration
- Trusted senders list for automatic image loading
- JMAP identities for proper sender address
- Improved email composer readability
- Horizontal scroll for wide HTML emails
This commit is contained in:
Matthieu MALVACHE
2026-01-08 02:08:18 +01:00
committed by Matthieu MALVACHE
parent dbffaf2a15
commit 58cfe09dc6
18 changed files with 778 additions and 100 deletions
+58 -14
View File
@@ -16,6 +16,8 @@ interface EmailComposerProps {
subject: string;
body: string;
draftId?: string;
fromEmail?: string;
identityId?: string;
}) => void;
onClose?: () => void;
onDiscardDraft?: (draftId: string) => void;
@@ -97,8 +99,9 @@ export function EmailComposer({
const lastSavedDataRef = useRef<string>("");
const [attachments, setAttachments] = useState<Array<{ file: File; blobId?: string; uploading?: boolean; error?: boolean }>>([]);
const fileInputRef = useRef<HTMLInputElement>(null);
const [selectedIdentityId, setSelectedIdentityId] = useState<string | null>(null);
const { client } = useAuthStore();
const { client, identities, primaryIdentity } = useAuthStore();
// Handle file selection
const handleFileSelect = async (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -255,6 +258,11 @@ export function EmailComposer({
}
}
// Get the selected identity or primary identity
const currentIdentity = selectedIdentityId
? identities.find(id => id.id === selectedIdentityId)
: primaryIdentity;
onSend?.({
to: toAddresses,
cc: ccAddresses,
@@ -262,6 +270,8 @@ export function EmailComposer({
subject,
body,
draftId: finalDraftId || undefined,
fromEmail: currentIdentity?.email,
identityId: currentIdentity?.id,
});
// Reset form
@@ -301,7 +311,7 @@ export function EmailComposer({
<div className={cn("flex flex-col h-full bg-background border rounded-lg", className)}>
<div className="flex items-center justify-between px-4 py-3 border-b">
<div className="flex items-center gap-2">
<h3 className="font-semibold">New Message</h3>
<h3 className="font-semibold">{t('new_message')}</h3>
{saveStatus === 'saving' && (
<div className="flex items-center gap-1 text-xs text-muted-foreground">
<Save className="w-3 h-3 animate-pulse" />
@@ -328,8 +338,32 @@ export function EmailComposer({
<div className="flex-1 flex flex-col">
<div className="space-y-2 px-4 py-3 border-b">
{/* From field - show dropdown if multiple identities, otherwise display email */}
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground w-16">To:</span>
<span className="text-sm text-muted-foreground w-16">{t('from')}:</span>
{identities.length > 1 ? (
<select
value={selectedIdentityId || primaryIdentity?.id || ''}
onChange={(e) => setSelectedIdentityId(e.target.value)}
className="flex-1 bg-transparent text-sm text-foreground outline-none cursor-pointer hover:text-muted-foreground transition-colors"
>
{identities.map((identity) => (
<option key={identity.id} value={identity.id}>
{identity.name ? `${identity.name} <${identity.email}>` : identity.email}
</option>
))}
</select>
) : (
<span className="text-sm text-foreground">
{primaryIdentity?.name
? `${primaryIdentity.name} <${primaryIdentity.email}>`
: primaryIdentity?.email || ''}
</span>
)}
</div>
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground w-16">{t('to')}:</span>
<Input
type="email"
placeholder="Recipient email addresses (comma separated)"
@@ -395,9 +429,9 @@ export function EmailComposer({
</div>
</div>
<div className="flex-1 px-4 py-3">
<div className="flex-1 px-4 py-3 min-h-0">
<textarea
className="w-full h-full resize-none outline-none text-sm"
className="w-full h-full resize-none outline-none text-sm bg-transparent text-foreground placeholder:text-muted-foreground"
placeholder="Compose email..."
value={body}
onChange={(e) => setBody(e.target.value)}
@@ -413,7 +447,7 @@ export function EmailComposer({
key={index}
className={cn(
"flex items-center gap-2 px-3 py-1 rounded-md text-sm",
att.error ? "bg-red-50 text-red-700" : "bg-gray-100 text-gray-700"
att.error ? "bg-red-500/10 text-red-600 dark:text-red-400" : "bg-muted text-foreground"
)}
>
{att.uploading ? (
@@ -424,12 +458,12 @@ export function EmailComposer({
<Paperclip className="w-3 h-3" />
)}
<span className="max-w-[200px] truncate">{att.file.name}</span>
<span className="text-xs text-gray-500">
<span className="text-xs text-muted-foreground">
({(att.file.size / 1024).toFixed(1)} KB)
</span>
<button
onClick={() => removeAttachment(index)}
className="ml-1 hover:text-red-600"
className="ml-1 hover:text-red-500"
>
<X className="w-3 h-3" />
</button>
@@ -440,7 +474,17 @@ export function EmailComposer({
)}
<div className="flex items-center justify-between px-4 py-3 border-t">
<div>
{/* Left side - Discard button */}
<button
type="button"
onClick={handleClose}
className="text-sm text-muted-foreground hover:text-red-500 transition-colors"
>
{t('discard')}
</button>
{/* Right side - Attach and Send */}
<div className="flex items-center gap-2">
<input
ref={fileInputRef}
type="file"
@@ -455,13 +499,13 @@ export function EmailComposer({
onClick={() => fileInputRef.current?.click()}
>
<Paperclip className="w-4 h-4 mr-2" />
Attach
{t('attach')}
</Button>
<Button onClick={handleSend}>
<Send className="w-4 h-4 mr-2" />
{t('send')}
</Button>
</div>
<Button onClick={handleSend}>
<Send className="w-4 h-4 mr-2" />
Send
</Button>
</div>
</div>
</div>
+43 -14
View File
@@ -134,6 +134,8 @@ export function EmailViewer({
const t = useTranslations('email_viewer');
const tNotifications = useTranslations('notifications');
const externalContentPolicy = useSettingsStore((state) => state.externalContentPolicy);
const addTrustedSender = useSettingsStore((state) => state.addTrustedSender);
const isSenderTrusted = useSettingsStore((state) => state.isSenderTrusted);
const [showFullHeaders, setShowFullHeaders] = useState(false);
const [allowExternalContent, setAllowExternalContent] = useState(false);
const [hasBlockedContent, setHasBlockedContent] = useState(false);
@@ -353,10 +355,16 @@ export function EmailViewer({
FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur'],
};
// Check if sender is trusted
const senderEmail = email.from?.[0]?.email?.toLowerCase();
const senderIsTrusted = senderEmail ? isSenderTrusted(senderEmail) : false;
// Block external content based on policy:
// 'allow' = never block, 'block' = always block, 'ask' = block until user allows
const shouldBlockExternal = externalContentPolicy === 'block' ||
(externalContentPolicy === 'ask' && !allowExternalContent);
// 'allow' = never block, 'block' = always block (unless trusted), 'ask' = block until user allows or trusted
const shouldBlockExternal = !senderIsTrusted && (
externalContentPolicy === 'block' ||
(externalContentPolicy === 'ask' && !allowExternalContent)
);
if (shouldBlockExternal) {
sanitizeConfig.FORBID_TAGS.push('link');
@@ -451,7 +459,7 @@ export function EmailViewer({
html: '<p style="color: #999;">No content available</p>',
isHtml: false
};
}, [email, allowExternalContent, hasBlockedContent, externalContentPolicy]);
}, [email, allowExternalContent, hasBlockedContent, externalContentPolicy, isSenderTrusted]);
// Show loading skeleton while email is being fetched
if (isLoading && !email) {
@@ -1094,17 +1102,38 @@ export function EmailViewer({
{/* Email Content Area */}
<div className="flex-1 overflow-auto bg-muted/30">
{/* Ultra Minimalist External Content Banner - only show in 'ask' mode */}
{hasBlockedContent && !allowExternalContent && externalContentPolicy === 'ask' && (
{/* External Content Banner - show in 'ask' or 'block' mode */}
{hasBlockedContent && !allowExternalContent && externalContentPolicy !== 'allow' && (
<div className="border-b border-border">
<div className="max-w-4xl mx-auto px-6 py-2">
<button
onClick={() => setAllowExternalContent(true)}
className="mx-auto flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<Image className="w-3.5 h-3.5" />
Show images
</button>
<div className="max-w-4xl mx-auto px-6 py-2 flex items-center justify-center gap-4">
{/* Load images button - only in 'ask' mode */}
{externalContentPolicy === 'ask' && (
<button
onClick={() => setAllowExternalContent(true)}
className="flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<Image className="w-3.5 h-3.5" />
{t('load_external_content')}
</button>
)}
{/* Trust sender button - in both 'ask' and 'block' modes */}
{email.from?.[0]?.email && (
<>
{externalContentPolicy === 'ask' && <span className="text-muted-foreground/50">|</span>}
<button
onClick={() => {
const senderEmail = email.from?.[0]?.email;
if (senderEmail) {
addTrustedSender(senderEmail);
setAllowExternalContent(true);
}
}}
className="flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors"
>
{t('trust_sender')}
</button>
</>
)}
</div>
</div>
)}
+52 -26
View File
@@ -75,6 +75,8 @@ export function ThreadConversationView({
}: ThreadConversationViewProps) {
const t = useTranslations();
const externalContentPolicy = useSettingsStore((state) => state.externalContentPolicy);
const addTrustedSender = useSettingsStore((state) => state.addTrustedSender);
const isSenderTrusted = useSettingsStore((state) => state.isSenderTrusted);
// Track which emails are expanded (most recent by default)
const [expandedIds, setExpandedIds] = useState<Set<string>>(new Set());
@@ -153,22 +155,30 @@ export function ThreadConversationView({
{/* Email Cards */}
<div className="flex-1 overflow-y-auto">
<div className="p-4 space-y-3">
{emails.map((email, index) => (
<EmailCard
key={email.id}
email={email}
isExpanded={expandedIds.has(email.id)}
isLatest={index === 0}
allowExternal={externalContentPolicy === 'allow' || allowExternalContent.has(email.id)}
onToggleExpanded={() => toggleExpanded(email.id)}
onAllowExternal={() => toggleAllowExternal(email.id)}
onReply={onReply ? () => onReply(email) : undefined}
onReplyAll={onReplyAll ? () => onReplyAll(email) : undefined}
onForward={onForward ? () => onForward(email) : undefined}
onDownloadAttachment={onDownloadAttachment}
onMarkAsRead={onMarkAsRead}
/>
))}
{emails.map((email, index) => {
const senderEmail = email.from?.[0]?.email?.toLowerCase();
const senderIsTrusted = senderEmail ? isSenderTrusted(senderEmail) : false;
return (
<EmailCard
key={email.id}
email={email}
isExpanded={expandedIds.has(email.id)}
isLatest={index === 0}
allowExternal={externalContentPolicy === 'allow' || senderIsTrusted || allowExternalContent.has(email.id)}
onToggleExpanded={() => toggleExpanded(email.id)}
onAllowExternal={() => toggleAllowExternal(email.id)}
onTrustSender={senderEmail ? () => {
addTrustedSender(senderEmail);
toggleAllowExternal(email.id);
} : undefined}
onReply={onReply ? () => onReply(email) : undefined}
onReplyAll={onReplyAll ? () => onReplyAll(email) : undefined}
onForward={onForward ? () => onForward(email) : undefined}
onDownloadAttachment={onDownloadAttachment}
onMarkAsRead={onMarkAsRead}
/>
);
})}
</div>
</div>
</div>
@@ -183,6 +193,7 @@ interface EmailCardProps {
allowExternal: boolean;
onToggleExpanded: () => void;
onAllowExternal: () => void;
onTrustSender?: () => void;
onReply?: () => void;
onReplyAll?: () => void;
onForward?: () => void;
@@ -197,6 +208,7 @@ function EmailCard({
allowExternal,
onToggleExpanded,
onAllowExternal,
onTrustSender,
onReply,
onReplyAll,
onForward,
@@ -382,16 +394,30 @@ function EmailCard({
<span className="text-muted-foreground">
{t("email_viewer.external_content_warning")}
</span>
<Button
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation();
onAllowExternal();
}}
>
{t("email_viewer.load_external_content")}
</Button>
<div className="flex items-center gap-2">
<Button
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation();
onAllowExternal();
}}
>
{t("email_viewer.load_external_content")}
</Button>
{onTrustSender && (
<Button
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation();
onTrustSender();
}}
>
{t("email_viewer.trust_sender")}
</Button>
)}
</div>
</div>
)}
+31
View File
@@ -1,20 +1,34 @@
"use client";
import { useState } from 'react';
import { useTranslations } from 'next-intl';
import { useSettingsStore } from '@/stores/settings-store';
import { SettingsSection, SettingItem, Select, ToggleSwitch } from './settings-section';
import { TrustedSendersModal } from '@/components/trusted-senders-modal';
import { ChevronRight } from 'lucide-react';
export function EmailSettings() {
const t = useTranslations('settings.email_behavior');
const [showTrustedModal, setShowTrustedModal] = useState(false);
const {
markAsReadDelay,
deleteAction,
showPreview,
emailsPerPage,
externalContentPolicy,
trustedSenders,
updateSetting,
} = useSettingsStore();
// Get count label for trusted senders button
const getTrustedSendersCount = () => {
const count = trustedSenders.length;
if (count === 0) return t('trusted_senders.count_zero');
if (count === 1) return t('trusted_senders.count_one');
return t('trusted_senders.count_other', { count });
};
return (
<SettingsSection title={t('title')} description={t('description')}>
{/* Mark as Read */}
@@ -75,6 +89,23 @@ export function EmailSettings() {
]}
/>
</SettingItem>
{/* Trusted Senders */}
<SettingItem label={t('trusted_senders.label')} description={t('trusted_senders.description')}>
<button
onClick={() => setShowTrustedModal(true)}
className="flex items-center gap-2 px-3 py-1.5 bg-muted hover:bg-accent rounded-md transition-colors"
>
<span className="text-sm text-foreground">{getTrustedSendersCount()}</span>
<ChevronRight className="w-4 h-4 text-muted-foreground" />
</button>
</SettingItem>
{/* Trusted Senders Modal */}
<TrustedSendersModal
isOpen={showTrustedModal}
onClose={() => setShowTrustedModal(false)}
/>
</SettingsSection>
);
}
+270
View File
@@ -0,0 +1,270 @@
"use client";
import { useState, useEffect, useRef, useMemo } from "react";
import { useTranslations } from "next-intl";
import { X, ShieldCheck, Search, Trash2, Plus } from "lucide-react";
import { Avatar } from "@/components/ui/avatar";
import { useSettingsStore } from "@/stores/settings-store";
import { cn } from "@/lib/utils";
interface TrustedSendersModalProps {
isOpen: boolean;
onClose: () => void;
}
export function TrustedSendersModal({ isOpen, onClose }: TrustedSendersModalProps) {
const t = useTranslations("settings.email_behavior.trusted_senders");
const modalRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const { trustedSenders, addTrustedSender, removeTrustedSender } = useSettingsStore();
const [searchQuery, setSearchQuery] = useState("");
const [isAdding, setIsAdding] = useState(false);
const [newEmail, setNewEmail] = useState("");
const [emailError, setEmailError] = useState("");
// Filter senders based on search query
const filteredSenders = useMemo(() => {
if (!searchQuery.trim()) return trustedSenders;
const query = searchQuery.toLowerCase();
return trustedSenders.filter((email) => email.toLowerCase().includes(query));
}, [trustedSenders, searchQuery]);
// Show search only when 5+ senders
const showSearch = trustedSenders.length >= 5;
// Close on Escape key
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") {
if (isAdding) {
setIsAdding(false);
setNewEmail("");
setEmailError("");
} else {
onClose();
}
}
};
if (isOpen) {
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}
}, [isOpen, isAdding, onClose]);
// Close on click outside
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
onClose();
}
};
if (isOpen) {
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}
}, [isOpen, onClose]);
// Focus input when adding mode is enabled
useEffect(() => {
if (isAdding && inputRef.current) {
inputRef.current.focus();
}
}, [isAdding]);
// Reset state when modal closes
useEffect(() => {
if (!isOpen) {
setSearchQuery("");
setIsAdding(false);
setNewEmail("");
setEmailError("");
}
}, [isOpen]);
const validateEmail = (email: string): boolean => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
const handleAddSender = () => {
const trimmedEmail = newEmail.trim().toLowerCase();
if (!trimmedEmail) {
setEmailError(t("invalid_email"));
return;
}
if (!validateEmail(trimmedEmail)) {
setEmailError(t("invalid_email"));
return;
}
if (trustedSenders.includes(trimmedEmail)) {
setEmailError(t("already_added"));
return;
}
addTrustedSender(trimmedEmail);
setNewEmail("");
setIsAdding(false);
setEmailError("");
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
handleAddSender();
}
};
if (!isOpen) return null;
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4 animate-in fade-in duration-150">
<div
ref={modalRef}
role="dialog"
aria-modal="true"
aria-labelledby="trusted-senders-title"
className={cn(
"bg-background border border-border rounded-lg shadow-xl",
"w-full max-w-md max-h-[60vh] overflow-hidden flex flex-col",
"animate-in zoom-in-95 duration-200"
)}
>
{/* Header */}
<div className="flex items-center justify-between px-6 py-4 border-b border-border flex-shrink-0">
<div className="flex items-center gap-3">
<ShieldCheck className="w-5 h-5 text-primary" />
<h2 id="trusted-senders-title" className="text-lg font-semibold text-foreground">
{t("modal_title")}
</h2>
</div>
<button
onClick={onClose}
aria-label={t("close")}
className="p-2 rounded-md hover:bg-muted transition-colors text-muted-foreground hover:text-foreground"
>
<X className="w-5 h-5" />
</button>
</div>
{/* Search (only when 5+ senders) */}
{showSearch && (
<div className="px-6 py-3 border-b border-border flex-shrink-0">
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
<input
type="text"
placeholder={t("search_placeholder")}
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="w-full pl-9 pr-3 py-2 text-sm bg-muted border border-border rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50"
/>
</div>
</div>
)}
{/* Content */}
<div className="flex-1 overflow-y-auto">
{trustedSenders.length === 0 ? (
/* Empty State */
<div className="flex flex-col items-center justify-center py-12 px-6 text-center">
<ShieldCheck className="w-12 h-12 text-muted-foreground/50 mb-4" />
<h3 className="text-base font-medium text-foreground mb-2">
{t("empty_title")}
</h3>
<p className="text-sm text-muted-foreground max-w-[280px] mb-6">
{t("empty_description")}
</p>
<button
onClick={() => setIsAdding(true)}
className="flex items-center gap-2 px-4 py-2 bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors text-sm font-medium"
>
<Plus className="w-4 h-4" />
{t("add_manually")}
</button>
</div>
) : filteredSenders.length === 0 ? (
/* No search results */
<div className="flex flex-col items-center justify-center py-12 px-6 text-center">
<Search className="w-10 h-10 text-muted-foreground/50 mb-3" />
<p className="text-sm text-muted-foreground">
{t("no_results")}
</p>
</div>
) : (
/* Sender list */
<div className="divide-y divide-border">
{filteredSenders.map((email) => (
<div
key={email}
className="flex items-center gap-3 px-6 py-3 hover:bg-muted/50 transition-colors group"
>
<Avatar email={email} size="sm" />
<span className="flex-1 text-sm text-foreground truncate">
{email}
</span>
<button
onClick={() => removeTrustedSender(email)}
className="p-1.5 rounded-md text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors opacity-0 group-hover:opacity-100 focus:opacity-100"
aria-label={`${t("remove")} ${email}`}
>
<Trash2 className="w-4 h-4" />
</button>
</div>
))}
</div>
)}
</div>
{/* Footer - Add sender */}
{trustedSenders.length > 0 && (
<div className="px-6 py-4 border-t border-border flex-shrink-0">
{isAdding ? (
<div className="space-y-2">
<div className="flex gap-2">
<input
ref={inputRef}
type="email"
placeholder={t("add_placeholder")}
value={newEmail}
onChange={(e) => {
setNewEmail(e.target.value);
setEmailError("");
}}
onKeyDown={handleKeyDown}
className={cn(
"flex-1 px-3 py-2 text-sm bg-background border rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50",
emailError ? "border-destructive" : "border-border"
)}
/>
<button
onClick={handleAddSender}
className="px-4 py-2 bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors text-sm font-medium"
>
{t("add_button")}
</button>
</div>
{emailError && (
<p className="text-xs text-destructive">{emailError}</p>
)}
</div>
) : (
<button
onClick={() => setIsAdding(true)}
className="flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<Plus className="w-4 h-4" />
{t("add_manually")}
</button>
)}
</div>
)}
</div>
</div>
);
}