Skip to content

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

Update browser extension to remember search query when pressing back on item details page (#1752)

Leendert de Borstad8f731267f5
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/ItemCard.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/ItemCard.tsx
index 375e9119c..691cb4822 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Items/ItemCard.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/ItemCard.tsx
@@ -10,6 +10,7 @@ import ItemIcon from './ItemIcon';
type ItemCardProps = {
item: Item;
showFolderPath?: boolean;
+ searchTerm?: string;
};
/**
@@ -19,7 +20,7 @@ type ItemCardProps = {
* It allows the user to navigate to the item details page when clicked.
*
*/
-const ItemCard: React.FC<ItemCardProps> = ({ item, showFolderPath = false }) => {
+const ItemCard: React.FC<ItemCardProps> = ({ item, showFolderPath = false, searchTerm = '' }) => {
const { t } = useTranslation();
const navigate = useNavigate();
@@ -66,7 +67,11 @@ const ItemCard: React.FC<ItemCardProps> = ({ item, showFolderPath = false }) =>
return (
<li>
<button
- onClick={() => navigate(`/items/${item.Id}`)}
+ onClick={() => {
+ // Build URL with search query parameter if present
+ const url = searchTerm ? `/items/${item.Id}?returnSearch=${encodeURIComponent(searchTerm)}` : `/items/${item.Id}`;
+ navigate(url);
+ }}
className="w-full p-2 border dark:border-gray-600 rounded flex items-center bg-white dark:bg-gray-800 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<div className="w-8 h-8 mr-2 flex-shrink-0">
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx b/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx
index a5e6d5349..81dc36733 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx
@@ -1,6 +1,6 @@
import React, { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
-import { useNavigate, useLocation } from 'react-router-dom';
+import { useNavigate, useLocation, useSearchParams } from 'react-router-dom';
import Logo from '@/entrypoints/popup/components/Logo';
import { useApp } from '@/entrypoints/popup/context/AppContext';
@@ -31,6 +31,7 @@ const Header: React.FC<HeaderProps> = ({
const app = useApp();
const navigate = useNavigate();
const location = useLocation();
+ const [searchParams] = useSearchParams();
// Updated route matching logic to handle URL parameters
const currentRoute = routes?.find(route => {
@@ -49,13 +50,20 @@ const Header: React.FC<HeaderProps> = ({
// Check if we have a returnTo path (set when opening expanded popup)
const returnPath = PopoutUtility.getReturnPath();
- // If we're in an expanded popup with a returnTo path and no history, use it
- if (returnPath && PopoutUtility.isPopup() && window.history.length <= 2) {
+ // Check if we're on an item details page with a returnSearch param
+ const returnSearch = searchParams.get('returnSearch');
+ const isItemDetailsPage = /^\/items\/[^/]+$/.test(location.pathname);
+
+ if (isItemDetailsPage && returnSearch) {
+ // Navigate back to items list with search query
+ navigate(`/items?search=${encodeURIComponent(returnSearch)}`);
+ } else if (returnPath && PopoutUtility.isPopup() && window.history.length <= 2) {
+ // If we're in an expanded popup with a returnTo path and no history, use it
navigate(returnPath);
} else {
navigate(-1);
}
- }, [navigate]);
+ }, [navigate, searchParams, location.pathname]);
/**
* Handle settings.
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
index 238fec88e..762d7b349 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx
@@ -96,26 +96,33 @@ const Reinitialize: React.FC = () => {
/*
* Check if user navigated away from the auto-matched page to a specific different page.
* Use fresh URL matching if:
- * - lastPage matches what URL matching would show (user stayed on auto-matched page)
- * - lastPage is /items (default index page - treat as "home" state)
+ * - 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)
*
* Restore user's navigation only if they navigated to a specific different page like:
- * - Settings, add/edit forms, a different item, folder view, etc.
+ * - Settings, add/edit forms, a different item, folder view, search queries, etc.
*/
- const isOnMatchedPage = lastPage === matchedPath;
- const isOnDefaultIndexPage = lastPage === '/items';
+ // 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 = isOnMatchedPage || isOnDefaultIndexPage;
if (!shouldUseFreshMatch) {
// Restore user's navigation since they navigated away from auto-matched page
if (savedHistory?.length > 1) {
// Navigate to the base route first
- navigate(savedHistory[0].pathname, { replace: true });
- // Then navigate to the final destination
- navigate(lastPage, { replace: false });
+ const firstEntry = savedHistory[0];
+ const firstPath = firstEntry.pathname + (firstEntry.search || '');
+ navigate(firstPath, { replace: true });
+ // Then navigate to the final destination with search params
+ const finalPath = lastPage + (lastHistoryEntry?.search || '');
+ navigate(finalPath, { replace: false });
} else {
// Simple navigation for non-nested routes
- navigate(lastPage, { replace: true });
+ const fullPath = lastPage + (lastHistoryEntry?.search || '');
+ navigate(fullPath, { replace: true });
}
return;
}
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
index 6663ed66c..6adcc9af6 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
@@ -118,8 +118,10 @@ const ItemsList: React.FC = () => {
const { executeVaultMutationAsync } = useVaultMutate();
const { setHeaderButtons } = useHeaderButtons();
const [items, setItems] = useState<Item[]>([]);
- // Initialize searchTerm from URL query parameter if present
- const [searchTerm, setSearchTerm] = useState(() => searchParams.get('search') || '');
+ // Initialize searchTerm from URL query parameter (restored from back navigation or popup restore)
+ const [searchTerm, setSearchTerm] = useState(() => {
+ return searchParams.get('search') || '';
+ });
const [filterType, setFilterType] = useState<FilterType>(getStoredFilter());
const [showFilterMenu, setShowFilterMenu] = useState(false);
const [showFolderModal, setShowFolderModal] = useState(false);
@@ -158,15 +160,18 @@ const ItemsList: React.FC = () => {
const [isLoading, setIsLoading] = useMinDurationLoading(true, 100);
/**
- * Clear search param from URL after reading it (to prevent it from persisting in browser history).
+ * Update URL when search term changes to persist it in navigation history.
*/
useEffect(() => {
- const searchFromUrl = searchParams.get('search');
- if (searchFromUrl) {
- // Remove the search param from URL while preserving the current path
- navigate(location.pathname, { replace: true });
+ // Build the new URL with or without search param
+ const newUrl = searchTerm ? `${location.pathname}?search=${encodeURIComponent(searchTerm)}` : location.pathname;
+
+ // Only update if the URL actually changed
+ const currentUrl = location.pathname + location.search;
+ if (newUrl !== currentUrl) {
+ navigate(newUrl, { replace: true });
}
- }, []); // eslint-disable-line react-hooks/exhaustive-deps -- Only run once on mount
+ }, [searchTerm, location.pathname, location.search, navigate]);
/**
* Reset search and filter when navigating via the vault tab (with resetFilters state).
@@ -961,6 +966,7 @@ const ItemsList: React.FC = () => {
key={item.Id}
item={item}
showFolderPath={!!searchTerm && !!item.FolderPath}
+ searchTerm={searchTerm}
/>
))}
</ul>