Skip to content

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

Refactor popup navigation history to use new shared service (#2312)

Leendert de Borst507c78d4f803
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index eff81d9b9..c89a67549 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -9,6 +9,7 @@ import { FieldKey, ItemTypes, createSystemField, type Item, type PasswordSetting
import type { Vault, VaultResponse, VaultPostResponse } from '@/utils/dist/core/models/webapi';
import { EncryptionUtility } from '@/utils/EncryptionUtility';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
+import { NavigationStateService } from '@/utils/NavigationStateService';
import { RecentlySelectedItemService } from '@/utils/RecentlySelectedItemService';
import { filterItems, AutofillMatchingMode, extractRootDomain, isUrlAlreadyLinked, generatePassword } from '@/utils/RustCore';
import { ServiceDetectionUtility } from '@/utils/serviceDetection/ServiceDetectionUtility';
@@ -294,10 +295,8 @@ export async function handleClearSession(): Promise<messageBoolResponse> {
await storage.removeItems([
'session:encryptionKey',
'session:persistedFormValues',
- 'session:lastVisitedPage',
- 'session:lastVisitedTime',
- 'session:navigationHistory',
]);
+ await NavigationStateService.clearNavigationState();
// Reset password unlock failed attempts counter on logout
await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
diff --git a/apps/browser-extension/src/entrypoints/popup/context/NavigationContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/NavigationContext.tsx
index 9730d888d..1a504f0b9 100644
--- a/apps/browser-extension/src/entrypoints/popup/context/NavigationContext.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/context/NavigationContext.tsx
@@ -5,17 +5,8 @@ import { useApp } from '@/entrypoints/popup/context/AppContext';
import { useDb } from '@/entrypoints/popup/context/DbContext';
import { AUTH_FLOW_PATHS } from '@/entrypoints/popup/utils/routes';
-import { storage } from '#imports';
-
-const LAST_VISITED_PAGE_KEY = 'session:lastVisitedPage';
-const LAST_VISITED_TIME_KEY = 'session:lastVisitedTime';
-const NAVIGATION_HISTORY_KEY = 'session:navigationHistory';
-
-type NavigationHistoryEntry = {
- pathname: string;
- search: string;
- hash: string;
-};
+import type { NavigationHistoryEntry } from '@/utils/NavigationStateService';
+import { NavigationStateService } from '@/utils/NavigationStateService';
type NavigationContextType = {
storeCurrentPage: () => Promise<void>;
@@ -75,11 +66,7 @@ export const NavigationProvider: React.FC<{ children: React.ReactNode }> = ({ ch
});
}
- await Promise.all([
- storage.setItem(LAST_VISITED_PAGE_KEY, location.pathname),
- storage.setItem(LAST_VISITED_TIME_KEY, Date.now()),
- storage.setItem(NAVIGATION_HISTORY_KEY, historyEntries),
- ]);
+ await NavigationStateService.storeNavigationState(location.pathname, historyEntries);
}
}, [location, isFullyInitialized, requiresAuth]);
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
index 5b44a2871..1db3b1fc2 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
@@ -9,21 +9,10 @@ import { consumePendingRedirectUrl } from '@/entrypoints/popup/hooks/useVaultLoc
import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
+import { NavigationStateService } from '@/utils/NavigationStateService';
-import { storage } from '#imports';
-
-const LAST_VISITED_PAGE_KEY = 'session:lastVisitedPage';
-const LAST_VISITED_TIME_KEY = 'session:lastVisitedTime';
-const NAVIGATION_HISTORY_KEY = 'session:navigationHistory';
-const LAST_TAB_URL_KEY = 'session:lastTabUrl';
const PAGE_MEMORY_DURATION = 120 * 1000; // 2 minutes in milliseconds
-type NavigationHistoryEntry = {
- pathname: string;
- search: string;
- hash: string;
-};
-
/**
* Initialize component that handles initial application setup, authentication checks,
* vault synchronization, and state restoration.
@@ -62,11 +51,9 @@ const Reinitialize: React.FC = () => {
*/
const matchResult = await matchCurrentTab();
- const [lastPage, lastVisitTime, savedHistory, lastTabUrl] = await Promise.all([
- storage.getItem(LAST_VISITED_PAGE_KEY) as Promise<string>,
- storage.getItem(LAST_VISITED_TIME_KEY) as Promise<number>,
- storage.getItem(NAVIGATION_HISTORY_KEY) as Promise<NavigationHistoryEntry[]>,
- storage.getItem(LAST_TAB_URL_KEY) as Promise<string>,
+ const [{ lastPage, lastVisitTime, history: savedHistory }, lastTabUrl] = await Promise.all([
+ NavigationStateService.getNavigationState(),
+ NavigationStateService.getLastTabUrl(),
]);
// Check if user switched to a different tab (different URL)
@@ -92,7 +79,7 @@ const Reinitialize: React.FC = () => {
if (!shouldUseFreshMatch) {
// Restore user's navigation since they navigated away from auto-matched page
- if (savedHistory?.length > 1) {
+ if (savedHistory && savedHistory.length > 1) {
// Navigate to the base route first
const firstEntry = savedHistory[0];
const firstPath = firstEntry.pathname + (firstEntry.search || '');
@@ -112,15 +99,13 @@ const Reinitialize: React.FC = () => {
// Clear stored navigation data since we're using fresh URL matching
await Promise.all([
- storage.removeItem(LAST_VISITED_PAGE_KEY),
- storage.removeItem(LAST_VISITED_TIME_KEY),
- storage.removeItem(NAVIGATION_HISTORY_KEY),
+ NavigationStateService.clearNavigationState(),
sendMessage('CLEAR_PERSISTED_FORM_VALUES'),
]);
// Save current tab URL for future tab-switch detection
if (currentTabUrl) {
- await storage.setItem(LAST_TAB_URL_KEY, currentTabUrl);
+ await NavigationStateService.setLastTabUrl(currentTabUrl);
}
// Navigate to the items index: any current-site match is shown as a suggestion there.
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemAddEdit.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemAddEdit.tsx
index 72d1772ac..60cd536bc 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemAddEdit.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemAddEdit.tsx
@@ -34,6 +34,7 @@ import { FieldCategories, FieldTypes, ItemTypes, getSystemFieldsForItemType, get
import { FaviconService } from '@/utils/FaviconService';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
+import { NavigationStateService } from '@/utils/NavigationStateService';
import * as RustCore from '@/utils/RustCore';
import { browser } from '#imports';
@@ -679,6 +680,16 @@ const ItemAddEdit: React.FC = () => {
console.error('Error autofilling created item into page:', err);
}
+ /*
+ * Drop the navigation state persisted by this window (NavigationContext stored '/items/add') so the
+ * next regular popup open starts fresh instead of restoring the completed create page.
+ */
+ try {
+ await NavigationStateService.clearNavigationState();
+ } catch (err) {
+ console.error('Error clearing persisted navigation state:', err);
+ }
+
// Close this popup window (it was opened as a standalone window from the content script).
try {
const currentWindow = await browser.windows.getCurrent();