Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update email format switcher logic and UI in browser extension (#150)

Leendert de Borst896e91285a80
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx b/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx
index f0eb0c43f..fab61511f 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx
@@ -1,6 +1,6 @@
-import React, { useEffect, useState, useCallback } from 'react';
+import React, { useEffect, useMemo, useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
-import { useParams, useNavigate } from 'react-router-dom';
+import { Link, useParams, useNavigate } from 'react-router-dom';
import Modal from '@/entrypoints/popup/components/Dialogs/Modal';
import LoadingSpinner from '@/entrypoints/popup/components/LoadingSpinner';
@@ -34,9 +34,9 @@ const EmailDetails: React.FC = (): React.ReactElement => {
const [showDeleteModal, setShowDeleteModal] = useState(false);
const [showMetadata, setShowMetadata] = useState(false);
const [viewMode, setViewMode] = useState<'html' | 'plain' | 'source'>('html');
+ const [credential, setCredential] = useState<{ id: string; name: string } | null>(null);
const { setIsInitialLoading } = useLoading();
const { setHeaderButtons } = useHeaderButtons();
- const [headerButtonsConfigured, setHeaderButtonsConfigured] = useState(false);
useEffect(() => {
// For popup windows, ensure we have proper history state for navigation
@@ -90,6 +90,54 @@ const EmailDetails: React.FC = (): React.ReactElement => {
loadEmail();
}, [id, dbContext?.sqliteClient, dbContext.isOffline, t, webApi, setIsLoading, setIsInitialLoading]);
+ /*
+ * Resolve the credential (item) that owns the recipient address for this email so we can
+ * surface a deep link to it from the metadata panel, mirroring the web app's behaviour.
+ */
+ useEffect(() => {
+ if (!email || !dbContext?.sqliteClient) {
+ setCredential(null);
+ return;
+ }
+
+ const address = `${email.toLocal}@${email.toDomain}`;
+ const match = dbContext.sqliteClient.items.findIdByEmail(address);
+ setCredential(match ? { id: match.Id, name: match.Name ?? address } : null);
+ }, [email, dbContext?.sqliteClient]);
+
+ // Available view modes for the cycle button — only formats the server actually provided.
+ const availableModes = useMemo<Array<'html' | 'plain' | 'source'>>(() => {
+ if (!email) {
+ return [];
+ }
+ const modes: Array<'html' | 'plain' | 'source'> = [];
+ if (email.messageHtml) {
+ modes.push('html');
+ }
+ if (email.messagePlain) {
+ modes.push('plain');
+ }
+ if (email.messageSource) {
+ modes.push('source');
+ }
+ return modes;
+ }, [email]);
+
+ const formatLabels = useMemo<Record<'html' | 'plain' | 'source', string>>(() => ({
+ html: t('emails.formatHtml'),
+ plain: t('emails.formatPlain'),
+ source: t('emails.formatSource'),
+ }), [t]);
+
+ const cycleViewMode = useCallback(() => {
+ if (availableModes.length <= 1) {
+ return;
+ }
+ const idx = availableModes.indexOf(viewMode);
+ const next = availableModes[(idx + 1) % availableModes.length];
+ setViewMode(next);
+ }, [availableModes, viewMode]);
+
/**
* Handle deleting an email.
*/
@@ -159,33 +207,41 @@ const EmailDetails: React.FC = (): React.ReactElement => {
}
};
- // Set header buttons on mount and clear on unmount
+ /*
+ * Set header buttons whenever the available formats or active view mode change so the
+ * format-cycle button label stays in sync. Mirrors the web app's header layout.
+ */
useEffect((): (() => void) => {
- // Only set the header buttons once on mount.
- if (!headerButtonsConfigured) {
- const headerButtonsJSX = (
- <div className="flex items-center gap-2">
- {!PopoutUtility.isPopup() && (
- <HeaderButton
- onClick={openInNewPopup}
- title={t('common.openInNewWindow')}
- iconType={HeaderIconType.EXPAND}
- />
- )}
+ const headerButtonsJSX = (
+ <div className="flex items-center gap-2">
+ {availableModes.length > 1 && (
+ <button
+ onClick={cycleViewMode}
+ title={t('emails.formatSwitchTitle')}
+ className="h-9 px-2 rounded-lg inline-flex items-center text-xs font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700"
+ >
+ {formatLabels[viewMode]}
+ </button>
+ )}
+ {!PopoutUtility.isPopup() && (
<HeaderButton
- onClick={() => setShowDeleteModal(true)}
- title={t('emails.deleteEmailTitle')}
- iconType={HeaderIconType.DELETE}
- variant="danger"
+ onClick={openInNewPopup}
+ title={t('common.openInNewWindow')}
+ iconType={HeaderIconType.EXPAND}
/>
- </div>
- );
+ )}
+ <HeaderButton
+ onClick={() => setShowDeleteModal(true)}
+ title={t('emails.deleteEmailTitle')}
+ iconType={HeaderIconType.DELETE}
+ variant="danger"
+ />
+ </div>
+ );
- setHeaderButtons(headerButtonsJSX);
- setHeaderButtonsConfigured(true);
- }
+ setHeaderButtons(headerButtonsJSX);
return () => {};
- }, [setHeaderButtons, headerButtonsConfigured, openInNewPopup, t]);
+ }, [setHeaderButtons, openInNewPopup, t, availableModes, viewMode, cycleViewMode, formatLabels]);
// Clear header buttons on unmount
useEffect((): (() => void) => {
@@ -227,16 +283,19 @@ const EmailDetails: React.FC = (): React.ReactElement => {
<div>
{/* Header */}
<div>
- <div className="flex justify-between items-start">
- <div className="flex items-center gap-2">
- <h1 className="text-lg font-bold text-gray-900 dark:text-white">{email.subject}</h1>
+ <div className="flex justify-between items-start gap-2">
+ <div className="flex items-start gap-2 min-w-0">
+ {/* Subject is truncated by default; expanding the (i) panel lets the full subject
+ wrap so users can read or copy long subjects without losing context. */}
+ <h1 className={`text-lg font-bold text-gray-900 dark:text-white ${showMetadata ? 'break-words' : 'truncate'}`}>{email.subject}</h1>
<button
onClick={() => setShowMetadata(!showMetadata)}
- className="p-1 rounded-full hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
+ className={`p-1 rounded-full transition-colors flex-shrink-0 ${showMetadata ? 'bg-gray-200 dark:bg-gray-700 text-primary-600 dark:text-primary-400' : 'text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700'}`}
title={showMetadata ? t('common.hideDetails') : t('common.showDetails')}
+ aria-expanded={showMetadata}
>
<svg
- className={`w-4 h-4 text-gray-500 dark:text-gray-400 transition-transform ${showMetadata ? 'rotate-180' : ''}`}
+ className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
@@ -256,36 +315,23 @@ const EmailDetails: React.FC = (): React.ReactElement => {
<p><span className="font-bold">{t('emails.from')}</span> <span title={email.fromLocal + "@" + email.fromDomain}>{email.fromDisplay}</span></p>
<p><span className="font-bold">{t('emails.to')}</span> <span title={email.toLocal + "@" + email.toDomain}>{email.toLocal}@{email.toDomain}</span></p>
<p><span className="font-bold">{t('emails.date')}</span> {new Date(email.dateSystem).toLocaleString()}</p>
+ {credential && (
+ <p>
+ <span className="font-bold">{t('emails.item')}</span>{' '}
+ <Link
+ to={`/items/${credential.id}`}
+ className="text-primary-600 hover:underline dark:text-primary-400"
+ >
+ {credential.name}
+ </Link>
+ </p>
+ )}
</div>
)}
</div>
- {/* Format Switcher */}
- <div className="flex gap-1 mt-4 mb-2">
- <button
- onClick={() => setViewMode('html')}
- className={viewMode === 'html' ? 'px-3 py-1 text-sm font-medium rounded-md bg-blue-500 text-white' : 'px-3 py-1 text-sm font-medium rounded-md text-gray-600 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700'}
- disabled={!email.messageHtml}
- >
- HTML
- </button>
- <button
- onClick={() => setViewMode('plain')}
- className={viewMode === 'plain' ? 'px-3 py-1 text-sm font-medium rounded-md bg-blue-500 text-white' : 'px-3 py-1 text-sm font-medium rounded-md text-gray-600 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700'}
- disabled={!email.messagePlain}
- >
- Plain
- </button>
- <button
- onClick={() => setViewMode('source')}
- className={viewMode === 'source' ? 'px-3 py-1 text-sm font-medium rounded-md bg-blue-500 text-white' : 'px-3 py-1 text-sm font-medium rounded-md text-gray-600 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700'}
- >
- Source
- </button>
- </div>
-
- {/* Email Body */}
- <div className="bg-white">
+ {/* Email Body — always rendered on a white background with dark text so contrast doesn't break in dark mode. */}
+ <div className="bg-white mt-4">
{viewMode === 'html' && email.messageHtml ? (
<iframe
srcDoc={ConversionUtility.sanitizeAndPrepareEmailHtml(email.messageHtml)}
@@ -294,15 +340,15 @@ const EmailDetails: React.FC = (): React.ReactElement => {
sandbox="allow-popups allow-popups-to-escape-sandbox"
/>
) : viewMode === 'plain' ? (
- <pre className="whitespace-pre-wrap text-gray-800 dark:text-gray-200 p-3 font-sans">
+ <pre className="whitespace-pre-wrap text-gray-800 p-3 font-sans">
{email.messagePlain ?? t('emails.emailNotFound')}
</pre>
) : viewMode === 'source' ? (
- <pre className="whitespace-pre-wrap text-gray-800 dark:text-gray-200 p-3 font-mono text-xs leading-relaxed">
- {email.messageHtml ?? t('emails.emailNotFound')}
+ <pre className="whitespace-pre-wrap text-gray-800 p-3 font-mono text-xs leading-relaxed">
+ {email.messageSource ?? t('emails.emailNotFound')}
</pre>
) : (
- <pre className="whitespace-pre-wrap text-gray-800 dark:text-gray-200 p-3">
+ <pre className="whitespace-pre-wrap text-gray-800 p-3">
{email.messagePlain}
</pre>
)}
diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.json
index 12d7f120c..ae1c58ce0 100644
--- a/apps/browser-extension/src/i18n/locales/en.json
+++ b/apps/browser-extension/src/i18n/locales/en.json
@@ -326,6 +326,11 @@
"date": "Date",
"emailContent": "Email content",
"emailNotFound": "Email not found",
+ "formatHtml": "HTML",
+ "formatPlain": "Text",
+ "formatSource": "Source",
+ "formatSwitchTitle": "Switch email format",
+ "item": "Item",
"noEmails": "No emails found",
"noEmailsDescription": "You have not received any emails at your private email addresses yet. When you receive a new email, it will appear here.",
"offlineMessage": "You are offline. Please connect to the internet to load your emails.",
diff --git a/apps/browser-extension/src/utils/EncryptionUtility.ts b/apps/browser-extension/src/utils/EncryptionUtility.ts
index 0a7647796..085cd5e51 100644
--- a/apps/browser-extension/src/utils/EncryptionUtility.ts
+++ b/apps/browser-extension/src/utils/EncryptionUtility.ts
@@ -336,6 +336,9 @@ export class EncryptionUtility {
if (email.messagePlain) {
decryptedEmail.messagePlain = await EncryptionUtility.symmetricDecrypt(email.messagePlain, symmetricKeyBase64);
}
+ if (email.messageSource) {
+ decryptedEmail.messageSource = await EncryptionUtility.symmetricDecrypt(email.messageSource, symmetricKeyBase64);
+ }
return decryptedEmail;
} catch (err) {
diff --git a/apps/browser-extension/src/utils/db/queries/ItemQueries.ts b/apps/browser-extension/src/utils/db/queries/ItemQueries.ts
index 0164e8dce..38644b4d0 100644
--- a/apps/browser-extension/src/utils/db/queries/ItemQueries.ts
+++ b/apps/browser-extension/src/utils/db/queries/ItemQueries.ts
@@ -230,6 +230,20 @@ export class ItemQueries {
AND i.IsDeleted = 0
AND i.DeletedAt IS NULL`;
+ /**
+ * Look up an item (id + name) by an email address stored in any of its login email fields.
+ */
+ public static readonly GET_ITEM_BY_EMAIL = `
+ SELECT i.Id as Id, i.Name as Name
+ FROM FieldValues fv
+ INNER JOIN Items i ON fv.ItemId = i.Id
+ WHERE fv.FieldKey = ?
+ AND fv.Value = ?
+ AND fv.IsDeleted = 0
+ AND i.IsDeleted = 0
+ AND i.DeletedAt IS NULL
+ LIMIT 1`;
+
/**
* Get item-level fields for change detection during updates.
*/
diff --git a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts b/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts
index 31e405854..dd05f2b58 100644
--- a/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts
+++ b/apps/browser-extension/src/utils/db/repositories/ItemRepository.ts
@@ -159,6 +159,19 @@ export class ItemRepository extends BaseRepository {
return results.map(row => row.Email);
}
+ /**
+ * Find the item (id + name) associated with a given email address, if any.
+ * @param email - The full email address (local@domain) to look up
+ * @returns Object with Id and Name, or null when no active item uses this address
+ */
+ public findIdByEmail(email: string): { Id: string; Name: string | null } | null {
+ const results = this.client.executeQuery<{ Id: string; Name: string | null }>(
+ ItemQueries.GET_ITEM_BY_EMAIL,
+ [FieldKey.LoginEmail, email]
+ );
+ return results.length > 0 ? results[0] : null;
+ }
+
/**
* Create a new item with field-based structure.
* @param item The item object to insert
diff --git a/apps/browser-extension/src/utils/dist/core/models/webapi/index.d.ts b/apps/browser-extension/src/utils/dist/core/models/webapi/index.d.ts
index f83fab78c..0bffdace9 100644
--- a/apps/browser-extension/src/utils/dist/core/models/webapi/index.d.ts
+++ b/apps/browser-extension/src/utils/dist/core/models/webapi/index.d.ts
@@ -194,6 +194,8 @@ type Email = {
messageHtml: string;
/** The plain text body of the email message */
messagePlain: string;
+ /** The raw RFC 822 source of the email message */
+ messageSource: string;
/** The ID of the email */
id: number;
/** The subject of the email */
diff --git a/core/models/src/webapi/Email.ts b/core/models/src/webapi/Email.ts
index 3eeec03df..0b0109df3 100644
--- a/core/models/src/webapi/Email.ts
+++ b/core/models/src/webapi/Email.ts
@@ -7,6 +7,9 @@ export type Email = {
/** The plain text body of the email message */
messagePlain: string;
+ /** The raw RFC 822 source of the email message */
+ messageSource: string;
+
/** The ID of the email */
id: number;