Skip to content

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

Refactor existing calls to useCurrentTabMatching (#2353)

Leendert de Borst97d5d7a9e5ed
diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useCurrentTabMatching.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useCurrentTabMatching.ts
deleted file mode 100644
index 1870142dd..000000000
--- a/apps/browser-extension/src/entrypoints/popup/hooks/useCurrentTabMatching.ts
+++ /dev/null
@@ -1,86 +0,0 @@
-import { useCallback } from 'react';
-
-import type { Item } from '@/utils/dist/core/models/vault';
-import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
-import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
-
-import { browser } from '#imports';
-
-/**
- * Result of current tab matching.
- */
-export type CurrentTabMatchResult = {
- /** Matched items for the current tab */
- items: Item[];
- /** Current tab URL (for prefilling search) */
- currentUrl: string;
- /** Current tab domain (for display/search) */
- domain: string;
-};
-
-/**
- * Hook for matching vault items against the current browser tab.
- */
-const useCurrentTabMatching = (): {
- matchCurrentTab: () => Promise<CurrentTabMatchResult | null>;
-} => {
- /**
- * Match vault items against the current browser tab.
- *
- * @returns Promise resolving to match result, or null if matching fails
- */
- const matchCurrentTab = useCallback(async (): Promise<CurrentTabMatchResult | null> => {
- try {
- // Get the current active tab
- const [activeTab] = await browser.tabs.query({ active: true, currentWindow: true });
-
- if (!activeTab?.url) {
- return null;
- }
-
- // Skip non-http(s) URLs (like chrome://, about:, etc.)
- if (!activeTab.url.startsWith('http://') && !activeTab.url.startsWith('https://')) {
- return null;
- }
-
- // Extract domain for search prefill
- let domain = '';
- try {
- const url = new URL(activeTab.url);
- domain = url.hostname.replace(/^www\./, '');
- } catch {
- return null;
- }
-
- // Get autofill matching mode from user settings
- const matchingMode = await LocalPreferencesService.getAutofillMatchingMode();
-
- // Use the same filtering logic as content script (without recently selected prioritization)
- const response = await sendMessage('GET_FILTERED_ITEMS', {
- currentUrl: activeTab.url,
- pageTitle: activeTab.title || '',
- matchingMode: matchingMode
- // includeRecentlySelected defaults to false (recently selected is for autofill only)
- });
-
- if (!response.success || !response.items) {
- return null;
- }
-
- return {
- items: response.items,
- currentUrl: activeTab.url,
- domain: domain
- };
- } catch (error) {
- console.error('Error matching current tab:', error);
- return null;
- }
- }, []);
-
- return {
- matchCurrentTab,
- };
-};
-
-export default useCurrentTabMatching;
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
index 1db3b1fc2..43f8007c6 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
@@ -4,7 +4,7 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { useApp } from '@/entrypoints/popup/context/AppContext';
import { useDb } from '@/entrypoints/popup/context/DbContext';
import { useLoading } from '@/entrypoints/popup/context/LoadingContext';
-import useCurrentTabMatching from '@/entrypoints/popup/hooks/useCurrentTabMatching';
+import useCurrentTabInfo from '@/entrypoints/popup/hooks/useCurrentTabInfo';
import { consumePendingRedirectUrl } from '@/entrypoints/popup/hooks/useVaultLockRedirect';
import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';
@@ -22,7 +22,7 @@ const Reinitialize: React.FC = () => {
const location = useLocation();
const { setIsInitialLoading } = useLoading();
const { syncVault } = useVaultSync();
- const { matchCurrentTab } = useCurrentTabMatching();
+ const { getCurrentTabInfo } = useCurrentTabInfo();
const hasInitialized = useRef(false);
// Auth and DB state
@@ -42,14 +42,14 @@ const Reinitialize: React.FC = () => {
/**
* Restore the last visited page and navigation history if it was visited within the memory duration.
- * Compares with URL matching result: if user navigated away from matched page, restore their navigation.
+ * Compares with the current tab URL: if user navigated away from matched page, restore their navigation.
*/
const restoreLastPage = useCallback(async (): Promise<void> => {
/*
- * Run URL matching so we can detect tab changes (used to decide between
+ * Read the current tab so we can detect tab changes (used to decide between
* restoring the last page vs. showing a fresh index).
*/
- const matchResult = await matchCurrentTab();
+ const tabInfo = await getCurrentTabInfo();
const [{ lastPage, lastVisitTime, history: savedHistory }, lastTabUrl] = await Promise.all([
NavigationStateService.getNavigationState(),
@@ -57,7 +57,7 @@ const Reinitialize: React.FC = () => {
]);
// Check if user switched to a different tab (different URL)
- const currentTabUrl = matchResult?.currentUrl;
+ const currentTabUrl = tabInfo?.currentUrl;
const hasTabChanged = currentTabUrl && lastTabUrl && currentTabUrl !== lastTabUrl;
if (lastPage && lastVisitTime) {
@@ -110,7 +110,7 @@ const Reinitialize: React.FC = () => {
// Navigate to the items index: any current-site match is shown as a suggestion there.
navigateToIndex();
- }, [navigate, matchCurrentTab, navigateToIndex]);
+ }, [navigate, getCurrentTabInfo, navigateToIndex]);
/**
* Run sync in background. If server has newer vault, useVaultSync will: