Skip to content

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

Tweak folder breadcrumb history navigation stack (#1695)

Leendert de Borst012a5c195f02
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Folders/FolderBreadcrumb.tsx b/apps/browser-extension/src/entrypoints/popup/components/Folders/FolderBreadcrumb.tsx
index 43e2392bd..b59391f33 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Folders/FolderBreadcrumb.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Folders/FolderBreadcrumb.tsx
@@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next';
import { useNavigate, useLocation } from 'react-router-dom';
import { useDb } from '@/entrypoints/popup/context/DbContext';
+import { useNavigationHistory } from '@/entrypoints/popup/hooks/useNavigationHistory';
import { getFolderPath, getFolderIdPath } from '@/utils/folderUtils';
@@ -43,6 +44,7 @@ const FolderBreadcrumb: React.FC<FolderBreadcrumbProps> = ({
const navigate = useNavigate();
const location = useLocation();
const dbContext = useDb();
+ const { findStepsBack } = useNavigationHistory();
/**
* Compute breadcrumb trail based on current folder.
@@ -73,30 +75,20 @@ const FolderBreadcrumb: React.FC<FolderBreadcrumbProps> = ({
}, [folderId, dbContext?.sqliteClient, location.pathname]);
/**
- * Handle breadcrumb navigation.
- * Uses navigate with negative delta to go back in history when clicking a parent folder.
- * This properly cleans up the history stack instead of adding duplicate entries.
+ * Handle breadcrumb navigation with history management.
*/
- const handleBreadcrumbClick = useCallback((targetPath: string, targetIndex: number) => {
- /*
- * Calculate how many steps back we need to go.
- * targetIndex is the position in the breadcrumb array (0 = root, 1 = first folder, etc.)
- * Current location is at (breadcrumbs.length) in the hierarchy.
- *
- * If we're on folder hierarchy: Root > A > B > C
- * - Breadcrumbs shown: [Root, A, B] (C is current, not shown)
- * - Click "A" (index 1): need to go back (breadcrumbs.length - targetIndex) = 2 steps
- */
- const stepsBack = breadcrumbs.length - targetIndex;
+ const handleBreadcrumbClick = useCallback((targetPath: string) => {
+ // Check if the target path exists in our navigation history
+ const stepsBack = findStepsBack(targetPath);
- if (stepsBack > 0) {
- // Go back in history to clean up the stack
+ if (stepsBack !== null && stepsBack > 0) {
+ // Path is in history - go back to it, removing all entries after it
navigate(-stepsBack);
} else {
- // Fallback to direct navigation with replace
- navigate(targetPath, { replace: true });
+ // Path not in history - navigate forward, adding it to the stack
+ navigate(targetPath);
}
- }, [navigate, breadcrumbs.length]);
+ }, [navigate, findStepsBack]);
/*
* Check if we're currently on the root items page.
@@ -126,7 +118,7 @@ const FolderBreadcrumb: React.FC<FolderBreadcrumbProps> = ({
</span>
) : (
<button
- onClick={() => handleBreadcrumbClick(rootPath, -1)}
+ onClick={() => handleBreadcrumbClick(rootPath)}
className="hover:text-orange-600 dark:hover:text-orange-400 transition-colors flex-shrink-0 flex items-center gap-1"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
@@ -135,7 +127,7 @@ const FolderBreadcrumb: React.FC<FolderBreadcrumbProps> = ({
{rootLabelText}
</button>
)}
- {breadcrumbs.map((crumb, index) => {
+ {breadcrumbs.map((crumb) => {
const crumbPath = `/items/folder/${crumb.id}`;
return (
@@ -144,7 +136,7 @@ const FolderBreadcrumb: React.FC<FolderBreadcrumbProps> = ({
<polyline points="9 6 15 12 9 18" />
</svg>
<button
- onClick={() => handleBreadcrumbClick(crumbPath, index)}
+ onClick={() => handleBreadcrumbClick(crumbPath)}
className="hover:text-orange-600 dark:hover:text-orange-400 transition-colors truncate"
title={crumb.name}
>
diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useNavigationHistory.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useNavigationHistory.ts
new file mode 100644
index 000000000..970041339
--- /dev/null
+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useNavigationHistory.ts
@@ -0,0 +1,55 @@
+import { useEffect, useRef } from 'react';
+import { useLocation } from 'react-router-dom';
+
+/**
+ * Return type for the useNavigationHistory hook.
+ */
+interface INavigationHistory {
+ /**
+ * Find how many steps back we need to go to reach the target path.
+ * Returns null if the path is not in the history stack.
+ */
+ findStepsBack: (targetPath: string) => number | null;
+}
+
+/**
+ * Custom hook to track navigation history within the React app.
+ * This allows us to determine if a path exists in the current navigation stack
+ * and calculate how many steps back we need to go.
+ */
+export const useNavigationHistory = (): INavigationHistory => {
+ const location = useLocation();
+ const historyRef = useRef<string[]>([]);
+
+ useEffect(() => {
+ const currentPath = location.pathname + location.search;
+
+ // Check if this is a back/forward navigation by looking for the path in history
+ const existingIndex = historyRef.current.indexOf(currentPath);
+
+ if (existingIndex !== -1) {
+ // User navigated back - trim history to this point
+ historyRef.current = historyRef.current.slice(0, existingIndex + 1);
+ } else {
+ // New navigation - add to history
+ historyRef.current.push(currentPath);
+ }
+ }, [location]);
+
+ /**
+ * Find how many steps back we need to go to reach the target path.
+ * Returns null if the path is not in the history stack.
+ */
+ const findStepsBack = (targetPath: string): number | null => {
+ const currentIndex = historyRef.current.length - 1;
+ const targetIndex = historyRef.current.lastIndexOf(targetPath);
+
+ if (targetIndex === -1 || targetIndex === currentIndex) {
+ return null; // Path not in history or is current page
+ }
+
+ return currentIndex - targetIndex;
+ };
+
+ return { findStepsBack };
+};
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 e16ef5f2e..450842fab 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
@@ -627,7 +627,7 @@ const ItemsList: React.FC = () => {
return (
<div>
{/* Breadcrumb navigation - only show when inside a folder */}
- <FolderBreadcrumb folderId={currentFolderId} refreshKey={folderRefreshKey} />
+ <FolderBreadcrumb folderId={currentFolderId} />
<div className="flex justify-between items-center gap-2 mb-4">
<div className="relative min-w-0 flex-1 flex items-center gap-2">