"use client"; import { useState } from 'react'; import { useTranslations } from 'next-intl'; import { useEmailStore } from '@/stores/email-store'; import { useAuthStore } from '@/stores/auth-store'; import { SettingsSection, SettingItem, Select } from './settings-section'; import { Plus, Pencil, Trash2, Check, X, FolderPlus } from 'lucide-react'; import { cn } from '@/lib/utils'; const STANDARD_ROLES = ['inbox', 'drafts', 'sent', 'trash', 'junk', 'archive'] as const; export function FolderSettings() { const t = useTranslations('settings.folders'); const { client } = useAuthStore(); const { mailboxes, createMailbox, renameMailbox, deleteMailbox, setMailboxRole } = useEmailStore(); const [isCreating, setIsCreating] = useState(false); const [newFolderName, setNewFolderName] = useState(''); const [editingId, setEditingId] = useState(null); const [editingName, setEditingName] = useState(''); const [deletingId, setDeletingId] = useState(null); const [isLoading, setIsLoading] = useState(false); // Only show own (non-shared) mailboxes const ownMailboxes = mailboxes.filter(mb => !mb.isShared); const getRoleMailboxId = (role: string): string => { const mb = ownMailboxes.find(m => m.role === role); return mb?.id ?? ''; }; const handleCreate = async () => { if (!client || !newFolderName.trim()) return; setIsLoading(true); try { await createMailbox(client, newFolderName.trim()); setNewFolderName(''); setIsCreating(false); } catch { // error is set in the store } finally { setIsLoading(false); } }; const handleRename = async (mailboxId: string) => { if (!client || !editingName.trim()) return; setIsLoading(true); try { await renameMailbox(client, mailboxId, editingName.trim()); setEditingId(null); setEditingName(''); } catch { // error is set in the store } finally { setIsLoading(false); } }; const handleDelete = async (mailboxId: string) => { if (!client) return; setIsLoading(true); try { await deleteMailbox(client, mailboxId); setDeletingId(null); } catch { // error is set in the store } finally { setIsLoading(false); } }; const handleRoleChange = async (role: string, mailboxId: string) => { if (!client) return; setIsLoading(true); try { if (mailboxId === '') { // Clear the role from whatever mailbox currently has it const current = ownMailboxes.find(m => m.role === role); if (current) { await setMailboxRole(client, current.id, null); } } else { await setMailboxRole(client, mailboxId, role); } } catch { // error is set in the store } finally { setIsLoading(false); } }; const startEdit = (mb: { id: string; name: string }) => { setEditingId(mb.id); setEditingName(mb.name); }; const cancelEdit = () => { setEditingId(null); setEditingName(''); }; return (
{/* Standard Folder Roles */} {STANDARD_ROLES.map((role) => ( setEditingName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') handleRename(mb.id); if (e.key === 'Escape') cancelEdit(); }} className="flex-1 px-2 py-1 text-sm rounded border border-border bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-ring" autoFocus disabled={isLoading} />
) : deletingId === mb.id ? (

{t('confirm_delete', { name: mb.name })}

) : ( <>
{mb.name} {mb.role && ( {mb.role} )}
{mb.myRights?.mayRename && ( )} {mb.myRights?.mayDelete && !mb.role && ( )}
)} ))} {/* Create folder */} {isCreating ? (
setNewFolderName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') handleCreate(); if (e.key === 'Escape') { setIsCreating(false); setNewFolderName(''); } }} placeholder={t('new_folder_name')} className="flex-1 px-2 py-1 text-sm rounded border border-border bg-background text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring" autoFocus disabled={isLoading} />
) : ( )} ); }