courrier multi account restore compose

This commit is contained in:
alma 2025-04-27 19:45:43 +02:00
parent e4dbc6a5c9
commit e33be95516
4 changed files with 233 additions and 230 deletions

8
.env
View File

@ -77,6 +77,12 @@ IMAP_HOST=mail.infomaniak.com
IMAP_PORT=993
NEWS_API_URL="http://172.16.0.104:8000"
# Required Redis environment variables to add to .env
REDIS_URL=redis://:mySecretPassword@localhost:6379
REDIS_PASSWORD=mySecretPassword
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=mySecretPassword
# Add a secret key for encrypting sensitive data in Redis
REDIS_ENCRYPTION_KEY=your-random-32-char-encryption-key

View File

@ -105,28 +105,28 @@ export async function GET(request: Request) {
const accountsWithFolders = await Promise.all(accounts.map(async (account) => {
try {
// Connect to IMAP server for this specific account
const client = new ImapFlow({
const client = new ImapFlow({
host: account.host,
port: account.port,
secure: true,
auth: {
secure: true,
auth: {
user: account.email,
pass: account.password,
},
logger: false,
tls: {
rejectUnauthorized: false
}
});
await client.connect();
},
logger: false,
tls: {
rejectUnauthorized: false
}
});
await client.connect();
// Get folders for this account
const folders = await getMailboxes(client);
// Close connection
await client.logout();
const folders = await getMailboxes(client);
// Close connection
await client.logout();
// Add display_name and color from database
const metadata = await prisma.$queryRaw`
SELECT display_name, color
@ -143,7 +143,7 @@ export async function GET(request: Request) {
color: displayMetadata.color || "#0082c9",
folders
};
} catch (error) {
} catch (error) {
console.error(`Error fetching folders for account ${account.email}:`, error);
// Return fallback folders on error
return {

View File

@ -11,7 +11,7 @@ import {
MoreHorizontal, FolderOpen, X, Paperclip, MessageSquare, Copy, EyeOff,
AlertOctagon, Archive, RefreshCw, Menu
} from 'lucide-react';
import { Dialog, DialogContent } from '@/components/ui/dialog';
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import {
AlertDialog,
@ -143,13 +143,13 @@ export default function CourrierPage() {
const updated = [...prev];
if (updated.length > 1) {
// Only update folders, preserve other properties including ID
if (updated[1]) {
if (updated[1]) {
updated[1] = {
...updated[1],
folders: mailboxes
};
}
console.log('Updated accounts with new mailboxes:', updated);
}
console.log('Updated accounts with new mailboxes:', updated);
}
return updated;
});
@ -265,7 +265,7 @@ export default function CourrierPage() {
});
});
}
if (!isMounted) return;
if (data.authenticated) {
@ -309,11 +309,11 @@ export default function CourrierPage() {
// Update the loading account in place to maintain references
updatedAccounts[1] = {
id: account.id, // Use the real account ID
name: account.display_name || account.email,
email: account.email,
color: account.color || 'bg-blue-500',
name: account.display_name || account.email,
email: account.email,
color: account.color || 'bg-blue-500',
folders: accountFolders
};
};
console.log(`[DEBUG] Updated loading account to real account: ${account.email} with ID ${account.id}`);
} else {
// Add additional accounts as new entries
@ -346,16 +346,16 @@ export default function CourrierPage() {
});
}
} else if (data.email) {
// Fallback to single account if allAccounts is not available
console.log(`[DEBUG] Fallback to single account: ${data.email}`);
// Force include some hardcoded folders if none are present
const fallbackFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
// Prioritize mailboxes from IMAP if available
const folderList = (data.mailboxes && data.mailboxes.length > 0) ?
data.mailboxes : fallbackFolders;
// Fallback to single account if allAccounts is not available
console.log(`[DEBUG] Fallback to single account: ${data.email}`);
// Force include some hardcoded folders if none are present
const fallbackFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
// Prioritize mailboxes from IMAP if available
const folderList = (data.mailboxes && data.mailboxes.length > 0) ?
data.mailboxes : fallbackFolders;
// Update the loading account if it exists
if (updatedAccounts.length > 1) {
updatedAccounts[1] = {
@ -677,195 +677,195 @@ export default function CourrierPage() {
<Plus className="h-4 w-4" />
</Button>
</div>
{/* Display all accounts */}
<div className="mt-1">
{/* Form for adding a new account */}
{showAddAccountForm && (
<div className="mb-3 p-2 border border-gray-200 rounded-md bg-gray-50">
<h4 className="text-xs font-medium mb-2 text-gray-700">Add IMAP Account</h4>
<form onSubmit={async (e) => {
e.preventDefault();
setLoading(true);
{/* Form for adding a new account */}
{showAddAccountForm && (
<div className="mb-3 p-2 border border-gray-200 rounded-md bg-gray-50">
<h4 className="text-xs font-medium mb-2 text-gray-700">Add IMAP Account</h4>
<form onSubmit={async (e) => {
e.preventDefault();
setLoading(true);
const formData = new FormData(e.currentTarget);
// Pull values from form with proper type handling
const formValues = {
email: formData.get('email')?.toString() || '',
password: formData.get('password')?.toString() || '',
host: formData.get('host')?.toString() || '',
port: parseInt(formData.get('port')?.toString() || '993'),
secure: formData.get('secure') === 'on',
display_name: formData.get('display_name')?.toString() || '',
smtp_host: formData.get('smtp_host')?.toString() || '',
smtp_port: formData.get('smtp_port')?.toString() ?
parseInt(formData.get('smtp_port')?.toString() || '587') : undefined,
smtp_secure: formData.get('smtp_secure') === 'on'
};
// If display_name is empty, use email
if (!formValues.display_name) {
formValues.display_name = formValues.email;
}
try {
// First test the connection
const testResponse = await fetch('/api/courrier/test-connection', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: formValues.email,
password: formValues.password,
host: formValues.host,
port: formValues.port,
secure: formValues.secure
})
});
const testResult = await testResponse.json();
if (!testResponse.ok) {
throw new Error(testResult.error || 'Connection test failed');
}
console.log('Connection test successful:', testResult);
// If connection test is successful, save the account
const saveResponse = await fetch('/api/courrier/account', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formValues)
});
const saveResult = await saveResponse.json();
if (!saveResponse.ok) {
throw new Error(saveResult.error || 'Failed to add account');
}
// Update accounts list
const newAccountObj = {
id: `account-${Date.now()}`, // generate unique string ID
name: formValues.display_name,
email: formValues.email,
color: `bg-blue-500`, // Default color class
folders: testResult.details.sampleFolders || ['INBOX', 'Sent', 'Drafts', 'Trash'] // Use discovered folders or defaults
};
setAccounts(prev => [...prev, newAccountObj]);
setShowAddAccountForm(false);
toast({
title: "Account added successfully",
description: `Your email account ${formValues.email} has been added.`,
duration: 5000
});
} catch (error) {
console.error('Error adding account:', error);
toast({
title: "Failed to add account",
description: error instanceof Error ? error.message : 'Unknown error',
variant: "destructive",
duration: 5000
});
} finally {
setLoading(false);
}
}}>
<div className="space-y-2">
<Tabs defaultValue="imap" className="w-full">
<TabsList className="grid w-full grid-cols-2 h-7">
<TabsTrigger value="imap" className="text-xs">IMAP Settings</TabsTrigger>
<TabsTrigger value="smtp" className="text-xs">SMTP Settings</TabsTrigger>
</TabsList>
const formData = new FormData(e.currentTarget);
// Pull values from form with proper type handling
const formValues = {
email: formData.get('email')?.toString() || '',
password: formData.get('password')?.toString() || '',
host: formData.get('host')?.toString() || '',
port: parseInt(formData.get('port')?.toString() || '993'),
secure: formData.get('secure') === 'on',
display_name: formData.get('display_name')?.toString() || '',
smtp_host: formData.get('smtp_host')?.toString() || '',
smtp_port: formData.get('smtp_port')?.toString() ?
parseInt(formData.get('smtp_port')?.toString() || '587') : undefined,
smtp_secure: formData.get('smtp_secure') === 'on'
};
// If display_name is empty, use email
if (!formValues.display_name) {
formValues.display_name = formValues.email;
}
try {
// First test the connection
const testResponse = await fetch('/api/courrier/test-connection', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: formValues.email,
password: formValues.password,
host: formValues.host,
port: formValues.port,
secure: formValues.secure
})
});
const testResult = await testResponse.json();
if (!testResponse.ok) {
throw new Error(testResult.error || 'Connection test failed');
}
console.log('Connection test successful:', testResult);
// If connection test is successful, save the account
const saveResponse = await fetch('/api/courrier/account', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formValues)
});
const saveResult = await saveResponse.json();
if (!saveResponse.ok) {
throw new Error(saveResult.error || 'Failed to add account');
}
// Update accounts list
const newAccountObj = {
id: `account-${Date.now()}`, // generate unique string ID
name: formValues.display_name,
email: formValues.email,
color: `bg-blue-500`, // Default color class
folders: testResult.details.sampleFolders || ['INBOX', 'Sent', 'Drafts', 'Trash'] // Use discovered folders or defaults
};
setAccounts(prev => [...prev, newAccountObj]);
setShowAddAccountForm(false);
toast({
title: "Account added successfully",
description: `Your email account ${formValues.email} has been added.`,
duration: 5000
});
} catch (error) {
console.error('Error adding account:', error);
toast({
title: "Failed to add account",
description: error instanceof Error ? error.message : 'Unknown error',
variant: "destructive",
duration: 5000
});
} finally {
setLoading(false);
}
}}>
<div className="space-y-2">
<Tabs defaultValue="imap" className="w-full">
<TabsList className="grid w-full grid-cols-2 h-7">
<TabsTrigger value="imap" className="text-xs">IMAP Settings</TabsTrigger>
<TabsTrigger value="smtp" className="text-xs">SMTP Settings</TabsTrigger>
</TabsList>
<TabsContent value="imap" className="mt-2 space-y-2">
<div>
<Label htmlFor="email" className="text-xs">Email</Label>
<Input
<Input
id="email"
name="email"
name="email"
placeholder="email@example.com"
className="h-7 text-xs"
required
/>
required
/>
</div>
<div>
<Label htmlFor="password" className="text-xs">Password</Label>
<Input
<Input
id="password"
name="password"
name="password"
type="password"
placeholder="•••••••••"
className="h-7 text-xs"
required
/>
required
/>
</div>
<div>
<Label htmlFor="display_name" className="text-xs">Display Name</Label>
<Input
<Input
id="display_name"
name="display_name"
name="display_name"
placeholder="John Doe"
className="h-7 text-xs"
/>
/>
</div>
<div>
<Label htmlFor="host" className="text-xs">IMAP Server</Label>
<Input
<Input
id="host"
name="host"
name="host"
placeholder="imap.example.com"
className="h-7 text-xs"
required
/>
required
/>
</div>
<div className="flex gap-2">
<div className="flex-1">
<Label htmlFor="port" className="text-xs">Port</Label>
<Input
<Input
id="port"
name="port"
name="port"
placeholder="993"
className="h-7 text-xs"
defaultValue="993"
required
/>
defaultValue="993"
required
/>
</div>
<div className="flex items-end pb-1">
<div className="flex items-center space-x-1">
<Checkbox id="secure" name="secure" defaultChecked />
<Checkbox id="secure" name="secure" defaultChecked />
<Label htmlFor="secure" className="text-xs">SSL</Label>
</div>
</div>
</div>
</TabsContent>
</div>
</div>
</TabsContent>
<TabsContent value="smtp" className="mt-2 space-y-2">
<div>
<Label htmlFor="smtp_host" className="text-xs">SMTP Server</Label>
<Input
<Input
id="smtp_host"
name="smtp_host"
name="smtp_host"
placeholder="smtp.example.com"
className="h-7 text-xs"
/>
/>
</div>
<div className="flex gap-2">
<div className="flex-1">
<Label htmlFor="smtp_port" className="text-xs">Port</Label>
<Input
<Input
id="smtp_port"
name="smtp_port"
name="smtp_port"
placeholder="587"
className="h-7 text-xs"
defaultValue="587"
/>
</div>
defaultValue="587"
/>
</div>
<div className="flex items-end pb-1">
<div className="flex items-center space-x-1">
<Checkbox id="smtp_secure" name="smtp_secure" defaultChecked />
@ -875,10 +875,10 @@ export default function CourrierPage() {
</div>
<div className="text-xs text-gray-500 italic">
Note: SMTP settings are only needed for sending emails
</div>
</TabsContent>
</Tabs>
</div>
</TabsContent>
</Tabs>
<div className="flex gap-2 pt-2">
<Button
type="submit"
@ -888,20 +888,20 @@ export default function CourrierPage() {
{loading ? <Loader2 className="h-3 w-3 animate-spin mr-1" /> : null}
Test & Add
</Button>
<Button
type="button"
<Button
type="button"
variant="outline"
className="h-7 text-xs"
onClick={() => setShowAddAccountForm(false)}
>
Cancel
</Button>
</div>
</div>
</form>
className="h-7 text-xs"
onClick={() => setShowAddAccountForm(false)}
>
Cancel
</Button>
</div>
</div>
)}
</form>
</div>
)}
{accounts.map((account) => (
<div key={account.id} className="mb-1">
<Button
@ -909,7 +909,7 @@ export default function CourrierPage() {
className={`w-full justify-start text-xs ${selectedAccount?.id === account.id ? 'bg-gray-100' : ''}`}
onClick={() => {
setSelectedAccount(account);
setShowFolders(true);
setShowFolders(true);
// Auto-expand this account when selected
if (account.id !== 'all-accounts') {
setExpandedAccounts(prev => ({
@ -949,22 +949,22 @@ export default function CourrierPage() {
{expandedAccounts[account.id] && account.id !== 'all-accounts' && account.folders && (
<div className="pl-4">
{account.folders.map((folder) => (
<Button
key={folder}
variant="ghost"
<Button
key={folder}
variant="ghost"
className={`w-full justify-start text-xs py-1 h-7 ${currentFolder === folder ? 'bg-gray-100' : ''}`}
onClick={() => handleMailboxChange(folder, account.id !== 'all-accounts' ? account.id : undefined)}
>
<div className="flex items-center w-full">
{getFolderIcon(folder)}
<span className="ml-2 truncate text-gray-700">{formatFolderName(folder)}</span>
{folder === 'INBOX' && unreadCount > 0 && (
{folder === 'INBOX' && unreadCount > 0 && (
<span className="ml-auto bg-blue-500 text-white text-[10px] px-1.5 rounded-full">
{unreadCount}
</span>
)}
</div>
</Button>
{unreadCount}
</span>
)}
</div>
</Button>
))}
</div>
)}
@ -973,14 +973,14 @@ export default function CourrierPage() {
</div>
</div>
</div>
</div>
</div>
{/* Main Content Area - conditionally show email list or detail view */}
<div className="flex-1 flex flex-col overflow-hidden" style={{ maxWidth: 'calc(100% - 240px)' }}>
<div className="flex-1 flex flex-col overflow-hidden">
{/* Header bar with search */}
<div className="p-2 border-b border-gray-100 bg-white flex items-center justify-between">
<Button
variant="ghost"
<Button
variant="ghost"
size="icon"
className="md:hidden h-9 w-9"
onClick={() => setMobileSidebarOpen(!mobileSidebarOpen)}
@ -996,7 +996,7 @@ export default function CourrierPage() {
placeholder="Search emails..."
className="pl-8 h-9 bg-gray-50 border-gray-200"
/>
</div>
</div>
<div className="flex items-center">
{selectedEmailIds.length > 0 && (
@ -1024,8 +1024,8 @@ export default function CourrierPage() {
onClick={() => handleBulkAction('archive')}
>
<Archive className="h-4 w-4 text-gray-500" />
</Button>
</div>
</Button>
</div>
)}
{session?.user && (
@ -1036,8 +1036,8 @@ export default function CourrierPage() {
</Avatar>
)}
</div>
</div>
</div>
{/* Email List or Detail View */}
<div className="flex-1 overflow-hidden bg-white">
{isLoading ? (
@ -1072,11 +1072,7 @@ export default function CourrierPage() {
) : selectedEmail ? (
<EmailDetailView
email={selectedEmail}
onBack={() => {
handleEmailSelect('');
// Ensure sidebar stays visible
setSidebarOpen(true);
}}
onBack={() => handleEmailSelect('')}
onReply={handleReply}
onReplyAll={handleReplyAll}
onForward={handleForward}
@ -1084,7 +1080,7 @@ export default function CourrierPage() {
/>
) : (
<div className="h-full overflow-hidden flex flex-col">
{/* Email List */}
{/* Email List */}
<div className="flex-1 overflow-y-auto">
{emails.length === 0 ? (
<div className="h-full flex items-center justify-center">
@ -1099,15 +1095,15 @@ export default function CourrierPage() {
</div>
</div>
) : (
<EmailList
emails={emails}
selectedEmailIds={selectedEmailIds}
selectedEmail={selectedEmail}
onSelectEmail={handleEmailSelect}
onToggleSelect={toggleEmailSelection}
onToggleSelectAll={toggleSelectAll}
onToggleStarred={toggleStarred}
onLoadMore={handleLoadMore}
<EmailList
emails={emails}
selectedEmailIds={selectedEmailIds}
selectedEmail={selectedEmail}
onSelectEmail={handleEmailSelect}
onToggleSelect={toggleEmailSelection}
onToggleSelectAll={toggleSelectAll}
onToggleStarred={toggleStarred}
onLoadMore={handleLoadMore}
hasMoreEmails={page < totalPages}
currentFolder={currentFolder}
isLoading={isLoading}
@ -1140,13 +1136,14 @@ export default function CourrierPage() {
<Dialog open={showComposeModal} onOpenChange={setShowComposeModal}>
<DialogContent className="sm:max-w-[800px] h-[80vh] p-0 overflow-hidden">
<ComposeEmail
type={composeType}
<DialogTitle className="sr-only">Compose Email</DialogTitle>
<ComposeEmail
type={composeType}
initialEmail={composeType !== 'new' ? selectedEmail : undefined}
onSend={handleSendEmail}
onClose={() => setShowComposeModal(false)}
onClose={() => setShowComposeModal(false)}
isSending={isSending}
/>
/>
</DialogContent>
</Dialog>
</>

View File

@ -915,4 +915,4 @@ function LegacyAdapter({
</div>
</div>
);
}
}