A local-first personal finance app
[AI] Keep the mobile page header mounted across navigation to avoid flashing (#7842)
Matiss Janis Aboltins329a7e81e7c2
diff --git a/packages/desktop-client/src/components/FinancesApp.tsx b/packages/desktop-client/src/components/FinancesApp.tsxindex c188def2ef..f1474c0373 100644--- a/packages/desktop-client/src/components/FinancesApp.tsx+++ b/packages/desktop-client/src/components/FinancesApp.tsx@@ -31,6 +31,7 @@ import { MobileBankSyncAccountEditPage } from './mobile/banksync/MobileBankSyncAimport { MobileNavTabs } from './mobile/MobileNavTabs';import { TransactionEdit } from './mobile/transactions/TransactionEdit';import { Notifications } from './Notifications';+import { MobilePageHeaderProvider, MobilePageHeaderSlot } from './Page';import { Reports } from './reports';import { LoadingIndicator } from './reports/LoadingIndicator';import { NarrowAlternate, WideComponent } from './responsive';@@ -219,189 +220,198 @@ export function FinancesApp() {isDisabled={!isNarrowWidth}scrollableRef={scrollableRef}>- <View- ref={scrollableRef}- style={{- flex: 1,- overflow: 'auto',- position: 'relative',- }}- >- <Titlebar+ <MobilePageHeaderProvider>+ <View+ ref={scrollableRef}style={{- WebkitAppRegion: 'drag',- position: 'absolute',- top: 0,- left: 0,- right: 0,- zIndex: 1000,+ flex: 1,+ overflow: 'auto',+ position: 'relative',}}- />- <Notifications />- <BankSyncStatus />-- <Routes>- <Route- path="/"- element={- isAccountsFetching || !accounts ? (- <LoadingIndicator />- ) : accounts.length > 0 ? (- <Navigate to="/budget" replace />- ) : (- // If there are no accounts, we want to redirect the user to- // the All Accounts screen which will prompt them to add an account- <Navigate to="/accounts" replace />- )- }+ >+ <Titlebar+ style={{+ WebkitAppRegion: 'drag',+ position: 'absolute',+ top: 0,+ left: 0,+ right: 0,+ zIndex: 1000,+ }}/>+ <Notifications />+ <BankSyncStatus />+ {isNarrowWidth && <MobilePageHeaderSlot />}- <Route path="/reports/*" element={<Reports />} />+ <Routes>+ <Route+ path="/"+ element={+ isAccountsFetching || !accounts ? (+ <LoadingIndicator />+ ) : accounts.length > 0 ? (+ <Navigate to="/budget" replace />+ ) : (+ // If there are no accounts, we want to redirect the user to+ // the All Accounts screen which will prompt them to add an account+ <Navigate to="/accounts" replace />+ )+ }+ />- <Route- path="/budget"- element={<NarrowAlternate name="Budget" />}- />+ <Route path="/reports/*" element={<Reports />} />- <Route- path="/schedules"- element={<NarrowAlternate name="Schedules" />}- />- <Route- path="/schedules/:id"- element={- <WideNotSupported>- <NarrowAlternate name="ScheduleEdit" />- </WideNotSupported>- }- />+ <Route+ path="/budget"+ element={<NarrowAlternate name="Budget" />}+ />- <Route- path="/payees"- element={<NarrowAlternate name="Payees" />}- />- <Route- path="/payees/:id"- element={- <WideNotSupported>- <NarrowAlternate name="PayeeEdit" />- </WideNotSupported>- }- />- <Route- path="/rules"- element={- <ErrorBoundary- FallbackComponent={FeatureErrorFallback}- resetKeys={[location.pathname]}- >- <NarrowAlternate name="Rules" />- </ErrorBoundary>- }- />- <Route- path="/rules/:id"- element={- <ErrorBoundary- FallbackComponent={FeatureErrorFallback}- resetKeys={[location.pathname]}- >- <NarrowAlternate name="RuleEdit" />- </ErrorBoundary>- }- />- <Route- path="/bank-sync"- element={<NarrowAlternate name="BankSync" />}- />- <Route- path="/bank-sync/account/:accountId/edit"- element={- <WideNotSupported redirectTo="/bank-sync">- <MobileBankSyncAccountEditPage />- </WideNotSupported>- }- />- <Route path="/tags" element={<ManageTagsPage />} />- <Route path="/settings" element={<Settings />} />+ <Route+ path="/schedules"+ element={<NarrowAlternate name="Schedules" />}+ />+ <Route+ path="/schedules/:id"+ element={+ <WideNotSupported>+ <NarrowAlternate name="ScheduleEdit" />+ </WideNotSupported>+ }+ />- <Route- path="/gocardless/link"- element={- <NarrowNotSupported>- <WideComponent name="GoCardlessLink" />- </NarrowNotSupported>- }- />+ <Route+ path="/payees"+ element={<NarrowAlternate name="Payees" />}+ />+ <Route+ path="/payees/:id"+ element={+ <WideNotSupported>+ <NarrowAlternate name="PayeeEdit" />+ </WideNotSupported>+ }+ />+ <Route+ path="/rules"+ element={+ <ErrorBoundary+ FallbackComponent={FeatureErrorFallback}+ resetKeys={[location.pathname]}+ >+ <NarrowAlternate name="Rules" />+ </ErrorBoundary>+ }+ />+ <Route+ path="/rules/:id"+ element={+ <ErrorBoundary+ FallbackComponent={FeatureErrorFallback}+ resetKeys={[location.pathname]}+ >+ <NarrowAlternate name="RuleEdit" />+ </ErrorBoundary>+ }+ />+ <Route+ path="/bank-sync"+ element={<NarrowAlternate name="BankSync" />}+ />+ <Route+ path="/bank-sync/account/:accountId/edit"+ element={+ <WideNotSupported redirectTo="/bank-sync">+ <MobileBankSyncAccountEditPage />+ </WideNotSupported>+ }+ />+ <Route path="/tags" element={<ManageTagsPage />} />+ <Route path="/settings" element={<Settings />} />- <Route- path="/enablebanking/auth_callback"- element={<EnableBankingCallback />}- />+ <Route+ path="/gocardless/link"+ element={+ <NarrowNotSupported>+ <WideComponent name="GoCardlessLink" />+ </NarrowNotSupported>+ }+ />- <Route- path="/accounts"- element={<NarrowAlternate name="Accounts" />}- />+ <Route+ path="/enablebanking/auth_callback"+ element={<EnableBankingCallback />}+ />- <Route- path="/accounts/:id"- element={<NarrowAlternate name="Account" />}- />+ <Route+ path="/accounts"+ element={<NarrowAlternate name="Accounts" />}+ />- <Route- path="/transactions/:transactionId"- element={- <WideNotSupported>- <TransactionEdit />- </WideNotSupported>- }- />+ <Route+ path="/accounts/:id"+ element={<NarrowAlternate name="Account" />}+ />- <Route- path="/categories/:id"- element={<NarrowAlternate name="Category" />}- />- {multiuserEnabled && (<Route- path="/user-directory"+ path="/transactions/:transactionId"element={- <ProtectedRoute- permission={Permissions.ADMINISTRATOR}- element={<UserDirectoryPage />}- />+ <WideNotSupported>+ <TransactionEdit />+ </WideNotSupported>}/>- )}- {multiuserEnabled && (+<Route- path="/user-access"- element={- <ProtectedRoute- permission={Permissions.ADMINISTRATOR}- validateOwner- element={<UserAccessPage />}- />- }+ path="/categories/:id"+ element={<NarrowAlternate name="Category" />}+ />+ {multiuserEnabled && (+ <Route+ path="/user-directory"+ element={+ <ProtectedRoute+ permission={Permissions.ADMINISTRATOR}+ element={<UserDirectoryPage />}+ />+ }+ />+ )}+ {multiuserEnabled && (+ <Route+ path="/user-access"+ element={+ <ProtectedRoute+ permission={Permissions.ADMINISTRATOR}+ validateOwner+ element={<UserAccessPage />}+ />+ }+ />+ )}+ {/* redirect all other traffic to the budget page */}+ <Route+ path="/*"+ element={<Navigate to="/budget" replace />}/>- )}- {/* redirect all other traffic to the budget page */}- <Route path="/*" element={<Navigate to="/budget" replace />} />+ </Routes>+ </View>++ <Routes>+ <Route path="/budget" element={<MobileNavTabs />} />+ <Route path="/accounts" element={<MobileNavTabs />} />+ <Route path="/settings" element={<MobileNavTabs />} />+ <Route path="/reports" element={<MobileNavTabs />} />+ <Route+ path="/reports/:dashboardId"+ element={<MobileNavTabs />}+ />+ <Route path="/bank-sync" element={<MobileNavTabs />} />+ <Route path="/rules" element={<MobileNavTabs />} />+ <Route path="/payees" element={<MobileNavTabs />} />+ <Route path="/schedules" element={<MobileNavTabs />} />+ <Route path="*" element={null} /></Routes>- </View>-- <Routes>- <Route path="/budget" element={<MobileNavTabs />} />- <Route path="/accounts" element={<MobileNavTabs />} />- <Route path="/settings" element={<MobileNavTabs />} />- <Route path="/reports" element={<MobileNavTabs />} />- <Route path="/reports/:dashboardId" element={<MobileNavTabs />} />- <Route path="/bank-sync" element={<MobileNavTabs />} />- <Route path="/rules" element={<MobileNavTabs />} />- <Route path="/payees" element={<MobileNavTabs />} />- <Route path="/schedules" element={<MobileNavTabs />} />- <Route path="*" element={null} />- </Routes>+ </MobilePageHeaderProvider></ScrollProvider></View></View>diff --git a/packages/desktop-client/src/components/Page.tsx b/packages/desktop-client/src/components/Page.tsxindex 425845a19f..80448d51a5 100644--- a/packages/desktop-client/src/components/Page.tsx+++ b/packages/desktop-client/src/components/Page.tsx@@ -1,5 +1,6 @@-import React from 'react';+import React, { createContext, useContext, useState } from 'react';import type { CSSProperties, ReactNode } from 'react';+import { createPortal } from 'react-dom';import { useResponsive } from '@actual-app/components/hooks/useResponsive';import { styles } from '@actual-app/components/styles';@@ -107,6 +108,50 @@ export function MobilePageHeader({);}+// On mobile the page header element stays mounted while navigating between+// pages so its background doesn't flash while the next page renders. Pages+// render their header into a single persistent `<MobilePageHeaderSlot />`+// (rendered by the app shell) through a portal. Without a provider (e.g. in+// tests or storybook) the header is rendered inline instead.+const MobilePageHeaderSlotContext = createContext<HTMLElement | null>(null);+const MobilePageHeaderSlotRefContext = createContext<+ ((element: HTMLElement | null) => void) | null+>(null);++export function MobilePageHeaderProvider({+ children,+}: {+ children: ReactNode;+}) {+ const [slot, setSlot] = useState<HTMLElement | null>(null);+ return (+ <MobilePageHeaderSlotRefContext.Provider value={setSlot}>+ <MobilePageHeaderSlotContext.Provider value={slot}>+ {children}+ </MobilePageHeaderSlotContext.Provider>+ </MobilePageHeaderSlotRefContext.Provider>+ );+}++type MobilePageHeaderSlotProps = {+ style?: CSSProperties;+};++export function MobilePageHeaderSlot({ style }: MobilePageHeaderSlotProps) {+ const slotRef = useContext(MobilePageHeaderSlotRefContext);+ return (+ <View+ ref={slotRef ?? undefined}+ style={{+ flexShrink: 0,+ minHeight: HEADER_HEIGHT,+ backgroundColor: theme.mobileHeaderBackground,+ ...style,+ }}+ />+ );+}+type PageProps = {header: ReactNode;style?: CSSProperties;@@ -117,6 +162,7 @@ type PageProps = {export function Page({ header, style, padding, children, footer }: PageProps) {const { isNarrowWidth } = useResponsive();+ const mobileHeaderSlot = useContext(MobilePageHeaderSlotContext);const childrenPadding = padding != null ? padding : isNarrowWidth ? 10 : 20;const headerToRender =@@ -130,28 +176,48 @@ export function Page({ header, style, padding, children, footer }: PageProps) {header);- return (+ const main = (<View+ role="main"style={{- ...(!isNarrowWidth && styles.page),flex: 1,- backgroundColor: isNarrowWidth- ? theme.mobilePageBackground- : theme.pageBackground,- ...style,+ overflowY: isNarrowWidth ? 'auto' : undefined,+ padding: `0 ${childrenPadding}px`,}}>- {headerToRender}+ {children}+ </View>+ );++ if (isNarrowWidth) {+ return (<View- role="main"style={{flex: 1,- overflowY: isNarrowWidth ? 'auto' : undefined,- padding: `0 ${childrenPadding}px`,+ backgroundColor: theme.mobilePageBackground,+ ...style,}}>- {children}+ {mobileHeaderSlot+ ? createPortal(headerToRender, mobileHeaderSlot)+ : headerToRender}+ {main}+ {footer}</View>+ );+ }++ return (+ <View+ style={{+ ...styles.page,+ flex: 1,+ backgroundColor: theme.pageBackground,+ ...style,+ }}+ >+ {headerToRender}+ {main}{footer}</View>);diff --git a/packages/desktop-client/src/components/reports/Overview.tsx b/packages/desktop-client/src/components/reports/Overview.tsxindex 3b133b4f90..30ce4fcbf3 100644--- a/packages/desktop-client/src/components/reports/Overview.tsx+++ b/packages/desktop-client/src/components/reports/Overview.tsx@@ -484,6 +484,7 @@ export function Overview({ dashboard }: OverviewProps) {style={{padding: '5px',borderBottom: '1px solid ' + theme.pillBorder,+ backgroundColor: theme.mobilePageBackground,}}><DashboardSelectordiff --git a/upcoming-release-notes/7842.md b/upcoming-release-notes/7842.mdnew file mode 100644index 0000000000..970a63de0f--- /dev/null+++ b/upcoming-release-notes/7842.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [MatissJanis]+---++Keep mobile page header mounted during navigation to eliminate flashing effects.