Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(transactions): center the bulk action bar when the sidebar is collapsed (#1041)

José Gázquezc61e494f2c4e
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 558746be..8d6bf285 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -7,6 +7,7 @@ import { ThemeProvider } from '@/components/theme-provider'
import { AuthProvider } from '@/contexts/auth-provider'
import { WorkspaceProvider } from '@/contexts/workspace-provider'
import { CollectionFilterProvider } from '@/contexts/collection-filter-provider'
+import { SidebarStateProvider } from '@/contexts/sidebar-state-provider'
import { ProtectedRoute } from '@/components/protected-route'
import { AdminRoute } from '@/components/admin-route'
import { AgentsRoute } from '@/components/agents-route'
@@ -86,7 +87,9 @@ function App() {
element={
<ProtectedRoute>
<CollectionFilterProvider>
- <AppLayout />
+ <SidebarStateProvider>
+ <AppLayout />
+ </SidebarStateProvider>
</CollectionFilterProvider>
</ProtectedRoute>
}
diff --git a/frontend/src/components/app-layout.tsx b/frontend/src/components/app-layout.tsx
index 46b5952e..5ced4328 100644
--- a/frontend/src/components/app-layout.tsx
+++ b/frontend/src/components/app-layout.tsx
@@ -7,6 +7,7 @@ import { useQuery } from '@tanstack/react-query'
import { useAuth } from '@/contexts/auth-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
import { useWorkspace } from '@/contexts/workspace-context'
+import { useSidebarState } from '@/contexts/sidebar-state-context'
import { CollectionSelector } from '@/components/collection-selector'
import { auth as authApi, admin as adminApi } from '@/lib/api'
import { resolveSupportedLang } from '@/lib/i18n'
@@ -66,8 +67,6 @@ import { formatCurrency } from '@/lib/format'
const QuickAddTransaction = lazy(() => import('@/components/quick-add-transaction'))
-const SIDEBAR_COLLAPSED_STORAGE_KEY = 'securo.sidebar.collapsed'
-
/** Placeholder rows shown while the workspace's module list is in flight. */
function NavSkeleton() {
return (
@@ -99,9 +98,7 @@ export function AppLayout() {
const location = useLocation()
const [sidebarOpen, setSidebarOpen] = useState(false)
const [quickAddOpen, setQuickAddOpen] = useState(false)
- const [desktopSidebarCollapsed, setDesktopSidebarCollapsed] = useState(
- () => localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY) === 'true',
- )
+ const { collapsed: desktopSidebarCollapsed, toggleCollapsed: toggleDesktopSidebar } = useSidebarState()
const [accountsExpanded, setAccountsExpanded] = useState(true)
const [accountsShowAll, setAccountsShowAll] = useState(false)
const { privacyMode, togglePrivacyMode, mask } = usePrivacyMode()
@@ -186,13 +183,6 @@ export function AppLayout() {
: typeof window !== 'undefined' &&
window.matchMedia?.('(prefers-color-scheme: dark)').matches
const toggleTheme = () => setTheme(isDark ? 'light' : 'dark')
- const toggleDesktopSidebar = () => {
- setDesktopSidebarCollapsed((collapsed) => {
- const next = !collapsed
- localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, String(next))
- return next
- })
- }
const { data: accountsList } = useQuery({
queryKey: ['accounts'],
diff --git a/frontend/src/contexts/sidebar-state-context.test.tsx b/frontend/src/contexts/sidebar-state-context.test.tsx
new file mode 100644
index 00000000..339a8c35
--- /dev/null
+++ b/frontend/src/contexts/sidebar-state-context.test.tsx
@@ -0,0 +1,80 @@
+import type { ReactNode } from 'react'
+import { describe, expect, it, beforeEach } from 'vitest'
+import { act, renderHook } from '@testing-library/react'
+
+import { SidebarStateProvider, SIDEBAR_COLLAPSED_STORAGE_KEY } from '@/contexts/sidebar-state-provider'
+import { useSidebarState } from '@/contexts/sidebar-state-context'
+
+function wrapper({ children }: { children: ReactNode }) {
+ return <SidebarStateProvider>{children}</SidebarStateProvider>
+}
+
+beforeEach(() => {
+ localStorage.clear()
+})
+
+describe('SidebarStateProvider', () => {
+ it('starts expanded when nothing is stored', () => {
+ const { result } = renderHook(() => useSidebarState(), { wrapper })
+
+ expect(result.current.collapsed).toBe(false)
+ })
+
+ it('restores the collapsed state from localStorage', () => {
+ localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, 'true')
+
+ const { result } = renderHook(() => useSidebarState(), { wrapper })
+
+ expect(result.current.collapsed).toBe(true)
+ })
+
+ it('treats any non-true value as expanded', () => {
+ localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, 'banana')
+
+ const { result } = renderHook(() => useSidebarState(), { wrapper })
+
+ expect(result.current.collapsed).toBe(false)
+ })
+
+ it('toggles collapsed and persists the choice', () => {
+ const { result } = renderHook(() => useSidebarState(), { wrapper })
+
+ act(() => result.current.toggleCollapsed())
+ expect(result.current.collapsed).toBe(true)
+ expect(localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY)).toBe('true')
+
+ act(() => result.current.toggleCollapsed())
+ expect(result.current.collapsed).toBe(false)
+ expect(localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY)).toBe('false')
+ })
+
+ it('flips a stored collapsed state back to expanded', () => {
+ localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, 'true')
+
+ const { result } = renderHook(() => useSidebarState(), { wrapper })
+
+ act(() => result.current.toggleCollapsed())
+
+ expect(result.current.collapsed).toBe(false)
+ expect(localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY)).toBe('false')
+ })
+
+ it('provides a stable value and toggle callback across renders', () => {
+ const { result } = renderHook(() => useSidebarState(), { wrapper })
+
+ const firstValue = result.current
+ act(() => result.current.toggleCollapsed())
+ const secondValue = result.current
+
+ // The callback identity stays stable across re-renders.
+ expect(firstValue.toggleCollapsed).toBe(secondValue.toggleCollapsed)
+ })
+})
+
+describe('useSidebarState', () => {
+ it('refuses to be used outside the provider', () => {
+ expect(() => renderHook(() => useSidebarState())).toThrow(
+ 'useSidebarState must be used within a SidebarStateProvider',
+ )
+ })
+})
diff --git a/frontend/src/contexts/sidebar-state-context.ts b/frontend/src/contexts/sidebar-state-context.ts
new file mode 100644
index 00000000..3c0236e3
--- /dev/null
+++ b/frontend/src/contexts/sidebar-state-context.ts
@@ -0,0 +1,18 @@
+import { createContext, useContext } from 'react'
+
+export type SidebarStateValue = {
+ /** Whether the desktop sidebar is collapsed to its narrow rail (desktop only). */
+ collapsed: boolean
+ /** Toggle the desktop sidebar's collapsed state (persists to localStorage). */
+ toggleCollapsed: () => void
+}
+
+export const SidebarStateContext = createContext<SidebarStateValue | null>(null)
+
+export function useSidebarState(): SidebarStateValue {
+ const ctx = useContext(SidebarStateContext)
+ if (!ctx) {
+ throw new Error('useSidebarState must be used within a SidebarStateProvider')
+ }
+ return ctx
+}
diff --git a/frontend/src/contexts/sidebar-state-provider.tsx b/frontend/src/contexts/sidebar-state-provider.tsx
new file mode 100644
index 00000000..c6e005ff
--- /dev/null
+++ b/frontend/src/contexts/sidebar-state-provider.tsx
@@ -0,0 +1,28 @@
+import { useState, useCallback, useMemo, type ReactNode } from 'react'
+
+import { SidebarStateContext, type SidebarStateValue } from '@/contexts/sidebar-state-context'
+
+export const SIDEBAR_COLLAPSED_STORAGE_KEY = 'securo.sidebar.collapsed'
+
+export function SidebarStateProvider({ children }: { children: ReactNode }) {
+ const [collapsed, setCollapsed] = useState(
+ () => localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY) === 'true',
+ )
+
+ const toggleCollapsed = useCallback(() => {
+ setCollapsed((current) => {
+ const next = !current
+ localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, String(next))
+ return next
+ })
+ }, [])
+
+ const value = useMemo(
+ () => ({ collapsed, toggleCollapsed }) satisfies SidebarStateValue,
+ [collapsed, toggleCollapsed],
+ )
+
+ return (
+ <SidebarStateContext.Provider value={value}>{children}</SidebarStateContext.Provider>
+ )
+}
diff --git a/frontend/src/pages/transactions.tsx b/frontend/src/pages/transactions.tsx
index 8c75a071..c5bb04f3 100644
--- a/frontend/src/pages/transactions.tsx
+++ b/frontend/src/pages/transactions.tsx
@@ -50,6 +50,8 @@ import { TransactionsPageActions } from '@/components/transactions-page-actions'
import { MobileBulkSelectionActions } from '@/components/mobile-bulk-selection-actions'
import { type ColumnDef, type ColumnId, useTransactionsGridState } from '@/components/transactions-grid-columns'
import { TransferDialog } from '@/components/transfer-dialog'
+import { useSidebarState } from '@/contexts/sidebar-state-context'
+import { cn } from '@/lib/utils'
import { LinkTransferDialog } from '@/components/link-transfer-dialog'
import { BulkAddToGroupDialog, type BulkAddToGroupSubmission } from '@/components/bulk-add-to-group-dialog'
import { TransactionsFilterBar } from '@/components/transactions-filter-bar'
@@ -84,6 +86,7 @@ const HIDE_IGNORED_STORAGE_KEY = 'securo.transactions.hideIgnored'
export default function TransactionsPage() {
const { t, i18n } = useTranslation()
+ const { collapsed: sidebarCollapsed } = useSidebarState()
const [searchParams] = useSearchParams()
const navigate = useNavigate()
const locale = useDisplayLocale()
@@ -1760,9 +1763,15 @@ export default function TransactionsPage() {
{/* Bulk Action Bar — aligned with the main content area: clears the
fixed sidebar on lg+ and matches the page's max-w-7xl + p-6 wrapper
so the bar visually sits over the transactions list, not the
- full viewport. */}
+ full viewport. The left offset follows the sidebar's real
+ collapsed state (240px expanded / 64px collapsed) so it stays
+ centered when the nav is collapsed (issue #1027). */}
{viewMode === 'list' && selectedIds.size > 0 && (
- <div className="fixed bottom-0 left-0 right-0 lg:left-60 z-50">
+ <div className={cn(
+ "fixed bottom-0 left-0 right-0 z-50",
+ "transition-[left] duration-300 ease-in-out motion-reduce:transition-none",
+ sidebarCollapsed ? 'lg:left-16' : 'lg:left-60',
+ )}>
<div className="mx-auto max-w-7xl px-3 md:px-6 pb-4 md:pb-6">
<div className="flex items-stretch gap-1.5 bg-card border border-border shadow-xl rounded-2xl p-2">
<MobileBulkSelectionActions
diff --git a/frontend/src/test/utils.tsx b/frontend/src/test/utils.tsx
index 053752ea..586e5217 100644
--- a/frontend/src/test/utils.tsx
+++ b/frontend/src/test/utils.tsx
@@ -18,6 +18,7 @@ import { render, type RenderOptions, type RenderResult } from '@testing-library/
import userEvent from '@testing-library/user-event'
import i18n from '@/lib/i18n'
+import { SidebarStateProvider } from '@/contexts/sidebar-state-provider'
/**
* Retries and caching are useful in the app and only add flake in a test: a
@@ -65,15 +66,17 @@ export function renderWithProviders(
return (
<I18nextProvider i18n={i18n}>
<QueryClientProvider client={queryClient}>
- <MemoryRouter initialEntries={[route]}>
- {path ? (
- <Routes>
- <Route path={path} element={children} />
- </Routes>
- ) : (
- children
- )}
- </MemoryRouter>
+ <SidebarStateProvider>
+ <MemoryRouter initialEntries={[route]}>
+ {path ? (
+ <Routes>
+ <Route path={path} element={children} />
+ </Routes>
+ ) : (
+ children
+ )}
+ </MemoryRouter>
+ </SidebarStateProvider>
</QueryClientProvider>
</I18nextProvider>
)