Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Make browser extension current site suggestion consistent with actual search results (#2353)
Leendert de Borstc4498ef62efa
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsxindex 802b21fa4..b3902b6b1 100644--- a/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsx+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/CurrentSiteSuggestion.tsx@@ -1,14 +1,16 @@-import React, { useEffect, useState } from 'react';+import React, { useEffect, useMemo, useState } from 'react';import { useTranslation } from 'react-i18next';import { useNavigate } from 'react-router-dom';-import useCurrentTabMatching, { type CurrentTabMatchResult } from '@/entrypoints/popup/hooks/useCurrentTabMatching';+import useCurrentTabInfo from '@/entrypoints/popup/hooks/useCurrentTabInfo';import type { Item } from '@/utils/dist/core/models/vault';+import { applySearchFilter } from '@/utils/ItemFilters';import ItemIcon from './ItemIcon';type CurrentSiteSuggestionProps = {+ items: Item[];onSearch: (domain: string) => void;};@@ -16,36 +18,40 @@ type CurrentSiteSuggestionProps = {* CurrentSiteSuggestion** Shows a suggestion for the current browser tab's matching item(s) as a shortcut at the top of the- * items index.+ * items index. Matching uses the same search filter as the search field, so clicking through always+ * lands on exactly the items counted here.*/-const CurrentSiteSuggestion: React.FC<CurrentSiteSuggestionProps> = ({ onSearch }) => {+const CurrentSiteSuggestion: React.FC<CurrentSiteSuggestionProps> = ({ items, onSearch }) => {const { t } = useTranslation();const navigate = useNavigate();- const { matchCurrentTab } = useCurrentTabMatching();- const [match, setMatch] = useState<CurrentTabMatchResult | null>(null);+ const { getCurrentTabInfo } = useCurrentTabInfo();+ const [domain, setDomain] = useState<string | null>(null);- // Resolve the current tab match once on mount.+ // Resolve the current tab domain once on mount.useEffect(() => {let active = true;- matchCurrentTab().then((result) => {+ getCurrentTabInfo().then((result) => {if (active) {- setMatch(result);+ setDomain(result?.domain ?? null);}});return (): void => {active = false;};- }, [matchCurrentTab]);+ }, [getCurrentTabInfo]);- if (!match || !match.domain) {+ const matches = useMemo(+ () => (domain ? applySearchFilter(items, domain) : []),+ [items, domain]+ );++ if (!domain) {return null;}- const { domain, items } = match;-// Single match: open the item directly on click.- if (items.length === 1) {- const item: Item = items[0];+ if (matches.length === 1) {+ const item: Item = matches[0];return (<buttononClick={() => navigate(`/items/${item.Id}`)}@@ -70,7 +76,7 @@ const CurrentSiteSuggestion: React.FC<CurrentSiteSuggestionProps> = ({ onSearch}// Multiple matches: search the vault for the domain on click.- if (items.length > 1) {+ if (matches.length > 1) {return (<buttononClick={() => onSearch(domain)}@@ -81,7 +87,7 @@ const CurrentSiteSuggestion: React.FC<CurrentSiteSuggestionProps> = ({ onSearch<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 })})+ {domain} ({t('items.numberOfItemMatchesFound', { count: matches.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" />diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useCurrentTabInfo.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useCurrentTabInfo.tsnew file mode 100644index 000000000..bd15b2792--- /dev/null+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useCurrentTabInfo.ts@@ -0,0 +1,53 @@+import { useCallback } from 'react';++import { browser } from '#imports';++/**+ * Details of the current browser tab.+ */+export type CurrentTabInfo = {+ /** Current tab URL */+ currentUrl: string;+ /** Current tab hostname, without a leading `www.` (for display/search) */+ domain: string;+};++/**+ * Hook for reading the current browser tab.+ */+const useCurrentTabInfo = (): {+ getCurrentTabInfo: () => Promise<CurrentTabInfo | null>;+} => {+ /**+ * Get the URL and hostname of the active tab.+ *+ * @returns Promise resolving to the tab info, or null for non-http(s) tabs and parse failures+ */+ const getCurrentTabInfo = useCallback(async (): Promise<CurrentTabInfo | null> => {+ try {+ 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;+ }++ return {+ currentUrl: activeTab.url,+ domain: new URL(activeTab.url).hostname.replace(/^www\./, ''),+ };+ } catch {+ return null;+ }+ }, []);++ return {+ getCurrentTabInfo,+ };+};++export default useCurrentTabInfo;diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsxindex dbe151b7b..d6af30f64 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx@@ -29,7 +29,7 @@ import type { Folder } from '@/utils/db/repositories/FolderRepository';import type { CredentialSortOrder } from '@/utils/db/repositories/SettingsRepository';import type { Item, ItemType } from '@/utils/dist/core/models/vault';import { canHaveSubfolders, getDescendantFolderIds, getFolderPath, getRecursiveItemCount } from '@/utils/FolderUtils';-import { applyTypeFilter, isItemTypeFilter, parseItemFilterType, type ItemFilterType } from '@/utils/ItemFilters';+import { applySearchFilter, applyTypeFilter, isItemTypeFilter, parseItemFilterType, type ItemFilterType } from '@/utils/ItemFilters';import { LocalPreferencesService } from '@/utils/LocalPreferencesService';import { useMinDurationLoading } from '@/hooks/useMinDurationLoading';@@ -655,28 +655,7 @@ const ItemsList: React.FC = () => {const typeFiltered = applyTypeFilter(folderScoped, filterType);- const searchLower = searchTerm.toLowerCase().trim();- if (!searchLower) {- return typeFiltered;- }-- const searchWords = searchLower.split(/\s+/).filter(word => word.length > 0);-- return typeFiltered.filter((item: Item) => {- const searchableFields: string[] = [- item.Name?.toLowerCase() || '',- ];-- item.Fields?.forEach(field => {- const value = Array.isArray(field.Value) ? field.Value.join(' ').toLowerCase() : (field.Value || '').toLowerCase();- searchableFields.push(value);- searchableFields.push(field.Label.toLowerCase());- });-- return searchWords.every(word =>- searchableFields.some(field => field.includes(word))- );- });+ return applySearchFilter(typeFiltered, searchTerm);})();/**@@ -933,7 +912,7 @@ 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 > CURRENT_SITE_SUGGESTION_MIN_ITEMS && !currentFolderId && !searchTerm && filterType === 'all' && (- <CurrentSiteSuggestion onSearch={setSearchTerm} />+ <CurrentSiteSuggestion items={items} onSearch={setSearchTerm} />)}{items.length === 0 ? (diff --git a/apps/browser-extension/src/utils/ItemFilters.ts b/apps/browser-extension/src/utils/ItemFilters.tsindex 883ff1b5f..167175e17 100644--- a/apps/browser-extension/src/utils/ItemFilters.ts+++ b/apps/browser-extension/src/utils/ItemFilters.ts@@ -61,3 +61,33 @@ export function applyTypeFilter(items: Item[], filterType: ItemFilterType): Itemreturn true;});}++/**+ * Apply the free-text search filter to a list of items.+ * Splits the term into words and keeps items where every word appears in the name,+ * a field value or a field label. Shared with the current-site suggestion so the+ * suggested match count always equals what the search field itself returns.+ */+export function applySearchFilter(items: Item[], searchTerm: string): Item[] {+ const searchWords = searchTerm.toLowerCase().trim().split(/\s+/).filter(word => word.length > 0);++ if (searchWords.length === 0) {+ return items;+ }++ return items.filter((item: Item) => {+ const searchableFields: string[] = [+ item.Name?.toLowerCase() ?? '',+ ];++ item.Fields?.forEach(field => {+ const value = Array.isArray(field.Value) ? field.Value.join(' ') : (field.Value ?? '');+ searchableFields.push(value.toLowerCase());+ searchableFields.push(field.Label?.toLowerCase() ?? '');+ });++ return searchWords.every(word =>+ searchableFields.some(field => field.includes(word))+ );+ });+}