Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Show current site matching credentials as suggestion instead of auto-opening (#2204)
Leendert de Borst75a192948f91
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsxnew file mode 100644index 000000000..802b21fa4--- /dev/null+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsx@@ -0,0 +1,97 @@+import React, { useEffect, useState } from 'react';+import { useTranslation } from 'react-i18next';+import { useNavigate } from 'react-router-dom';++import useCurrentTabMatching, { type CurrentTabMatchResult } from '@/entrypoints/popup/hooks/useCurrentTabMatching';++import type { Item } from '@/utils/dist/core/models/vault';++import ItemIcon from './ItemIcon';++type CurrentSiteSuggestionProps = {+ onSearch: (domain: string) => void;+};++/**+ * CurrentSiteSuggestion+ *+ * Shows a suggestion for the current browser tab's matching item(s) as a shortcut at the top of the+ * items index.+ */+const CurrentSiteSuggestion: React.FC<CurrentSiteSuggestionProps> = ({ onSearch }) => {+ const { t } = useTranslation();+ const navigate = useNavigate();+ const { matchCurrentTab } = useCurrentTabMatching();+ const [match, setMatch] = useState<CurrentTabMatchResult | null>(null);++ // Resolve the current tab match once on mount.+ useEffect(() => {+ let active = true;+ matchCurrentTab().then((result) => {+ if (active) {+ setMatch(result);+ }+ });+ return (): void => {+ active = false;+ };+ }, [matchCurrentTab]);++ if (!match || !match.domain) {+ return null;+ }++ const { domain, items } = match;++ // Single match: open the item directly on click.+ if (items.length === 1) {+ const item: Item = items[0];+ return (+ <button+ onClick={() => navigate(`/items/${item.Id}`)}+ className="w-full mb-4 p-2 flex items-center gap-2 rounded-lg border border-orange-300 dark:border-orange-500/40 bg-orange-50 dark:bg-orange-900/20 hover:bg-orange-100 dark:hover:bg-orange-900/30 transition-colors focus:outline-none focus:ring-2 focus:ring-orange-500 text-left"+ >+ <div className="w-8 h-8 flex-shrink-0">+ <ItemIcon item={item} className="w-8 h-8" />+ </div>+ <div className="min-w-0 flex-1">+ <p className="text-xs font-medium text-orange-700 dark:text-orange-300 truncate">+ {t('items.currentSiteMatch', { domain })}+ </p>+ <p className="font-medium text-gray-900 dark:text-white truncate">+ {item.Name || t('items.untitled')}+ </p>+ </div>+ <svg className="w-5 h-5 flex-shrink-0 text-orange-500 dark:text-orange-400" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">+ <polyline points="9 18 15 12 9 6" />+ </svg>+ </button>+ );+ }++ // Multiple matches: search the vault for the domain on click.+ if (items.length > 1) {+ return (+ <button+ onClick={() => onSearch(domain)}+ className="w-full mb-4 p-2 flex items-center gap-2 rounded-lg border border-orange-300 dark:border-orange-500/40 bg-orange-50 dark:bg-orange-900/20 hover:bg-orange-100 dark:hover:bg-orange-900/30 transition-colors focus:outline-none focus:ring-2 focus:ring-orange-500 text-left"+ >+ <svg className="w-5 h-5 flex-shrink-0 text-orange-500 dark:text-orange-400" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">+ <circle cx="11" cy="11" r="8" />+ <line x1="21" y1="21" x2="16.65" y2="16.65" />+ </svg>+ <span className="min-w-0 text-sm flex-1 font-medium text-orange-700 dark:text-orange-300 truncate">+ {domain} ({t('items.numberOfItemMatchesFound', { count: items.length })})+ </span>+ <svg className="w-5 h-5 flex-shrink-0 text-orange-500 dark:text-orange-400" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">+ <polyline points="9 18 15 12 9 6" />+ </svg>+ </button>+ );+ }++ // No match: no suggestion.+ return null;+};++export default CurrentSiteSuggestion;diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/ItemIcon.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/ItemIcon.tsxindex 575095ad9..8328e3598 100644--- a/apps/browser-extension/src/entrypoints/popup/components/Items/ItemIcon.tsx+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/ItemIcon.tsx@@ -84,8 +84,10 @@ const ItemIcon: React.FC<ItemIconProps> = ({ item, className = 'w-8 h-8' }) => {return <SvgIcon svg={getCardIconSvg(cardNumber)} className={className} />;}- // For Login/Alias types, use Logo if available, otherwise placeholder- const logoSrc = item.Logo && item.Logo.length > 0 ? SqliteClient.imgSrcFromBytes(item.Logo) : null;+ /*+ * For Login/Alias types, use Logo if available, otherwise placeholder.+ */+ const logoSrc = item.Logo ? SqliteClient.imgSrcFromBytes(item.Logo) : null;if (logoSrc) {return (diff --git a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsxindex ac2607aac..5b44a2871 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx@@ -45,47 +45,22 @@ const Reinitialize: React.FC = () => {const requiresAuth = isFullyInitialized && (!isLoggedIn || !dbAvailable);/**- * Get the expected navigation path based on URL matching result.+ * Navigate to the items index when the popup opens with a fresh (non-restored) state.*/- const getMatchedPath = useCallback((matchResult: { items: { Id: string }[]; domain: string } | null): string => {- if (matchResult && matchResult.items.length === 1) {- return `/items/${matchResult.items[0].Id}`;- } else if (matchResult && matchResult.items.length > 1) {- /*- * For multiple matches, we navigate to /items with search param,- * but the saved lastPage won't have the param, so we just check against /items- */- return '/items';- } else {- return '/items';- }- }, []);-- /**- * Navigate based on URL matching for the current tab.- */- const navigateWithUrlMatching = useCallback(async (matchResult: { items: { Id: string }[]; domain: string } | null): Promise<void> => {- if (matchResult && matchResult.items.length === 1) {- // Single match - navigate to items first, then to the item (for back button support)- navigate('/items', { replace: true });- navigate(`/items/${matchResult.items[0].Id}`, { replace: false });- } else if (matchResult && matchResult.items.length > 1) {- // Multiple matches - navigate to items list with domain search to help user find the right one- navigate(`/items?search=${encodeURIComponent(matchResult.domain)}`, { replace: true });- } else {- // No matches or matching failed - navigate to items page without search (don't prefill search when there are no matches)- navigate('/items', { replace: true });- }+ const navigateToIndex = useCallback((): void => {+ navigate('/items', { replace: true });}, [navigate]);/*** 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 URL matching result: if user navigated away from matched page, restore their navigation.*/const restoreLastPage = useCallback(async (): Promise<void> => {- // First, run URL matching to see what we would auto-navigate to+ /*+ * Run URL matching so we can detect tab changes (used to decide between+ * restoring the last page vs. showing a fresh index).+ */const matchResult = await matchCurrentTab();- const matchedPath = getMatchedPath(matchResult);const [lastPage, lastVisitTime, savedHistory, lastTabUrl] = await Promise.all([storage.getItem(LAST_VISITED_PAGE_KEY) as Promise<string>,@@ -102,21 +77,18 @@ const Reinitialize: React.FC = () => {const timeSinceLastVisit = Date.now() - lastVisitTime;if (timeSinceLastVisit <= PAGE_MEMORY_DURATION) {/*- * Check if user navigated away from the auto-matched page to a specific different page.- * Use fresh URL matching if:- * - Tab URL has changed (user switched tabs)- * - lastPage matches what URL matching would show AND has no search query (user stayed on auto-matched page)- * - lastPage is /items with no search query (default index page - treat as "home" state)+ * Show a fresh items index if:+ * - Tab URL has changed (user switched tabs), or+ * - lastPage is the default index (/items, no search query) - the "home" state.*- * Restore user's navigation only if they navigated to a specific different page like:- * - Settings, add/edit forms, a different item, folder view, search queries, etc.+ * Otherwise restore the user's navigation, since they had navigated to a+ * specific page (settings, add/edit forms, a particular item, folder view,+ * search queries, etc.) that we want to bring them back to.*/- // Check if it's the default index page (no search query or other params)const lastHistoryEntry = savedHistory?.[savedHistory.length - 1];const hasSearchQuery = lastHistoryEntry?.search && lastHistoryEntry.search.length > 0;- const isOnMatchedPage = lastPage === matchedPath && !hasSearchQuery;const isOnDefaultIndexPage = lastPage === '/items' && !hasSearchQuery;- const shouldUseFreshMatch = hasTabChanged || isOnMatchedPage || isOnDefaultIndexPage;+ const shouldUseFreshMatch = hasTabChanged || isOnDefaultIndexPage;if (!shouldUseFreshMatch) {// Restore user's navigation since they navigated away from auto-matched page@@ -151,9 +123,9 @@ const Reinitialize: React.FC = () => {await storage.setItem(LAST_TAB_URL_KEY, currentTabUrl);}- // Navigate based on URL matching- await navigateWithUrlMatching(matchResult);- }, [navigate, matchCurrentTab, getMatchedPath, navigateWithUrlMatching]);+ // Navigate to the items index: any current-site match is shown as a suggestion there.+ navigateToIndex();+ }, [navigate, matchCurrentTab, navigateToIndex]);/*** Run sync in background. If server has newer vault, useVaultSync will:diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsxindex a4029c200..de85b3921 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx@@ -7,6 +7,7 @@ import FolderBreadcrumb from '@/entrypoints/popup/components/Folders/FolderBreadimport FolderModal from '@/entrypoints/popup/components/Folders/FolderModal';import HeaderButton from '@/entrypoints/popup/components/HeaderButton';import { HeaderIconType } from '@/entrypoints/popup/components/Icons/HeaderIcons';+import CurrentSiteSuggestion from '@/entrypoints/popup/components/Items/CurrentSiteSuggestion';import FolderPill from '@/entrypoints/popup/components/Items/FolderPill';import ItemCard from '@/entrypoints/popup/components/Items/ItemCard';import ItemFilterDropdown from '@/entrypoints/popup/components/Items/ItemFilterDropdown';@@ -866,6 +867,11 @@ const ItemsList: React.FC = () => {<></>)}+ {/* Current-site suggestion: show a suggestion for the current site's matching item(s) to quickly view/open them. */}+ {items.length > 0 && !currentFolderId && !searchTerm && filterType === 'all' && (+ <CurrentSiteSuggestion onSearch={setSearchTerm} />+ )}+{items.length === 0 ? (<><div className="text-gray-500 dark:text-gray-400 space-y-2 mb-10">diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.jsonindex 270f93f76..e11c05e10 100644--- a/apps/browser-extension/src/i18n/locales/en.json+++ b/apps/browser-extension/src/i18n/locales/en.json@@ -204,6 +204,8 @@"folder": "Folder","welcomeTitle": "Welcome to AliasVault!","welcomeDescription": "To use the AliasVault browser extension: navigate to a website and use the AliasVault autofill popup to create a new credential.",+ "currentSiteMatch": "For {{domain}}",+ "numberOfItemMatchesFound": "{{count}} items found","noMatchingItems": "No items match the selected filter.","noMatchingItemsSearch": "No items matching \"{{search}}\"","noMatchingItemsWithFilter": "No {{filter}} items matching \"{{search}}\"",