Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add edit form components (#2413)
Leendert de Borste32905ffe908
diff --git a/apps/web/src/components/forms/CopyPasteFormRow.tsx b/apps/web/src/components/forms/CopyPasteFormRow.tsxnew file mode 100644index 000000000..226b52385--- /dev/null+++ b/apps/web/src/components/forms/CopyPasteFormRow.tsx@@ -0,0 +1,37 @@+import React from 'react';++import { useClipboardCopy } from '@/hooks/useClipboardCopy';++type CopyPasteFormRowProps = {+ id: string;+ label?: string | null;+ value: string;+};++/**+ * Read-only value that copies to the clipboard on click.+ */+const CopyPasteFormRow: React.FC<CopyPasteFormRowProps> = ({ id, label = null, value }) => {+ const { copied, copyToClipboard } = useClipboardCopy(id);++ return (+ <>+ {label !== null && label !== '' && (+ <label htmlFor={id} className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{label}</label>+ )}+ <div className="relative flex-grow">+ <input type="text" autoComplete="off" id={id} className={`outline-0 shadow-sm bg-gray-50 border ${copied ? 'border-green-500 border-2' : 'border-gray-300'} text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-10 dark:bg-gray-700 ${copied ? 'dark:border-green-500' : 'dark:border-gray-600'} dark:placeholder-gray-400 dark:text-white`} value={value} onClick={() => void copyToClipboard(value)} readOnly />+ {copied && (+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-green-500 dark:text-green-400">+ <svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">+ <path d="M8 3a1 1 0 011-1h2a1 1 0 110 2H9a1 1 0 01-1-1z"></path>+ <path d="M6 3a2 2 0 00-2 2v11a2 2 0 002 2h8a2 2 0 002-2V5a2 2 0 00-2-2 3 3 0 01-3 3H9a3 3 0 01-3-3z"></path>+ </svg>+ </span>+ )}+ </div>+ </>+ );+};++export default CopyPasteFormRow;diff --git a/apps/web/src/components/forms/CopyPastePasswordFormRow.tsx b/apps/web/src/components/forms/CopyPastePasswordFormRow.tsxnew file mode 100644index 000000000..54c5a73a9--- /dev/null+++ b/apps/web/src/components/forms/CopyPastePasswordFormRow.tsx@@ -0,0 +1,53 @@+import React, { useState } from 'react';++import { useClipboardCopy } from '@/hooks/useClipboardCopy';++type CopyPastePasswordFormRowProps = {+ id: string;+ label?: string;+ value: string;+};++/**+ * Read-only masked value that copies to the clipboard on click, with a show/hide toggle.+ */+const CopyPastePasswordFormRow: React.FC<CopyPastePasswordFormRowProps> = ({ id, label = '', value }) => {+ const { copied, copyToClipboard } = useClipboardCopy(id);+ const [isPasswordVisible, setIsPasswordVisible] = useState(false);+ const borderClasses = `${copied ? 'border-green-500 border-2' : 'border-gray-300'} ${copied ? 'dark:border-green-500' : 'dark:border-gray-600'}`;++ return (+ <>+ {label !== '' && <label htmlFor={id} className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{label}</label>}+ <div className="relative">+ {isPasswordVisible ? (+ <div id={id} className={`outline-0 shadow-sm bg-gray-50 border ${borderClasses} text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-20 min-h-[42px] break-all cursor-pointer dark:bg-gray-700 dark:text-white`} onClick={() => void copyToClipboard(value)}>{value}</div>+ ) : (+ <input type="password" id={id} className={`outline-0 shadow-sm bg-gray-50 border ${borderClasses} text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-20 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white`} value={value} onClick={() => void copyToClipboard(value)} readOnly />+ )}+ <button type="button" className="absolute inset-y-1 right-1 flex items-center justify-center w-10 h-8 text-gray-500 bg-gray-200 rounded-md shadow-sm hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors duration-200 dark:bg-gray-600 dark:hover:bg-gray-500 dark:text-gray-300" onClick={() => setIsPasswordVisible(v => !v)}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">+ {isPasswordVisible ? (+ <>+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>+ </>+ ) : (+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"></path>+ )}+ </svg>+ </button>+ {copied && (+ <span className="absolute top-1 h-8 right-10 flex items-center pr-3 text-green-500 dark:text-green-400">+ <svg className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">+ <path d="M8 3a1 1 0 011-1h2a1 1 0 110 2H9a1 1 0 01-1-1z"></path>+ <path d="M6 3a2 2 0 00-2 2v11a2 2 0 002 2h8a2 2 0 002-2V5a2 2 0 00-2-2 3 3 0 01-3 3H9a3 3 0 01-3-3z"></path>+ </svg>+ </span>+ )}+ </div>+ </>+ );+};++export default CopyPastePasswordFormRow;diff --git a/apps/web/src/components/forms/CustomFieldLabel.tsx b/apps/web/src/components/forms/CustomFieldLabel.tsxnew file mode 100644index 000000000..a9cf21f83--- /dev/null+++ b/apps/web/src/components/forms/CustomFieldLabel.tsx@@ -0,0 +1,36 @@+import React from 'react';+import { useTranslation } from 'react-i18next';++type CustomFieldLabelProps = {+ htmlFor: string;+ label: string;+ onEdit: () => void;+ onDelete?: () => void;+};++/**+ * Label row of a custom field with edit and delete buttons.+ */+const CustomFieldLabel: React.FC<CustomFieldLabelProps> = ({ htmlFor, label, onEdit, onDelete }) => {+ const { t } = useTranslation();++ return (+ <div className="flex items-center gap-2 mb-2">+ <label htmlFor={htmlFor} className="text-sm font-medium text-gray-900 dark:text-white">{label}</label>+ <button type="button" onClick={onEdit} className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 text-xs" title={t('components.main.forms.editableFieldLabel.EditLabel')}>+ <svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z" />+ </svg>+ </button>+ {onDelete && (+ <button type="button" onClick={onDelete} className="text-red-600 dark:text-red-400 hover:text-red-700 dark:hover:text-red-300 text-xs" title={t('components.main.forms.editableFieldLabel.DeleteField')}>+ <svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />+ </svg>+ </button>+ )}+ </div>+ );+};++export default CustomFieldLabel;diff --git a/apps/web/src/components/forms/CustomFieldModal.tsx b/apps/web/src/components/forms/CustomFieldModal.tsxnew file mode 100644index 000000000..2e8277e72--- /dev/null+++ b/apps/web/src/components/forms/CustomFieldModal.tsx@@ -0,0 +1,94 @@+import { FieldTypes } from '@aliasvault/models/vault';+import React, { useEffect, useRef, useState } from 'react';+import { useTranslation } from 'react-i18next';++import FormModal from '@/components/shared/FormModal';++type CustomFieldModalProps = {+ isOpen: boolean;+ isEditMode?: boolean;+ initialLabel?: string;+ initialFieldType?: string;+ onClose: () => void;+ onSubmit: (label: string, fieldType: string) => void;+};++/** The field types a custom field can have, with their translation keys. */+const FIELD_TYPE_OPTIONS: [string, string][] = [+ [FieldTypes.Text, 'FieldTypeText'],+ [FieldTypes.Password, 'FieldTypePassword'],+ [FieldTypes.Hidden, 'FieldTypeHidden'],+ [FieldTypes.TextArea, 'FieldTypeTextArea'],+ [FieldTypes.URL, 'FieldTypeUrl'],+ [FieldTypes.Email, 'FieldTypeEmail'],+ [FieldTypes.Phone, 'FieldTypePhone'],+ [FieldTypes.Number, 'FieldTypeNumber'],+ [FieldTypes.Date, 'FieldTypeDate'],+];++/**+ * Modal for creating and editing a custom field.+ */+const CustomFieldModal: React.FC<CustomFieldModalProps> = ({ isOpen, isEditMode = false, initialLabel = '', initialFieldType = FieldTypes.Text, onClose, onSubmit }) => {+ const { t } = useTranslation();+ const [label, setLabel] = useState(initialLabel);+ const [fieldType, setFieldType] = useState(initialFieldType);+ const inputRef = useRef<HTMLInputElement>(null);++ // Reset the form to the field being edited (or the defaults) whenever the modal opens, then focus the label.+ useEffect(() => {+ if (!isOpen) {+ return;+ }+ setLabel(initialLabel);+ setFieldType(initialFieldType);+ const timer = setTimeout(() => {+ inputRef.current?.focus();+ inputRef.current?.select();+ }, 50);+ return (): void => clearTimeout(timer);+ }, [initialFieldType, initialLabel, isOpen]);++ /**+ * Submit when the label is filled in.+ */+ const handleConfirm = (): void => {+ if (label.trim().length > 0) {+ onSubmit(label.trim(), fieldType);+ onClose();+ }+ };++ return (+ <FormModal+ isOpen={isOpen}+ title={isEditMode ? t('components.main.items.addFieldMenu.EditCustomField') : t('components.main.items.addFieldMenu.AddCustomField')}+ confirmText={isEditMode ? t('sharedResources.Save') : t('components.main.items.addFieldMenu.Add')}+ cancelText={t('components.main.items.addFieldMenu.Cancel')}+ confirmButtonClass="bg-primary-600 hover:bg-primary-500 dark:bg-primary-700 dark:hover:bg-primary-600"+ maxWidth="md"+ onClose={onClose}+ onConfirm={handleConfirm}+ icon={(+ <svg className="h-6 w-6 text-primary-600 dark:text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6" />+ </svg>+ )}+ >+ <div className="space-y-4">+ <div>+ <label htmlFor="custom-field-label-input" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">{t('components.main.items.addFieldMenu.FieldLabel')}</label>+ <input ref={inputRef} type="text" id="custom-field-label-input" value={label} onChange={e => setLabel(e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:text-white" placeholder={t('components.main.items.addFieldMenu.EnterFieldName')} />+ </div>+ <div>+ <label htmlFor="custom-field-type-select" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">{t('components.main.items.addFieldMenu.FieldType')}</label>+ <select id="custom-field-type-select" value={fieldType} onChange={e => setFieldType(e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:text-white">+ {FIELD_TYPE_OPTIONS.map(([value, key]) => <option key={value} value={value}>{t(`components.main.items.addFieldMenu.${key}`)}</option>)}+ </select>+ </div>+ </div>+ </FormModal>+ );+};++export default CustomFieldModal;diff --git a/apps/web/src/components/forms/EditFormRow.tsx b/apps/web/src/components/forms/EditFormRow.tsxnew file mode 100644index 000000000..695e4afd3--- /dev/null+++ b/apps/web/src/components/forms/EditFormRow.tsx@@ -0,0 +1,35 @@+import React from 'react';++type EditFormRowProps = {+ id: string;+ label: string;+ type?: 'text' | 'textarea';+ value: string;+ onChange: (value: string) => void;+ onFocus?: (event: React.FocusEvent<HTMLInputElement>) => void;+ placeholder?: string;+ labelStyle?: 'default' | 'header';+};++/** Classes of every editable text input on the item form. */+export const EDIT_INPUT_CLASSES = 'outline-0 shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white';++/**+ * Labeled text input or textarea.+ */+const EditFormRow: React.FC<EditFormRowProps> = ({ id, label, type = 'text', value, onChange, onFocus, placeholder = '', labelStyle = 'default' }) => (+ <>+ {labelStyle === 'header'+ ? <label htmlFor={id} className="mb-4 text-xl font-semibold dark:text-white block">{label}</label>+ : <label htmlFor={id} className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{label}</label>}+ <div className="relative">+ {type === 'textarea' ? (+ <textarea id={id} style={{ height: '200px' }} className={EDIT_INPUT_CLASSES} value={value} onChange={e => onChange(e.target.value)} placeholder={placeholder} autoCapitalize="off" autoCorrect="off"></textarea>+ ) : (+ <input type="text" id={id} autoComplete="off" onFocus={onFocus} className={EDIT_INPUT_CLASSES} value={value} onChange={e => onChange(e.target.value)} placeholder={placeholder} autoCapitalize="off" autoCorrect="off" />+ )}+ </div>+ </>+);++export default EditFormRow;diff --git a/apps/web/src/components/forms/EditPasswordFormRow.tsx b/apps/web/src/components/forms/EditPasswordFormRow.tsxnew file mode 100644index 000000000..aab4cdcde--- /dev/null+++ b/apps/web/src/components/forms/EditPasswordFormRow.tsx@@ -0,0 +1,142 @@+import { generatePassword } from '@aliasvault/client/rust/RustCore';+import { MAX_WORD_COUNT, MIN_WORD_COUNT } from '@aliasvault/models/defaults';+import React, { useEffect, useState } from 'react';+import { useTranslation } from 'react-i18next';++import PasswordSettingsPopup from '@/components/settings/PasswordSettingsPopup';+import { useDb } from '@/context/DbContext';+import { lengthToSlider, SLIDER_MAX, SLIDER_MIN, sliderToLength } from '@/utils/PasswordLengthSlider';++import type { PasswordSettings } from '@aliasvault/models/vault';++type EditPasswordFormRowProps = {+ id: string;+ label: string;+ value: string;+ onChange: (value: string) => void;+ placeholder?: string;+ /** Show the value in clear text; a generated password always shows. */+ showPassword?: boolean;+ showGenerateButtons?: boolean;+};++/**+ * Password input with show/hide, generator settings, a generate button and the inline length slider.+ */+const EditPasswordFormRow: React.FC<EditPasswordFormRowProps> = ({ id, label, value, onChange, placeholder = '', showPassword = false, showGenerateButtons = true }) => {+ const { t } = useTranslation();+ const dbContext = useDb();+ const [isVisible, setIsVisible] = useState(showPassword);+ const [isSettingsVisible, setIsSettingsVisible] = useState(false);+ const [settings, setSettings] = useState<PasswordSettings | null>(null);+ const [sliderValue, setSliderValue] = useState(0);+ const tk = 'components.main.settings.passwordSettingsPopup';++ useEffect(() => {+ if (showPassword) {+ setIsVisible(true);+ }+ }, [showPassword]);++ useEffect(() => {+ const stored = dbContext.sqliteClient?.settings.getPasswordSettings();+ if (stored) {+ setSettings(stored);+ setSliderValue(lengthToSlider(stored.Length));+ }+ }, [dbContext.sqliteClient]);++ const isDiceware = settings?.Type === 'diceware';++ /**+ * Generate a password with the current settings and show it.+ */+ const regenerate = async (current: PasswordSettings): Promise<void> => {+ onChange(await generatePassword(current));+ setIsVisible(true);+ };++ /**+ * Take over the settings chosen in the popup and its generated password.+ */+ const handleSettingsSaved = (saved: PasswordSettings, generated: string): void => {+ setSettings(saved);+ setSliderValue(lengthToSlider(saved.Length));+ setIsVisible(true);+ onChange(generated);+ };++ const visibilityButtonClasses = `px-3 text-gray-500 dark:text-white bg-gray-200 hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium text-sm dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800${showGenerateButtons ? '' : ' rounded-r-lg'}`;++ return (+ <>+ <label htmlFor={id} className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{label}</label>+ <div className="flex">+ <div className="relative flex-grow">+ <input type={isVisible ? 'text' : 'password'} id={id} autoComplete="off" className="outline-0 shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-l-lg block w-full p-2.5 pr-16 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value={value} onChange={e => onChange(e.target.value)} placeholder={placeholder} />+ </div>+ <div className="flex">+ <button type="button" className={visibilityButtonClasses} onClick={() => setIsVisible(v => !v)}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">+ {isVisible ? (+ <>+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>+ </>+ ) : (+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"></path>+ )}+ </svg>+ </button>+ {showGenerateButtons && (+ <>+ <button type="button" id={`${id}-generator-settings`} className="px-3 text-gray-500 dark:text-white bg-gray-200 hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium text-sm border-l border-gray-300 dark:border-gray-700 dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" onClick={() => setIsSettingsVisible(true)}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path>+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>+ </svg>+ </button>+ <button type="button" className="px-3 text-gray-500 dark:text-white bg-gray-200 hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium rounded-r-lg text-sm border-l border-gray-300 dark:border-gray-700 dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" onClick={() => settings && void regenerate(settings)}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>+ </svg>+ </button>+ </>+ )}+ </div>+ </div>++ {showGenerateButtons && settings && (+ <div className="pt-2">+ <div className="flex items-center justify-between mb-1">+ <label htmlFor={`${id}-inline-length`} className="text-sm font-medium text-gray-700 dark:text-gray-300">+ {isDiceware ? t(`${tk}.WordCountLabel`) : t(`${tk}.PasswordLengthLabel`, { 0: settings.Length })}+ </label>+ {isDiceware && <span className="text-sm text-gray-600 dark:text-gray-400 font-mono">{settings.WordCount}</span>}+ </div>+ {isDiceware ? (+ <input type="range" id={`${id}-inline-length`} min={MIN_WORD_COUNT} max={MAX_WORD_COUNT} step="1" className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700 av-range-slider" value={settings.WordCount} onChange={(e) => {+ const next = { ...settings, WordCount: Number.parseInt(e.target.value, 10) };+ setSettings(next);+ void regenerate(next);+ }} />+ ) : (+ <input type="range" id={`${id}-inline-length`} min={SLIDER_MIN} max={SLIDER_MAX} step="0.1" className="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700 av-range-slider" value={sliderValue} onChange={(e) => {+ const slider = Number.parseFloat(e.target.value);+ setSliderValue(slider);+ const next = { ...settings, Length: sliderToLength(slider) };+ setSettings(next);+ void regenerate(next);+ }} />+ )}+ </div>+ )}++ {isSettingsVisible && settings && (+ <PasswordSettingsPopup passwordSettings={settings} isTemporary onSaveSettings={handleSettingsSaved} onClose={() => setIsSettingsVisible(false)} />+ )}+ </>+ );+};++export default EditPasswordFormRow;diff --git a/apps/web/src/components/forms/EditUsernameFormRow.tsx b/apps/web/src/components/forms/EditUsernameFormRow.tsxnew file mode 100644index 000000000..e6f0d07e3--- /dev/null+++ b/apps/web/src/components/forms/EditUsernameFormRow.tsx@@ -0,0 +1,31 @@+import React from 'react';++type EditUsernameFormRowProps = {+ id: string;+ label: string;+ value: string;+ onChange: (value: string) => void;+ onGenerateNewUsername: () => void;+ placeholder?: string;+};++/**+ * Username input with a generate button.+ */+const EditUsernameFormRow: React.FC<EditUsernameFormRowProps> = ({ id, label, value, onChange, onGenerateNewUsername, placeholder = '' }) => (+ <>+ <label htmlFor={id} className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{label}</label>+ <div className="flex">+ <div className="relative flex-grow">+ <input type="text" id={id} autoComplete="off" className="outline-0 shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-l-lg block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value={value} onChange={e => onChange(e.target.value)} placeholder={placeholder} />+ </div>+ <button type="button" id="generate-username-button" className="px-3 text-gray-500 bg-gray-200 hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium rounded-r-lg text-sm dark:text-white dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" onClick={onGenerateNewUsername}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>+ </svg>+ </button>+ </div>+ </>+);++export default EditUsernameFormRow;diff --git a/apps/web/src/components/forms/EmailDomainField.tsx b/apps/web/src/components/forms/EmailDomainField.tsxnew file mode 100644index 000000000..1b972f385--- /dev/null+++ b/apps/web/src/components/forms/EmailDomainField.tsx@@ -0,0 +1,254 @@+import React, { useEffect, useRef, useState } from 'react';+import { useTranslation } from 'react-i18next';++import { getAppConfig } from '@/config/AppConfig';+import { useDb } from '@/context/DbContext';++type EmailDomainFieldProps = {+ id: string;+ value: string;+ onChange: (value: string) => void;+ error?: string | null;+ required?: boolean;+ onRemove?: () => void;+ /** Start in free text email mode instead of alias mode. */+ defaultToEmailMode?: boolean;+ onGenerateAlias?: () => void;+};++/**+ * Email input that switches between a free text email and an alias email (which shows a domain picker element).+ */+const EmailDomainField: React.FC<EmailDomainFieldProps> = ({ id, value, onChange, error = null, required = false, onRemove, defaultToEmailMode = false, onGenerateAlias }) => {+ const { t } = useTranslation();+ const dbContext = useDb();+ const [privateDomains, setPrivateDomains] = useState<string[]>([]);+ const [publicDomains, setPublicDomains] = useState<string[]>([]);+ const [hiddenPrivateDomains, setHiddenPrivateDomains] = useState<string[]>([]);+ const [isCustomDomain, setIsCustomDomain] = useState(defaultToEmailMode);+ const [localPart, setLocalPart] = useState('');+ const [selectedDomain, setSelectedDomain] = useState('');+ const [isPopupVisible, setIsPopupVisible] = useState(false);+ const modeToggledByUser = useRef(false);+ const tk = 'components.main.forms.emailDomainField';++ const showPrivateDomains = privateDomains.length > 0 && !(privateDomains.length === 1 && (privateDomains[0] === 'DISABLED.TLD' || privateDomains[0] === ''));+ const defaultDomain = showPrivateDomains ? privateDomains[0] : publicDomains[0] ?? '';++ useEffect(() => {+ let cancelled = false;+ void dbContext.getVaultMetadata().then((metadata) => {+ if (cancelled) {+ return;+ }+ const hidden = metadata?.hiddenPrivateEmailDomains ?? getAppConfig().hiddenPrivateEmailDomains;+ setHiddenPrivateDomains(hidden);+ setPrivateDomains((metadata?.privateEmailDomains ?? getAppConfig().privateEmailDomains).filter(d => !hidden.includes(d)));+ setPublicDomains(metadata?.publicEmailDomains ?? []);+ });+ return (): void => {+ cancelled = true;+ };+ }, [dbContext]);++ /*+ * Derive the local part, domain and mode from the value. After a toggle the user's mode choice is kept; on load+ * the mode is detected from the domain. A value without a domain in alias mode gets the domain appended.+ */+ useEffect(() => {+ const skipAutoDetect = modeToggledByUser.current;+ if (value.length === 0) {+ setLocalPart('');+ setSelectedDomain(d => d.length > 0 ? d : defaultDomain);+ return;+ }++ const at = value.indexOf('@');+ if (at >= 0) {+ const local = value.substring(0, at);+ const domain = value.substring(at + 1);+ setLocalPart(local);+ setSelectedDomain(domain);+ if (!skipAutoDetect) {+ setIsCustomDomain(!(publicDomains.includes(domain) || privateDomains.includes(domain) || hiddenPrivateDomains.includes(domain)));+ }+ return;+ }++ setLocalPart(value);+ if (!skipAutoDetect) {+ setIsCustomDomain(true);+ }+ const domain = selectedDomain.length > 0 ? selectedDomain : defaultDomain;+ if (selectedDomain.length === 0) {+ setSelectedDomain(defaultDomain);+ }+ if (!isCustomDomain && skipAutoDetect && value.trim().length > 0 && domain.length > 0) {+ onChange(`${value}@${domain}`);+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [value, publicDomains, privateDomains, hiddenPrivateDomains, defaultDomain]);++ /**+ * The user typed in the input.+ */+ const onInput = (input: string): void => {+ if (isCustomDomain) {+ onChange(input);+ return;+ }+ if (input.includes('@')) {+ setIsCustomDomain(true);+ onChange(input);+ return;+ }+ setLocalPart(input);+ if (input.trim().length === 0) {+ onChange('');+ return;+ }+ const domain = selectedDomain.length > 0 ? selectedDomain : defaultDomain;+ if (domain.length > 0) {+ if (selectedDomain.length === 0) {+ setSelectedDomain(domain);+ }+ onChange(`${input}@${domain}`);+ } else {+ onChange(input);+ }+ };++ /**+ * Pick a domain from the popup.+ */+ const selectDomain = (domain: string): void => {+ setSelectedDomain(domain);+ const cleanLocalPart = localPart.includes('@') ? localPart.split('@')[0] : localPart;+ onChange(cleanLocalPart.trim().length === 0 ? '' : `${cleanLocalPart}@${domain}`);+ setIsCustomDomain(false);+ setIsPopupVisible(false);+ };++ /**+ * Switch to free text email mode, starting fresh.+ */+ const toggleToEmailMode = (): void => {+ if (isCustomDomain) {+ return;+ }+ modeToggledByUser.current = true;+ setIsCustomDomain(true);+ setLocalPart('');+ onChange('');+ };++ /**+ * Switch to alias mode and generate an alias.+ */+ const toggleToAliasMode = (): void => {+ if (!isCustomDomain) {+ return;+ }+ modeToggledByUser.current = true;+ setIsCustomDomain(false);+ setSelectedDomain(defaultDomain);+ setLocalPart('');+ onChange('');+ onGenerateAlias?.();+ };++ /**+ * Generate a new alias in place.+ */+ const handleRegenerate = (): void => {+ modeToggledByUser.current = true;+ onGenerateAlias?.();+ };++ /**+ * Classes of a domain chip in the popup.+ */+ const domainButtonClasses = (domain: string): string => `px-3 py-1.5 text-sm rounded-md transition-colors ${selectedDomain === domain ? 'bg-primary-600 text-white hover:bg-primary-700' : 'bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-600 border border-gray-300 dark:border-gray-600'}`;++ return (+ <>+ <div className="space-y-2">+ <div className="flex items-center justify-between">+ <div className="flex items-center">+ <div className="flex items-center">+ <button type="button" disabled={isCustomDomain} className={isCustomDomain ? 'text-sm font-medium text-primary-600 dark:text-primary-400' : 'text-sm font-medium text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 cursor-pointer transition-colors'} onClick={toggleToEmailMode}>+ {t('sharedResources.Email')}+ </button>+ <span className="mx-2 text-gray-400 dark:text-gray-500">/</span>+ <button type="button" disabled={!isCustomDomain} className={!isCustomDomain ? 'text-sm font-medium text-primary-600 dark:text-primary-400' : 'text-sm font-medium text-gray-400 dark:text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 cursor-pointer transition-colors'} onClick={toggleToAliasMode}>+ {t(`${tk}.Alias`)}+ </button>+ {required && <span className="text-red-500 ml-1">*</span>}+ </div>+ </div>+ {onRemove && (+ <button type="button" onClick={onRemove} className="w-5 h-5 flex items-center justify-center text-gray-300 hover:text-red-400 dark:text-gray-500 dark:hover:text-red-400 transition-colors" title={t('sharedResources.Delete')}>+ <svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">+ <line x1="18" y1="6" x2="6" y2="18" />+ <line x1="6" y1="6" x2="18" y2="18" />+ </svg>+ </button>+ )}+ </div>++ <div className="relative w-full overflow-visible">+ <div className="flex w-full">+ <input type="text" id={id} className={`outline-0 shadow-sm bg-gray-50 text-gray-900 flex-1 min-w-0 px-3 py-2 border text-sm ${error !== null ? 'border-red-500' : 'border-gray-300 dark:border-gray-600'} ${isCustomDomain ? 'rounded-md' : 'rounded-l-md'} focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:text-white`} value={isCustomDomain ? value : localPart} onChange={e => onInput(e.target.value)} />+ {!isCustomDomain && (+ <button type="button" onClick={() => setIsPopupVisible(v => !v)} className={`inline-flex items-center px-2 py-2 border border-l-0 border-gray-300 dark:border-gray-600 ${onGenerateAlias ? '' : 'rounded-r-md'} bg-gray-50 dark:bg-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-500 cursor-pointer text-sm truncate max-w-[120px]`}>+ <span className="text-gray-500 dark:text-gray-400">@</span>+ <span className="truncate ml-0.5">{selectedDomain}</span>+ </button>+ )}+ {!isCustomDomain && onGenerateAlias && (+ <button type="button" onClick={handleRegenerate} className="px-3 text-gray-500 dark:text-white bg-gray-200 hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium rounded-r-lg text-sm border-l border-gray-300 dark:border-gray-700 dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" title={t('sharedResources.Generate')}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />+ </svg>+ </button>+ )}+ </div>++ {isPopupVisible && !isCustomDomain && (+ <div className="absolute z-50 mt-2 w-full bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 max-h-96 overflow-y-auto">+ <div className="p-4">+ {showPrivateDomains && (+ <div className="mb-4">+ <h4 className="text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">+ {t(`${tk}.PrivateEmailTitle`)} <span className="text-gray-500 dark:text-gray-400">({t(`${tk}.PrivateEmailAliasVaultServer`)})</span>+ </h4>+ <p className="text-xs text-gray-500 dark:text-gray-400 mb-3">{t(`${tk}.PrivateEmailDescription`)}</p>+ <div className="flex flex-wrap gap-2">+ {privateDomains.map(domain => <button key={domain} type="button" onClick={() => selectDomain(domain)} className={domainButtonClasses(domain)}>{domain}</button>)}+ </div>+ </div>+ )}++ <div className={showPrivateDomains ? 'border-t border-gray-200 dark:border-gray-600 pt-4' : ''}>+ <h4 className="text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">{t(`${tk}.PublicEmailTitle`)}</h4>+ <p className="text-xs text-gray-500 dark:text-gray-400 mb-3">{t(`${tk}.PublicEmailDescription`)}</p>+ <div className="flex flex-wrap gap-2">+ {publicDomains.map(domain => <button key={domain} type="button" onClick={() => selectDomain(domain)} className={domainButtonClasses(domain)}>{domain}</button>)}+ </div>+ </div>+ </div>+ </div>+ )}+ </div>++ {error !== null && <p className="text-sm text-red-500 mt-1">{error}</p>}+ </div>++ {isPopupVisible && !isCustomDomain && (+ <div className="fixed inset-0 bg-gray-600 bg-opacity-50 z-40" onClick={() => setIsPopupVisible(false)} style={{ top: 0, left: 0, right: 0, bottom: 0 }}></div>+ )}+ </>+ );+};++export default EmailDomainField;diff --git a/apps/web/src/components/forms/MultiValueFormRow.tsx b/apps/web/src/components/forms/MultiValueFormRow.tsxnew file mode 100644index 000000000..c1337d86b--- /dev/null+++ b/apps/web/src/components/forms/MultiValueFormRow.tsx@@ -0,0 +1,41 @@+import React from 'react';++import { EDIT_INPUT_CLASSES } from '@/components/forms/EditFormRow';++type MultiValueFormRowProps = {+ id: string;+ label: string;+ values: string[];+ onChange: (values: string[]) => void;+ onFocus?: (index: number, event: React.FocusEvent<HTMLInputElement>) => void;+ placeholder?: string;+};++/**+ * A list of text inputs for a multi-value field, with a plus button on the last one.+ */+const MultiValueFormRow: React.FC<MultiValueFormRowProps> = ({ id, label, values, onChange, onFocus, placeholder = '' }) => {+ const shown = values.length === 0 ? [''] : values;++ return (+ <>+ <label htmlFor={id} className="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{label}</label>+ <div className="space-y-2">+ {shown.map((value, index) => (+ <div key={index} className="relative">+ <input type="text" id={`${id}-${index}`} autoComplete="off" className={EDIT_INPUT_CLASSES} value={value} onChange={e => onChange(shown.map((v, i) => i === index ? e.target.value : v))} onFocus={e => onFocus?.(index, e)} placeholder={placeholder} autoCapitalize="off" autoCorrect="off" />+ {index === shown.length - 1 && (+ <button type="button" id={`add-${id}`} onClick={() => onChange([...shown, ''])} className="absolute right-2 top-1/2 -translate-y-1/2 w-6 h-6 flex items-center justify-center text-gray-400 hover:text-primary-500 dark:hover:text-primary-400 transition-colors">+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6" />+ </svg>+ </button>+ )}+ </div>+ ))}+ </div>+ </>+ );+};++export default MultiValueFormRow;diff --git a/apps/web/src/components/forms/RemovableSection.tsx b/apps/web/src/components/forms/RemovableSection.tsxnew file mode 100644index 000000000..dc9ad51bd--- /dev/null+++ b/apps/web/src/components/forms/RemovableSection.tsx@@ -0,0 +1,34 @@+import React from 'react';+import { useTranslation } from 'react-i18next';++type RemovableSectionProps = {+ title?: string;+ canRemove: boolean;+ onRemove: () => void;+ children: React.ReactNode;+};++/**+ * A card section with an optional remove button in the corner.+ */+const RemovableSection: React.FC<RemovableSectionProps> = ({ title = '', canRemove, onRemove, children }) => {+ const { t } = useTranslation();++ return (+ <div className="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800 relative">+ {title.length > 0 && <h3 className="mb-4 text-xl font-semibold dark:text-white">{title}</h3>}+ <div className="grid gap-6">+ {children}+ </div>+ {canRemove && (+ <button type="button" onClick={onRemove} className="absolute top-3 right-3 text-gray-400 hover:text-red-500 transition-colors" title={t('sharedResources.Delete')}>+ <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12" />+ </svg>+ </button>+ )}+ </div>+ );+};++export default RemovableSection;