Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
import { applySearchFilter } from '@aliasvault/client/items/ItemFilters';
import React, { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import Icon from '@/entrypoints/popup/components/Icons/Icon';
import useCurrentTabInfo from '@/entrypoints/popup/hooks/useCurrentTabInfo';
import { itemRoute } from '@/utils/ItemRoute';
import ItemIcon from './ItemIcon';
import type { Item } from '@aliasvault/models/vault';
type CurrentSiteSuggestionProps = {
items: Item[];
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. 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> = ({ items, onSearch }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const { getCurrentTabInfo } = useCurrentTabInfo();
const [domain, setDomain] = useState<string | null>(null);
// Resolve the current tab domain once on mount.
useEffect(() => {
let active = true;
getCurrentTabInfo().then((result) => {
if (active) {
setDomain(result?.domain ?? null);
}
});
return (): void => {
active = false;
};
}, [getCurrentTabInfo]);
const matches = useMemo(
() => (domain ? applySearchFilter(items, domain) : []),
[items, domain]
);
if (!domain) {
return null;
}
// Single match: open the item directly on click.
if (matches.length === 1) {
const item: Item = matches[0];
return (
<button
onClick={() => navigate(itemRoute(item))}
className="w-full mb-4 p-2 flex items-center gap-2 rounded-lg border border-primary-300 dark:border-primary-500/40 bg-primary-50 dark:bg-primary-900/20 hover:bg-primary-100 dark:hover:bg-primary-900/30 transition-colors focus:outline-none focus:ring-2 focus:ring-primary-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-primary-700 dark:text-primary-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>
<Icon name="chevron-right" className="w-5 h-5 flex-shrink-0 text-primary-500 dark:text-primary-400" />
</button>
);
}
// Multiple matches: search the vault for the domain on click.
if (matches.length > 1) {
return (
<button
onClick={() => onSearch(domain)}
className="w-full mb-4 p-2 flex items-center gap-2 rounded-lg border border-primary-300 dark:border-primary-500/40 bg-primary-50 dark:bg-primary-900/20 hover:bg-primary-100 dark:hover:bg-primary-900/30 transition-colors focus:outline-none focus:ring-2 focus:ring-primary-500 text-left"
>
<Icon name="search" className="w-5 h-5 flex-shrink-0 text-primary-500 dark:text-primary-400" />
<span className="min-w-0 text-sm flex-1 font-medium text-primary-700 dark:text-primary-300 truncate">
{domain} ({t('items.numberOfItemMatchesFound', { count: matches.length })})
</span>
<Icon name="chevron-right" className="w-5 h-5 flex-shrink-0 text-primary-500 dark:text-primary-400" />
</button>
);
}
// No match: no suggestion.
return null;
};
export default CurrentSiteSuggestion;