Skip to content

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

Refactor field history to only show when stored history record(s) are different than current value

Leendert de Borst0b19e92b0d1b
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldBlock.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldBlock.tsx
index 40e1dd3d8..ae59f8516 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldBlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldBlock.tsx
@@ -1,3 +1,4 @@
+import { previousFieldValues } from '@aliasvault/client/utilities/FieldHistory';
import { FieldTypes } from '@aliasvault/models/vault';
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
@@ -113,24 +114,7 @@ const FieldBlock: React.FC<FieldBlockProps> = ({ field, itemId, manifestId, hide
try {
const history = dbContext.sqliteClient.items.getFieldHistory({ Id: itemId, ManifestId: manifestId }, field.FieldKey);
- if (history.length > 1) {
- // Multiple history records - always show icon
- setHistoryCount(history.length);
- } else if (history.length === 1) {
- // Single history record - check if value differs from current
- const currentValues = Array.isArray(field.Value) ? field.Value : [field.Value];
- const currentValueJson = JSON.stringify(currentValues.filter(v => v && v.trim() !== ''));
- const historyValueJson = history[0].ValueSnapshot;
-
- // Only show icon if history value differs from current value
- if (currentValueJson !== historyValueJson) {
- setHistoryCount(1);
- } else {
- setHistoryCount(0);
- }
- } else {
- setHistoryCount(0);
- }
+ setHistoryCount(previousFieldValues(history, field.Value).length);
} catch (error) {
logFailure('[FieldBlock] Error checking history', error);
}
@@ -186,6 +170,7 @@ const FieldBlock: React.FC<FieldBlockProps> = ({ field, itemId, manifestId, hide
manifestId={manifestId}
fieldKey={field.FieldKey}
fieldLabel={label}
+ currentValue={field.Value}
fieldType={field.FieldType}
isHidden={field.IsHidden}
/>
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldHistoryModal.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldHistoryModal.tsx
index 46c0065aa..f367f143f 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldHistoryModal.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/FieldHistoryModal.tsx
@@ -1,3 +1,4 @@
+import { parseValueSnapshot, previousFieldValues } from '@aliasvault/client/utilities/FieldHistory';
import { FieldTypes } from '@aliasvault/models/vault';
import React, { useState, useEffect, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
@@ -19,6 +20,7 @@ type FieldHistoryModalProps = {
manifestId: string;
fieldKey: string;
fieldLabel: string;
+ currentValue: string | string[];
fieldType: FieldType;
isHidden: boolean;
}
@@ -36,6 +38,7 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({
manifestId,
fieldKey,
fieldLabel,
+ currentValue,
fieldType,
isHidden
}) => {
@@ -60,13 +63,13 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({
try {
setLoading(true);
const historyRecords = dbContext.sqliteClient.items.getFieldHistory({ Id: itemId, ManifestId: manifestId }, fieldKey);
- setHistory(historyRecords);
+ setHistory(previousFieldValues(historyRecords, currentValue));
} catch (error) {
logFailure('Error loading field history', error);
} finally {
setLoading(false);
}
- }, [dbContext?.sqliteClient, itemId, manifestId, fieldKey]);
+ }, [dbContext?.sqliteClient, itemId, manifestId, fieldKey, currentValue]);
useEffect(() => {
if (!isOpen) {
@@ -93,17 +96,6 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({
}
};
- /**
- * Parse a value snapshot into an array of values.
- */
- const parseValueSnapshot = (snapshot: string): string[] => {
- try {
- return JSON.parse(snapshot);
- } catch {
- return [snapshot];
- }
- };
-
/**
* Handle delete of a history record.
*/
diff --git a/apps/mobile-app/components/items/FieldHistoryModal.tsx b/apps/mobile-app/components/items/FieldHistoryModal.tsx
index a5d23b645..be53b2763 100644
--- a/apps/mobile-app/components/items/FieldHistoryModal.tsx
+++ b/apps/mobile-app/components/items/FieldHistoryModal.tsx
@@ -1,3 +1,4 @@
+import { parseValueSnapshot, previousFieldValues } from '@aliasvault/client/utilities/FieldHistory';
import { MaterialIcons } from '@expo/vector-icons';
import React, { useState, useEffect, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
@@ -28,6 +29,7 @@ type FieldHistoryModalProps = {
item: ItemRef;
fieldKey: string;
fieldLabel: string;
+ currentValue: string | string[];
fieldType: FieldType;
isHidden: boolean;
}
@@ -44,6 +46,7 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({
item,
fieldKey,
fieldLabel,
+ currentValue,
fieldType,
isHidden
}) => {
@@ -65,13 +68,13 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({
try {
setLoading(true);
const historyRecords = await dbContext.sqliteClient.items.getFieldHistory({ Id: item.Id, ManifestId: item.ManifestId }, fieldKey);
- setHistory(historyRecords);
+ setHistory(previousFieldValues(historyRecords, currentValue));
} catch (error) {
console.error('Error loading field history:', error);
} finally {
setLoading(false);
}
- }, [dbContext?.sqliteClient, item.Id, item.ManifestId, fieldKey]);
+ }, [dbContext?.sqliteClient, item.Id, item.ManifestId, fieldKey, currentValue]);
useEffect(() => {
if (!isOpen) {
@@ -98,17 +101,6 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({
}
};
- /**
- * Parse a value snapshot into an array of values.
- */
- const parseValueSnapshot = (snapshot: string): string[] => {
- try {
- return JSON.parse(snapshot) as string[];
- } catch {
- return [snapshot];
- }
- };
-
/**
* Handle delete of a history record.
*/
diff --git a/apps/mobile-app/components/items/details/FieldBlock.tsx b/apps/mobile-app/components/items/details/FieldBlock.tsx
index a0c5251a8..4463d09e7 100644
--- a/apps/mobile-app/components/items/details/FieldBlock.tsx
+++ b/apps/mobile-app/components/items/details/FieldBlock.tsx
@@ -3,6 +3,7 @@ import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { View, Text, StyleSheet, TouchableOpacity, Linking } from 'react-native';
+import { previousFieldValues } from '@aliasvault/client/utilities/FieldHistory';
import type { ItemRef } from '@aliasvault/client/database/ItemRef';
import type { ItemField } from '@aliasvault/models/vault';
import { FieldTypes } from '@aliasvault/models/vault';
@@ -72,24 +73,7 @@ const FieldBlock: React.FC<FieldBlockProps> = ({ field, item, hideLabel = false
try {
const history = await dbContext.sqliteClient.items.getFieldHistory({ Id: itemId, ManifestId: itemManifestId }, field.FieldKey);
- if (history.length > 1) {
- // Multiple history records - always show icon
- setHistoryCount(history.length);
- } else if (history.length === 1) {
- // Single history record - check if value differs from current
- const currentValues = Array.isArray(field.Value) ? field.Value : [field.Value];
- const currentValueJson = JSON.stringify(currentValues.filter(v => v && v.trim() !== ''));
- const historyValueJson = history[0].ValueSnapshot;
-
- // Only show icon if history value differs from current value
- if (currentValueJson !== historyValueJson) {
- setHistoryCount(1);
- } else {
- setHistoryCount(0);
- }
- } else {
- setHistoryCount(0);
- }
+ setHistoryCount(previousFieldValues(history, field.Value).length);
} catch (error) {
console.error('[FieldBlock] Error checking history:', error);
}
@@ -248,6 +232,7 @@ const FieldBlock: React.FC<FieldBlockProps> = ({ field, item, hideLabel = false
item={{ Id: itemId, ManifestId: itemManifestId }}
fieldKey={field.FieldKey}
fieldLabel={label}
+ currentValue={field.Value}
fieldType={field.FieldType}
isHidden={field.IsHidden}
/>
diff --git a/apps/web/src/components/items/FieldBlock.tsx b/apps/web/src/components/items/FieldBlock.tsx
index 79555ad67..e21be16d1 100644
--- a/apps/web/src/components/items/FieldBlock.tsx
+++ b/apps/web/src/components/items/FieldBlock.tsx
@@ -1,3 +1,4 @@
+import { previousFieldValues } from '@aliasvault/client/utilities/FieldHistory';
import { FieldTypes } from '@aliasvault/models/vault';
import React, { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -84,13 +85,7 @@ const FieldBlock: React.FC<FieldBlockProps> = ({ field, item, fullWidth = false,
return;
}
const records = dbContext.sqliteClient.items.getFieldHistory({ Id: item.Id, ManifestId: item.ManifestId }, field.FieldKey);
- if (records.length > 1) {
- setHistoryCount(records.length);
- } else if (records.length === 1) {
- setHistoryCount(records[0].ValueSnapshot !== JSON.stringify([field.Value].filter(v => v.trim().length > 0)) ? 1 : 0);
- } else {
- setHistoryCount(0);
- }
+ setHistoryCount(previousFieldValues(records, field.Value).length);
}, [dbContext.sqliteClient, field.EnableHistory, field.FieldKey, field.Value, item.Id, item.ManifestId, showHistoryModal]);
const labelElement = (
@@ -149,7 +144,7 @@ const FieldBlock: React.FC<FieldBlockProps> = ({ field, item, fullWidth = false,
<>
{renderField()}
{showHistoryModal && (
- <FieldHistoryModal item={item} fieldKey={field.FieldKey} fieldLabel={label} isHidden={field.FieldType === FieldTypes.Password || field.FieldType === FieldTypes.Hidden} onClose={() => setShowHistoryModal(false)} />
+ <FieldHistoryModal item={item} fieldKey={field.FieldKey} fieldLabel={label} currentValue={field.Value} isHidden={field.FieldType === FieldTypes.Password || field.FieldType === FieldTypes.Hidden} onClose={() => setShowHistoryModal(false)} />
)}
</>
);
diff --git a/apps/web/src/components/items/FieldHistoryModal.tsx b/apps/web/src/components/items/FieldHistoryModal.tsx
index 690bb9074..d15766e57 100644
--- a/apps/web/src/components/items/FieldHistoryModal.tsx
+++ b/apps/web/src/components/items/FieldHistoryModal.tsx
@@ -1,3 +1,4 @@
+import { parseValueSnapshot, previousFieldValues } from '@aliasvault/client/utilities/FieldHistory';
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -16,22 +17,11 @@ type FieldHistoryModalProps = {
item: ItemRef;
fieldKey: string;
fieldLabel: string;
+ currentValue: string | string[];
isHidden: boolean;
onClose: () => void;
};
-/**
- * The values in a history snapshot, or the raw snapshot when it is not a JSON list.
- */
-const parseValueSnapshot = (snapshot: string): string[] => {
- try {
- const values: unknown = JSON.parse(snapshot);
- return Array.isArray(values) ? values.map(String) : [snapshot];
- } catch {
- return [snapshot];
- }
-};
-
/**
* "MMM d, yyyy h:mm tt".
*/
@@ -40,7 +30,7 @@ const formatDate = (value: string): string => new Date(value).toLocaleString('en
/**
* Modal listing the past values of a field.
*/
-const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({ item, fieldKey, fieldLabel, isHidden, onClose }) => {
+const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({ item, fieldKey, fieldLabel, currentValue, isHidden, onClose }) => {
const { t } = useTranslation();
const dbContext = useDb();
const { executeVaultMutationInBackground } = useVaultMutate();
@@ -53,9 +43,9 @@ const FieldHistoryModal: React.FC<FieldHistoryModalProps> = ({ item, fieldKey, f
*/
const loadHistory = useCallback((): void => {
setIsLoading(true);
- setRecords(dbContext.sqliteClient?.items.getFieldHistory({ Id: item.Id, ManifestId: item.ManifestId }, fieldKey) ?? []);
+ setRecords(previousFieldValues(dbContext.sqliteClient?.items.getFieldHistory({ Id: item.Id, ManifestId: item.ManifestId }, fieldKey) ?? [], currentValue));
setIsLoading(false);
- }, [dbContext.sqliteClient, fieldKey, item.Id, item.ManifestId]);
+ }, [dbContext.sqliteClient, fieldKey, item.Id, item.ManifestId, currentValue]);
useEffect(() => loadHistory(), [loadHistory]);
diff --git a/core/client/src/utilities/FieldHistory.ts b/core/client/src/utilities/FieldHistory.ts
new file mode 100644
index 000000000..bd60e99cf
--- /dev/null
+++ b/core/client/src/utilities/FieldHistory.ts
@@ -0,0 +1,51 @@
+/**
+ * Field history value helpers.
+ */
+
+/**
+ * The values in a `FieldHistories.ValueSnapshot`: a JSON list of strings, or the raw snapshot when it is not one.
+ * @param snapshot - the stored snapshot
+ * @returns The snapshot's values
+ */
+export function parseValueSnapshot(snapshot: string): string[] {
+ try {
+ const parsed: unknown = JSON.parse(snapshot);
+ if (Array.isArray(parsed)) {
+ return parsed.filter(value => value !== null && value !== undefined).map(String);
+ }
+ return typeof parsed === 'string' ? [parsed] : [snapshot];
+ } catch {
+ return [snapshot];
+ }
+}
+
+/**
+ * Whether a history snapshot holds exactly the given field value.
+ * @param snapshot - the stored snapshot
+ * @param value - the current field value
+ * @returns True when both hold the same values in the same order
+ */
+export function snapshotEqualsValue(snapshot: string, value: string | string[] | null | undefined): boolean {
+ const snapshotValues = withoutEmpty(parseValueSnapshot(snapshot));
+ const currentValues = withoutEmpty(Array.isArray(value) ? value : [value]);
+ return snapshotValues.length === currentValues.length && snapshotValues.every((entry, index) => entry === currentValues[index]);
+}
+
+/**
+ * The previous values of a field: its history (newest first) without the newest record when that holds the current value.
+ * @param records - the field's history records, newest first
+ * @param value - the current field value
+ * @returns The records to show as history
+ */
+export function previousFieldValues<T extends { ValueSnapshot: string }>(records: ReadonlyArray<T>, value: string | string[] | null | undefined): T[] {
+ return records.length > 0 && snapshotEqualsValue(records[0].ValueSnapshot, value) ? records.slice(1) : [...records];
+}
+
+/**
+ * The values that are not empty or whitespace-only.
+ * @param values - the values to filter
+ * @returns The non-empty values
+ */
+function withoutEmpty(values: ReadonlyArray<string | null | undefined>): string[] {
+ return values.filter((value): value is string => typeof value === 'string' && value.trim() !== '');
+}