feat: enhance identity management with identity refresh functionality and improved modal behavior

This commit is contained in:
Linus Rath
2026-03-17 21:50:35 +01:00
parent 5c933a595f
commit 3186198fad
2 changed files with 49 additions and 17 deletions
+19 -4
View File
@@ -664,13 +664,24 @@ export function EmailComposer({
finalBody = body + '\n\n-- \n' + currentIdentity.textSignature;
}
// Build HTML body when replying/forwarding with original HTML content
// Build HTML signature block (prefer htmlSignature, fall back to escaped textSignature)
const buildSignatureHtml = (): string => {
if (currentIdentity?.htmlSignature) {
return `<br><br>-- <br>${sanitizeEmailHtml(currentIdentity.htmlSignature)}`;
}
if (currentIdentity?.textSignature) {
return `<br><br>-- <br>${currentIdentity.textSignature.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>')}`;
}
return '';
};
// Build HTML body
let finalHtmlBody: string | undefined;
const signatureHtml = buildSignatureHtml();
if (replyTo?.htmlBody && (mode === 'reply' || mode === 'replyAll' || mode === 'forward')) {
// Reply/forward with original HTML content
const escapedBody = body.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>');
const signatureHtml = currentIdentity?.textSignature
? `<br><br>-- <br>${currentIdentity.textSignature.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>')}`
: '';
const date = replyTo.receivedAt ? formatDateTime(replyTo.receivedAt, timeFormat, { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' }) : '';
const fromAddr = replyTo.from?.[0];
const fromStr = fromAddr ? `${fromAddr.name || fromAddr.email}` : tCommon('unknown');
@@ -679,6 +690,10 @@ export function EmailComposer({
: `On ${date}, ${fromStr} wrote:<br>`;
finalHtmlBody = `<div>${escapedBody}</div>${signatureHtml}<br><div><div>${quoteHeader}</div><blockquote style="margin:0 0 0 0.8ex;border-left:2px solid #ccc;padding-left:1ex">${replyTo.htmlBody}</blockquote></div>`;
} else if (signatureHtml) {
// New compose or plain-text reply — include HTML body with signature
const escapedBody = body.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>');
finalHtmlBody = `<div>${escapedBody}</div>${signatureHtml}`;
}
try {
+30 -13
View File
@@ -38,7 +38,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
const tNotif = useTranslations('notifications');
const client = useAuthStore((state) => state.client);
const { identities, addIdentity, updateIdentityLocal, removeIdentity } = useIdentityStore();
const identities = useIdentityStore((state) => state.identities);
const syncIdentities = useSyncIdentities();
const [editingId, setEditingId] = useState<string | null>(null);
@@ -46,6 +46,25 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
const [deletingId, setDeletingId] = useState<string | null>(null);
const { dialogProps: confirmDialogProps, confirm: confirmDialog } = useConfirmDialog();
// Re-fetch all identities from server and update stores
const refreshIdentities = useCallback(async () => {
if (!client) return;
try {
const serverIdentities = await client.getIdentities();
const username = useAuthStore.getState().username;
const sorted = [...serverIdentities].sort((a, b) => {
const aMatch = a.email === username ? -1 : 0;
const bMatch = b.email === username ? -1 : 0;
return aMatch - bMatch;
});
useIdentityStore.getState().setIdentities(sorted);
syncIdentities();
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to refresh identities';
toast.error(message);
}
}, [client, syncIdentities]);
// Focus trap with Escape handling
const modalRef = useFocusTrap({
isActive: isOpen,
@@ -60,9 +79,10 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
restoreFocus: true,
});
// Close on click outside
// Close on click outside (but not when ConfirmDialog is open)
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
if (confirmDialogProps.isOpen) return;
if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
onClose();
}
@@ -72,13 +92,13 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}
}, [isOpen, onClose, modalRef]);
}, [isOpen, onClose, modalRef, confirmDialogProps.isOpen]);
const handleCreate = useCallback(async (data: IdentityFormData) => {
if (!client) return;
try {
const newIdentity = await client.createIdentity(
await client.createIdentity(
data.name,
data.email,
data.replyTo,
@@ -87,8 +107,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
data.htmlSignature
);
addIdentity(newIdentity);
syncIdentities();
await refreshIdentities();
setIsCreating(false);
toast.success(tNotif('identity_created'));
} catch (error) {
@@ -96,7 +115,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
toast.error(tNotif('identity_create_failed', { error: message }));
throw error;
}
}, [client, addIdentity, t, tNotif]);
}, [client, refreshIdentities, t, tNotif]);
const handleUpdate = useCallback(async (identity: Identity, data: IdentityFormData) => {
if (!client) return;
@@ -110,8 +129,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
htmlSignature: data.htmlSignature,
});
updateIdentityLocal(identity.id, data);
syncIdentities();
await refreshIdentities();
setEditingId(null);
toast.success(tNotif('identity_updated'));
} catch (error) {
@@ -119,7 +137,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
toast.error(tNotif('identity_update_failed', { error: message }));
throw error;
}
}, [client, updateIdentityLocal, t, tNotif]);
}, [client, refreshIdentities, t, tNotif]);
const handleDelete = useCallback(async (identity: Identity) => {
if (!client) return;
@@ -140,8 +158,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
try {
await client.deleteIdentity(identity.id);
removeIdentity(identity.id);
syncIdentities();
await refreshIdentities();
toast.success(tNotif('identity_deleted'));
} catch (error) {
const message = error instanceof Error ? error.message : t('validation_errors.unknown_error');
@@ -149,7 +166,7 @@ export function IdentityManagerModal({ isOpen, onClose }: IdentityManagerModalPr
} finally {
setDeletingId(null);
}
}, [client, removeIdentity, t, tNotif, confirmDialog]);
}, [client, refreshIdentities, t, tNotif, confirmDialog]);
if (!isOpen) return null;