Skip to content

Preserve tag filters cache offline - #102350

Merged
JS00001 merged 21 commits into
Expensify:mainfrom
ShridharGoel:shridhar/fix-offline-tag-filters
Oct 6, 2026
Merged

JS00001 merged 21 commits into
Expensify:mainfrom
ShridharGoel:shridhar/fix-offline-tag-filters

Conversation

@ShridharGoel

@ShridharGoel ShridharGoel commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Preserves cached tag filter results in Onyx when useSearchTagFilters unmounts so previously loaded tags remain accessible when reopening the filter offline. When online, reopening with incomplete pagination continues to re-fetch from the first page as intended. Also removes the unused clearSearchTagFiltersState action.

Fixed Issues

$ #102049

Tests

  1. Go to Spend > Expenses.
  2. Open Filters > Tag and verify tags load.
  3. Close Filters > Tag.
  4. Go offline.
  5. Open Filters > Tag.
  6. Verify the tag list shows previously loaded tags.
  7. Type in the search input while offline and verify cached tags filter locally.
  8. Clear the search input and verify cached tags return.
  9. Go online and verify the list refreshes from the server.
  • Verify that no errors appear in the JS console

Offline tests

QA Steps

Same as tests.

  • 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 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 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)
  • 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)
  • 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 the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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

MacOS: Chrome / Safari
Screen.Recording.2026-09-26.at.7.46.57.PM.mov

Do not wipe cached tag filter results on unmount so they remain available offline when reopening the filter.
@ShridharGoel ShridharGoel changed the title fix(search): preserve tag filters cache offline Preserve tag filters cache offline Sep 26, 2026
@ShridharGoel
ShridharGoel marked this pull request as ready for review September 26, 2026 14:19
@ShridharGoel
ShridharGoel requested review from a team as code owners September 26, 2026 14:19
@melvin-bot
melvin-bot Bot requested review from JmillsExpensify and Krishna2323 and removed request for a team September 26, 2026 14:19
@melvin-bot

melvin-bot Bot commented Sep 26, 2026

Copy link
Copy Markdown

@Krishna2323 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]

Comment thread src/hooks/useSearchTagFilters.ts Resolved
Unify pagination state updates across ref and Onyx to satisfy CONSISTENCY-3.

@JmillsExpensify JmillsExpensify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Comment thread src/hooks/useSearchTagFilters.ts Resolved
Comment thread src/hooks/useSearchTagFilters.ts Resolved
Comment thread src/hooks/useSearchTagFilters.ts Resolved
Reset active search term on unmount and scope pagination cache by policyIDs so stale terms and wrong workspace tags do not suppress offline fallback.
@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

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 Δ
src/ONYXKEYS.ts 100.00% <ø> (ø)
...nents/Search/FilterComponents/CategorySelector.tsx 73.52% <100.00%> (+0.80%) ⬆️
...components/Search/FilterComponents/TagSelector.tsx 85.71% <100.00%> (+85.71%) ⬆️
src/hooks/useSearchTagFilters.ts 95.12% <100.00%> (+0.67%) ⬆️
src/libs/ExportOnyxState/masking.ts 80.95% <ø> (ø)
src/setup/index.ts 100.00% <ø> (ø)
src/libs/SearchQueryUtils.ts 89.55% <91.66%> (-0.02%) ⬇️
src/libs/actions/Search.ts 67.52% <62.50%> (+2.27%) ⬆️
... and 19 files with indirect coverage changes

Searching online overwrote cached results with partial matches, leaving too few tags to enable the search box offline. Cache base tags separately in pagination state so unmount or offline mode restores the full tag list.
@Krishna2323

Copy link
Copy Markdown
Contributor

@MelvinBot review

@MelvinBot

Copy link
Copy Markdown
Contributor

Code review

The approach works for the reported case, but I found two bugs to fix before merge. CI is green except the approval check.

1. Offline search shows another workspace's tags. useSearchTagFilters.ts:67 builds effectiveBaseResults, baseHasMore, and baseCursor even when isPolicyScopeMismatch is true. updatePagination then saves those old base values under the new policyIDs.

Repro: cache tags for workspace A, go offline, and scope the filter to workspace B. The list is empty at first, which is correct. Type one letter, and searchTags (L213-L217) saves A's tags as B's. The mismatch check stops matching, and TagSelector shows A's tags. It also skips its synced POLICY_TAGS fallback. Online, the same write shows A's tags while B's first page loads. If that request fails, A's tags stay.

Fix: scope the base values to the current workspace, for example const effectiveBaseResults = isPolicyScopeMismatch ? undefined : (baseResults ?? searchResults). Do the same for baseHasMore and baseCursor. Add a test that types offline after a scope mismatch.

2. Online reopen no longer re-fetches a complete tag list. The old unmount cleanup cleared the cache, so every reopen fetched fresh tags. Now, after a full load (baseHasMore === false), reopening online makes the mount effect call searchTags('') (L300). That call takes the local-filter branch (L224) and skips the API. Tags added, renamed, or deleted later in the session don't appear until the app restarts, because the key is RAM-only.

Fix: make the mount and reconnect fetch always call the server for the first page. The cached baseResults can stay visible while it loads. Add a test for an online remount with a complete cache.

Minor cleanups (non-blocking)
  • openSearchTagFiltersPage always returns tags as an array (?? []), so the tags !== undefined checks at L194 and L246 never do anything. baseCursor defaults to '', so the ?? stateRef.current.nextCursor fallback at L215 also never runs.
  • The full base tag list is now stored twice: in RAM_ONLY_SEARCH_TAG_FILTERS_RESULTS and in baseResults on the pagination key. updatePagination rewrites the whole array with Onyx.set on every keystroke. Accounts with many tags can have thousands of items after paging. You could keep baseResults and write only the small pagination fields on each keystroke.
  • The cleanup effect now depends on [policyIDs] (L272), so it also runs when the scope changes while the filter is open. That's fine once bug 1 is fixed, but the comment still says it runs on unmount only.

view run

@Krishna2323

Copy link
Copy Markdown
Contributor

@ShridharGoel pelase check the bugs found by melvin, I confirmed both are real

@ShridharGoel

Copy link
Copy Markdown
Contributor Author

@MelvinBot review

@MelvinBot

MelvinBot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Code review (re-review of f202b4f8d5)

Both earlier bugs are fixed, but the new per-workspace keys add one regression: online server searches now show no results.

  • Bug 1 (other workspace's tags): fixed. Keying pagination and results by policyIDs isolates the cache per scope.
  • Bug 2 (no refresh on reopen): fixed. Mount and reconnect now call fetchFirstPage, which skips the local-filter branch.

New bug: first-page results for a non-empty query never reach the new results key. The client only writes the results key for cursor pages (Search.ts:1116). The first page arrives through the server's onyxData, which writes to the old searchTagFiltersResults key. That key isn't searchTagFiltersResults_<policyIDs>, so the hook never sees it (L50).

The empty-query list still works because it uses baseResults from the response's tags. For a non-empty query online, the hook reads the empty per-workspace key (L69) and discards the response tags (L222).

Repro: use an account with more than 200 tags (one page), so search goes to the server. Type a query online. The list shows only "No tag", and "load more" appends later pages onto an empty list. This hits the high-tag accounts from the linked issue. The unit tests mock openSearchTagFiltersPage and seed the results key directly, so they don't catch it.

Fix: in openSearchTagFiltersPage, write the response tags to resultsKey for the first page too, for example Onyx.set(resultsKey, params.cursor ? [...currentResults, ...newTags] : newTags). Add a test that runs a non-empty server search and checks searchResults.

Minor (non-blocking)
  • The server still sends onyxData for the old searchTagFiltersResults key. That key is no longer in the RAM-only list in setup/index.ts, so Onyx now saves those tag names to disk under an unused key. Keep the old key registered as RAM-only until the backend stops sending it.
  • The policyIDs field on SearchTagFiltersPaginationState is now redundant because the key already includes the scope.

I found the new bug by reading the code. The web tester confirmed that Filters > Tag lists the workspace tags on this branch with an empty query. The tester couldn't capture the response body, and a 200+ tag account wasn't available, so the search case wasn't run in the app.


view run · view recording

Copy link
Copy Markdown
Contributor

Agreed

@Krishna2323

Copy link
Copy Markdown
Contributor

@ShridharGoel but it works for P-1 and not P-2. In online mode, I loaded all the tags from both policies, but after going offline and filtering by P-1, the P-1 tags are shown while the same doesn't happen for P-2. Am I missing something?

@ShridharGoel

Copy link
Copy Markdown
Contributor Author

@Krishna2323 Can you try after clearing the cache? Maybe P-1's tags were already cached under P-1's specific key if you filtered by that earlier?

@Krishna2323

Copy link
Copy Markdown
Contributor

@ShridharGoel same results even after clearing cache:

Monosnap.screencast.2026-10-06.01-14-06.mp4

@ShridharGoel

Copy link
Copy Markdown
Contributor Author

@Krishna2323 Is P-1 your default workspace?

I was able to repro, and it was happening since just after resetting the cache OpenPolicyTagsPage was getting called which fetched the tags of the default workspace. Can you see if that's happening for you too? This adds the tags to the main Onyx key policyTags_<policyID> which works as a fallback in the search filter.

@Krishna2323

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (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 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)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • 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 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.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this 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.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari
Monosnap.screencast.2026-10-06.16-15-31.mp4

@Krishna2323

Copy link
Copy Markdown
Contributor

Is P-1 your default workspace?

@ShridharGoel Yes. P-1 is my default workspace, so OpenPolicyTagsPage populated its policyTags_ fallback. Makes sense now, fine to leave as is.

@Krishna2323 Krishna2323 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@melvin-bot
melvin-bot Bot requested a review from JS00001 October 6, 2026 10:49

@JS00001 JS00001 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couple of small comments. Also, why were the github action js files updated? This shouldnt have affected those, right?

Comment thread src/hooks/useSearchTagFilters.ts Resolved
Comment thread src/libs/SearchQueryUtils.ts Resolved
ShridharGoel and others added 2 commits October 6, 2026 21:13
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ShridharGoel

Copy link
Copy Markdown
Contributor Author

why were the github action js files updated?

Seems like they got added by mistake

@ShridharGoel
ShridharGoel requested a review from JS00001 October 6, 2026 16:37
@JS00001

JS00001 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

is this ready again @ShridharGoel

@ShridharGoel

Copy link
Copy Markdown
Contributor Author

@JS00001 Yes

@JS00001
JS00001 merged commit e9a6c46 into Expensify:main Oct 6, 2026
34 of 35 checks passed
@OSBotify

OSBotify commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🚧 JS00001 has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify OSBotify mentioned this pull request Oct 6, 2026
100 tasks done
@OSBotify

OSBotify commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/JS00001 in version: 9.5.4-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site update is needed. This PR is a bug fix: previously loaded tags now stay available when you reopen the Tag filter offline. It doesn't add a feature, rename anything, or make any sentence in App/docs/articles incorrect.


view run

@OSBotify

OSBotify commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/puneetlath in version: 9.5.4-3 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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.

6 participants