Skip to content

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

Tweak ItemAddEdit and add field menu component (#1404)

Leendert de Borst03b7b77642fd
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Forms/AddFieldMenu.tsx b/apps/browser-extension/src/entrypoints/popup/components/Forms/AddFieldMenu.tsx
index 28a91f0b4..b4b4516d1 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Forms/AddFieldMenu.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Forms/AddFieldMenu.tsx
@@ -3,33 +3,49 @@ import { useTranslation } from 'react-i18next';
import ModalWrapper from '@/entrypoints/popup/components/Dialogs/ModalWrapper';
-import type { FieldType } from '@/utils/dist/core/models/vault';
+import type { FieldType, SystemFieldDefinition } from '@/utils/dist/core/models/vault';
+import { FieldCategories } from '@/utils/dist/core/models/vault';
/**
- * Visibility state for optional sections.
+ * Configuration for an optional section (not field-based).
*/
-type SectionVisibility = {
- showNotes: boolean;
- show2FA: boolean;
- showAttachments: boolean;
+type OptionalSection = {
+ /** Unique key for this section */
+ key: string;
+ /** Whether this section is currently visible */
+ isVisible: boolean;
+ /** Callback to add/show this section */
+ onAdd: () => void;
};
/**
- * Callbacks for adding optional sections.
+ * Callbacks for adding custom fields.
*/
-type SectionCallbacks = {
- onAddNotes: () => void;
- onAdd2FA: () => void;
- onAddAttachments: () => void;
+type AddFieldMenuCallbacks = {
+ /** Callback when a system field is added */
+ onAddSystemField: (fieldKey: string) => void;
+ /** Callback when a custom field is added */
onAddCustomField: (label: string, fieldType: FieldType) => void;
};
type AddFieldMenuProps = {
- isEditMode: boolean;
- /** Whether 2FA is supported for the current item type (determined by model config) */
- supports2FA: boolean;
- visibility: SectionVisibility;
- callbacks: SectionCallbacks;
+ /**
+ * Optional system fields for the current item type.
+ * These are fields with ShowByDefault: false that can be added via the menu.
+ */
+ optionalSystemFields: SystemFieldDefinition[];
+ /**
+ * Field keys that are currently visible (either have a value or were manually added).
+ */
+ visibleFieldKeys: Set<string>;
+ /**
+ * Optional sections (like 2FA, Attachments) that are not field-based.
+ */
+ optionalSections: OptionalSection[];
+ /**
+ * Callbacks for adding fields.
+ */
+ callbacks: AddFieldMenuCallbacks;
};
/**
@@ -78,15 +94,41 @@ const PlusIcon: React.FC = () => (
</svg>
);
+/**
+ * Get icon for a field category.
+ */
+const getFieldIcon = (category: string): React.ReactNode => {
+ switch (category) {
+ case FieldCategories.Notes:
+ return <NotesIcon />;
+ default:
+ return <PlusIcon />;
+ }
+};
+
+/**
+ * Get icon for optional sections.
+ */
+const getSectionIcon = (key: string): React.ReactNode => {
+ switch (key) {
+ case '2fa':
+ return <LockIcon />;
+ case 'attachments':
+ return <AttachmentIcon />;
+ default:
+ return <PlusIcon />;
+ }
+};
+
/**
* A dropdown menu for adding optional fields and sections to an item.
- * Includes built-in logic for determining which options to show based on item type.
- * Also includes the custom field modal.
+ * Dynamically determines which options to show based on system field registry
+ * and current field visibility.
*/
const AddFieldMenu: React.FC<AddFieldMenuProps> = ({
- isEditMode,
- supports2FA,
- visibility,
+ optionalSystemFields,
+ visibleFieldKeys,
+ optionalSections,
callbacks
}) => {
const { t } = useTranslation();
@@ -127,70 +169,53 @@ const AddFieldMenu: React.FC<AddFieldMenuProps> = ({
}, []);
/**
- * Handle adding notes and closing menu.
- */
- const handleAddNotes = useCallback((): void => {
- callbacks.onAddNotes();
- setIsOpen(false);
- }, [callbacks]);
-
- /**
- * Handle adding 2FA and closing menu.
+ * Handle adding a system field and closing menu.
*/
- const handleAdd2FA = useCallback((): void => {
- callbacks.onAdd2FA();
+ const handleAddSystemField = useCallback((fieldKey: string): void => {
+ callbacks.onAddSystemField(fieldKey);
setIsOpen(false);
}, [callbacks]);
/**
- * Handle adding attachments and closing menu.
+ * Handle adding an optional section and closing menu.
*/
- const handleAddAttachments = useCallback((): void => {
- callbacks.onAddAttachments();
+ const handleAddSection = useCallback((onAdd: () => void): void => {
+ onAdd();
setIsOpen(false);
- }, [callbacks]);
+ }, []);
/**
- * Build menu options based on item type and current visibility.
+ * Build menu options based on optional system fields and sections.
*/
const menuOptions = useMemo((): MenuOption[] => {
const options: MenuOption[] = [];
- /*
- * Notes option - available for all types when not already shown.
- * In edit mode, notes are always shown if they have content, so we don't need to show the option.
- */
- if (!visibility.showNotes && !isEditMode) {
- options.push({
- key: 'notes',
- label: t('credentials.notes'),
- icon: <NotesIcon />,
- action: handleAddNotes
- });
- }
+ // Add optional system fields that are not currently visible
+ optionalSystemFields.forEach(field => {
+ if (!visibleFieldKeys.has(field.FieldKey)) {
+ options.push({
+ key: field.FieldKey,
+ label: t(`fieldLabels.${field.FieldKey}`, { defaultValue: field.FieldKey }),
+ icon: getFieldIcon(field.Category),
+ action: () => handleAddSystemField(field.FieldKey)
+ });
+ }
+ });
- // 2FA TOTP option - only for types with login fields
- if (supports2FA && !visibility.show2FA) {
- options.push({
- key: '2fa',
- label: t('common.twoFactorAuthentication'),
- icon: <LockIcon />,
- action: handleAdd2FA
- });
- }
-
- // Attachments option - available for all types when not already shown
- if (!visibility.showAttachments) {
- options.push({
- key: 'attachments',
- label: t('common.attachments'),
- icon: <AttachmentIcon />,
- action: handleAddAttachments
- });
- }
+ // Add optional sections that are not currently visible
+ optionalSections.forEach(section => {
+ if (!section.isVisible) {
+ options.push({
+ key: section.key,
+ label: t(`common.${section.key === '2fa' ? 'twoFactorAuthentication' : section.key}`),
+ icon: getSectionIcon(section.key),
+ action: () => handleAddSection(section.onAdd)
+ });
+ }
+ });
return options;
- }, [supports2FA, isEditMode, visibility, t, handleAddNotes, handleAdd2FA, handleAddAttachments]);
+ }, [optionalSystemFields, visibleFieldKeys, optionalSections, t, handleAddSystemField, handleAddSection]);
return (
<>
@@ -307,3 +332,4 @@ const AddFieldMenu: React.FC<AddFieldMenuProps> = ({
};
export default AddFieldMenu;
+export type { OptionalSection };
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/credentials/ItemAddEdit.tsx b/apps/browser-extension/src/entrypoints/popup/pages/credentials/ItemAddEdit.tsx
index ff2fe0af8..6e7920699 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/credentials/ItemAddEdit.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/credentials/ItemAddEdit.tsx
@@ -6,7 +6,7 @@ import AttachmentUploader from '@/entrypoints/popup/components/Credentials/Detai
import PasskeyEditor from '@/entrypoints/popup/components/Credentials/Details/PasskeyEditor';
import TotpEditor from '@/entrypoints/popup/components/Credentials/Details/TotpEditor';
import Modal from '@/entrypoints/popup/components/Dialogs/Modal';
-import AddFieldMenu from '@/entrypoints/popup/components/Forms/AddFieldMenu';
+import AddFieldMenu, { type OptionalSection } from '@/entrypoints/popup/components/Forms/AddFieldMenu';
import EditableFieldLabel from '@/entrypoints/popup/components/Forms/EditableFieldLabel';
import EmailDomainField from '@/entrypoints/popup/components/Forms/EmailDomainField';
import { FormInput } from '@/entrypoints/popup/components/Forms/FormInput';
@@ -30,7 +30,7 @@ import { useVaultMutate } from '@/entrypoints/popup/hooks/useVaultMutate';
import { SKIP_FORM_RESTORE_KEY } from '@/utils/Constants';
import type { Item, ItemField, ItemType, FieldType, Attachment, TotpCode } from '@/utils/dist/core/models/vault';
-import { FieldCategories, FieldTypes, ItemTypes, getSystemFieldsForItemType, isFieldShownByDefault } from '@/utils/dist/core/models/vault';
+import { FieldCategories, FieldTypes, ItemTypes, getSystemFieldsForItemType, getOptionalFieldsForItemType, isFieldShownByDefault } from '@/utils/dist/core/models/vault';
import { FaviconService } from '@/utils/FaviconService';
import { browser } from '#imports';
@@ -67,7 +67,6 @@ type PersistedFormData = {
secretKey: string;
};
};
- showNotes: boolean;
show2FA: boolean;
showAttachments: boolean;
manuallyAddedFields: string[];
@@ -117,7 +116,6 @@ const ItemAddEdit: React.FC = () => {
// UI visibility state
const [showTypeDropdown, setShowTypeDropdown] = useState(false);
- const [showNotes, setShowNotes] = useState(false);
const [show2FA, setShow2FA] = useState(false);
const [showAttachments, setShowAttachments] = useState(false);
@@ -177,9 +175,6 @@ const ItemAddEdit: React.FC = () => {
if (data.totpEditorState) {
setTotpEditorState(data.totpEditorState);
}
- if (data.showNotes !== undefined) {
- setShowNotes(data.showNotes);
- }
if (data.show2FA !== undefined) {
setShow2FA(data.show2FA);
}
@@ -206,7 +201,6 @@ const ItemAddEdit: React.FC = () => {
fieldValues,
customFields,
totpEditorState,
- showNotes,
show2FA,
showAttachments,
manuallyAddedFields: Array.from(manuallyAddedFields),
@@ -233,6 +227,42 @@ const ItemAddEdit: React.FC = () => {
return applicableSystemFields.find(field => field.FieldKey === 'notes.content');
}, [applicableSystemFields]);
+ /**
+ * Optional system fields for the current item type.
+ * These are fields with ShowByDefault: false that can be added via the + menu.
+ */
+ const optionalSystemFields = useMemo(() => {
+ if (!item) {
+ return [];
+ }
+ return getOptionalFieldsForItemType(item.ItemType);
+ }, [item]);
+
+ /**
+ * Set of field keys that are currently visible (shown by default, manually added, or have values).
+ */
+ const visibleFieldKeys = useMemo(() => {
+ const keys = new Set<string>();
+ // Add fields that are shown by default
+ applicableSystemFields.forEach(field => {
+ if (item && isFieldShownByDefault(field, item.ItemType)) {
+ keys.add(field.FieldKey);
+ }
+ });
+ // Add manually added fields
+ manuallyAddedFields.forEach(key => keys.add(key));
+ // Add fields that were initially visible (had values)
+ initiallyVisibleFields.forEach(key => keys.add(key));
+ // Add fields with current values
+ Object.keys(fieldValues).forEach(key => {
+ const value = fieldValues[key];
+ if (value && (Array.isArray(value) ? value.length > 0 : value.toString().trim() !== '')) {
+ keys.add(key);
+ }
+ });
+ return keys;
+ }, [item, applicableSystemFields, manuallyAddedFields, initiallyVisibleFields, fieldValues]);
+
/**
* Check if a field should be shown for the current item type.
* Returns true if field is shown by default, was manually added, or had initial value (edit mode).
@@ -331,13 +361,6 @@ const ItemAddEdit: React.FC = () => {
}));
}
- // Check if notes should be shown by default for this type
- const typeFields = getSystemFieldsForItemType(effectiveType);
- const notesFieldDef = typeFields.find(f => f.FieldKey === 'notes.content');
- if (notesFieldDef && isFieldShownByDefault(notesFieldDef, effectiveType)) {
- setShowNotes(true);
- }
-
// Load folders
if (dbContext?.sqliteClient) {
const allFolders = dbContext.sqliteClient.folders.getAll();
@@ -743,26 +766,8 @@ const ItemAddEdit: React.FC = () => {
});
}
- // Check field visibility based on model config for the new type
- const newTypeFields = getSystemFieldsForItemType(newType);
-
- // Check if alias fields should be shown by default for the new type (for auto-generation)
- const newAliasField = newTypeFields.find(f => f.Category === FieldCategories.Alias);
- const aliasShownByDefault = newAliasField ? isFieldShownByDefault(newAliasField, newType) : false;
- if (aliasShownByDefault && !isEditMode) {
- aliasGeneratedRef.current = false;
- }
-
- // Check if notes should be shown by default for the new type
- const newNotesField = newTypeFields.find(f => f.FieldKey === 'notes.content');
- const notesShownByDefault = newNotesField ? isFieldShownByDefault(newNotesField, newType) : false;
- setShowNotes(notesShownByDefault || !!fieldValues['notes.content']);
-
- // Update 2FA visibility - supported for types with login fields
- const newTypeHasLoginFields = newTypeFields.some(f => f.FieldKey === 'login.username' || f.FieldKey === 'login.password');
- if (!newTypeHasLoginFields && show2FA) {
- setShow2FA(false);
- }
+ // Reset alias generated flag, so alias fields will be filled (again) if they are shown by the new type
+ aliasGeneratedRef.current = false;
setItem({
...item,
@@ -771,24 +776,21 @@ const ItemAddEdit: React.FC = () => {
});
setShowTypeDropdown(false);
- }, [item, isEditMode, fieldValues, show2FA]);
+ }, [item, isEditMode]);
/**
- * Remove notes section - clears value and hides the section.
+ * Remove notes section - clears value and removes from manually added fields.
*/
const handleRemoveNotesSection = useCallback(() => {
setFieldValues(prev => ({
...prev,
'notes.content': ''
}));
- setShowNotes(false);
- }, []);
-
- /**
- * Handle adding notes section.
- */
- const handleAddNotes = useCallback((): void => {
- setShowNotes(true);
+ setManuallyAddedFields(prev => {
+ const newSet = new Set(prev);
+ newSet.delete('notes.content');
+ return newSet;
+ });
}, []);
/**
@@ -806,12 +808,34 @@ const ItemAddEdit: React.FC = () => {
}, []);
/**
- * Handle adding an optional system field (e.g., email for Login type).
+ * Handle adding an optional system field (e.g., email for Login type, notes).
*/
const handleAddOptionalField = useCallback((fieldKey: string): void => {
setManuallyAddedFields(prev => new Set(prev).add(fieldKey));
}, []);
+ /**
+ * Optional sections (non-field-based) for the AddFieldMenu.
+ */
+ const optionalSections = useMemo((): OptionalSection[] => {
+ const sections: OptionalSection[] = [];
+ // 2FA - only for types with login fields
+ if (hasLoginFields) {
+ sections.push({
+ key: '2fa',
+ isVisible: show2FA,
+ onAdd: handleAdd2FA
+ });
+ }
+ // Attachments - always available
+ sections.push({
+ key: 'attachments',
+ isVisible: showAttachments,
+ onAdd: handleAddAttachments
+ });
+ return sections;
+ }, [hasLoginFields, show2FA, showAttachments, handleAdd2FA, handleAddAttachments]);
+
/**
* Handle removing an optional system field (e.g., email for Login type).
* Only allowed for fields that were manually added (not shown by default).
@@ -1232,11 +1256,11 @@ const ItemAddEdit: React.FC = () => {
)}
{/* Notes Section */}
- {notesField && (showNotes || fieldValues['notes.content']) && (
+ {notesField && visibleFieldKeys.has('notes.content') && (
<FormSection
title={t('credentials.notes')}
actions={
- !isEditMode && !shouldShowField(notesField) ? (
+ !shouldShowField(notesField) ? (
<button
type="button"
onClick={handleRemoveNotesSection}
@@ -1283,17 +1307,11 @@ const ItemAddEdit: React.FC = () => {
{/* Add Field Menu */}
<AddFieldMenu
- isEditMode={isEditMode}
- supports2FA={hasLoginFields}
- visibility={{
- showNotes,
- show2FA,
- showAttachments
- }}
+ optionalSystemFields={optionalSystemFields}
+ visibleFieldKeys={visibleFieldKeys}
+ optionalSections={optionalSections}
callbacks={{
- onAddNotes: handleAddNotes,
- onAdd2FA: handleAdd2FA,
- onAddAttachments: handleAddAttachments,
+ onAddSystemField: handleAddOptionalField,
onAddCustomField: handleAddCustomField
}}
/>