Skip to content

Add filter negation feature#93909

Open
bernhardoj wants to merge 47 commits into
Expensify:mainfrom
bernhardoj:feat/93208-filter-negation
Open

Add filter negation feature#93909
bernhardoj wants to merge 47 commits into
Expensify:mainfrom
bernhardoj:feat/93208-filter-negation

Conversation

@bernhardoj

@bernhardoj bernhardoj commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

Fixed Issues

$ #93208
PROPOSAL:

Tests

Same as QA Steps

  • Verify that no errors appear in the JS console

Offline tests

Same as QA Steps

QA Steps

  1. Go to Spend > Expenses
  2. Press the Filters button
  3. Open Status, Workspace, To, From, Currency, Purchase currency, Merchant, and "Exported to" filter
  4. Verify it has "Filter type" with "is" and "is not" options, and "is" is initially selected
  5. Open Has filter
  6. Verify it has "Filter type" with "has" and "doesn't have" options, and "has" is initially selected
  7. Apply any value and filter type to the filters
  8. If a negative filter type is applied (is not/doesn't have), verify the filter chip/bar label is prefixed with -
  9. Close and reopen the filter
  10. Verify the filter value and type are still selected with the correct value
  11. Open the save search page
  12. Verify it has the same label as in the filter chip/bar

For steps 3-7, if you want more test steps, follow this:
No filter is applied yet

  1. Open any filter from the step above
  2. Verify the positive filter type is selected
  3. Change the filter type to negative
  4. Verify the filter is not applied yet (because no value is selected)
  5. Apply any value
  6. Verify that a negation filter is applied
  7. Change the filter type back to positive
  8. Verify that a positive filter is applied
  9. Unapply and reapply the value
  10. Verify that a positive filter is applied

For filter chip/bar:

  1. Press any applied filter chip/bar
  2. Verify that the filter value and type are selected with the correct value
  3. Play around with the test steps above
  • 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 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 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.
  • 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
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
web.mp4

@bernhardoj
bernhardoj requested review from a team as code owners June 18, 2026 05:17
@melvin-bot

melvin-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

Hey, I noticed you changed src/languages/en.ts in a PR from a fork. For security reasons, translations are not generated automatically for PRs from forks.

If you want to automatically generate translations for other locales, an Expensify employee will have to:

  1. Look at the code and make sure there are no malicious changes.
  2. Run the Generate static translations GitHub workflow. If you have write access and the K2 extension, you can simply click: [this button]

Alternatively, if you are an external contributor, you can run the translation script locally with your own OpenAI API key. To learn more, try running:

npx ts-node ./scripts/generateTranslations.ts --help

Typically, you'd want to translate only what you changed by running npx ts-node ./scripts/generateTranslations.ts --compare-ref main

@melvin-bot
melvin-bot Bot requested review from DylanDylann and JmillsExpensify and removed request for a team June 18, 2026 05:18
@melvin-bot

melvin-bot Bot commented Jun 18, 2026

Copy link
Copy Markdown

@DylanDylann 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 June 18, 2026 05:18
@bernhardoj

Copy link
Copy Markdown
Contributor Author

Can I run the translation script myself?

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d5746a1607

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@bernhardoj

Copy link
Copy Markdown
Contributor Author

Btw, when we apply a negated HAS filter with a Tag value, it will be applied as "Tag: No tag".

w.mp4

It's from this code. It causes weird buggy behavior with the Has filter component, as shown in the video above.

if (hasNegatedTagFilter) {
filtersForm[FILTER_KEYS.TAG] = [CONST.SEARCH.TAG_EMPTY_VALUE];
}

Is that expected?

@github-actions

Copy link
Copy Markdown
Contributor

🚧 @dubielzyk-expensify has triggered a test Expensify/App build. You can view the workflow run here.

@dubielzyk-expensify

Copy link
Copy Markdown
Contributor

This is looking great already @bernhardoj. Let's make this text label (13px) not body (15px)

CleanShot 2026-06-18 at 16 28 42@2x

@dubielzyk-expensify

dubielzyk-expensify commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

I'm not convinced we should use the is/is not in the chip itself:

CleanShot 2026-06-18 at 16 29 35@2x

I have to go and will have a noodle, but I think we might want an icon or do From: not Jon Dubby. But even that feels weird. I don't think it's crucial for it to be written fully out, but just be able to signal between is/isn't in some way, then when you open the filter you can see it more clearly there anyways

cc @Expensify/design @trjExpensify @JmillsExpensify

@bernhardoj

Copy link
Copy Markdown
Contributor Author

Let's make this text label (13px) not body (15px)

Done

@JS00001
JS00001 requested review from JS00001 and aimane-chnaif and removed request for DylanDylann June 18, 2026 12:12
@JmillsExpensify

Copy link
Copy Markdown
Contributor

I'm not convinced we should use the is/is not in the chip itself:

CleanShot 2026-06-18 at 16 29 35@2x I have to go and will have a noodle, but I think we might want an icon or do `From: not Jon Dubby`. But even that feels weird. I don't think it's crucial for it to be written fully out, but just be able to signal between is/isn't in some way, then when you open the filter you can see it more clearly there anyways

cc @Expensify/design @trjExpensify @JmillsExpensify

I'm fine with what I'm seeing in the adhoc.

@JmillsExpensify

Copy link
Copy Markdown
Contributor

@bernhardoj Looks like we're still missing support for Status and Workspace in the adhoc. Those are mentioned in the OP. Let me know if I'm missing something!

CleanShot 2026-06-18 at 16 47 06@2x

@JS00001

JS00001 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Status and workspace are still having their BE PRs worked on: https://github.com/Expensify/Auth/pull/22241

@JS00001

JS00001 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Can we determine if its negated or not by its key and show any negated filters?

@bernhardoj

Copy link
Copy Markdown
Contributor Author

@JS00001 the reason it doesn't show is actually because I gated it through isFilterNegatable.

const NEGATABLE_FILTER_KEYS = [
CONST.SEARCH.SYNTAX_FILTER_KEYS.TO,
CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,
CONST.SEARCH.SYNTAX_FILTER_KEYS.HAS,
CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY,
CONST.SEARCH.SYNTAX_FILTER_KEYS.PURCHASE_CURRENCY,
CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT,
CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPORTED_TO,
CONST.SEARCH.SYNTAX_FILTER_KEYS.POLICY_ID,
CONST.SEARCH.SYNTAX_FILTER_KEYS.STATUS,
] as const;

image

It's the same filtering logic used for the filter bars/chips.

Do we want to remove the filtering? This will show the filter bar and the applied filters on the search page for every negated filter.

image

But it won't have the negation switch.

image image

@JS00001

JS00001 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@bernhardoj I think that makes sense, since they can be negated via the search query

@bernhardoj

Copy link
Copy Markdown
Contributor Author

@JS00001 but is it fine that the popup won't have the negation (Filter type) switch? (the left image). Notice that no option is selected, because it only works with non-negated value.

@JS00001

JS00001 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Yes, I think the popup not showing the negation is fine, that's the current behavior

@bernhardoj

Copy link
Copy Markdown
Contributor Author

Cool, updated!

Copy link
Copy Markdown
Contributor

Yeah, I also think that's fine.

@bernhardoj
bernhardoj requested a review from aimane-chnaif July 17, 2026 08:54
@JS00001

JS00001 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

@aimane-chnaif back to you

@aimane-chnaif

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 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
    • 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
    • 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 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

@aimane-chnaif

Copy link
Copy Markdown
Contributor

A few issues found:

Bug 1:

  1. Search type:expense category!=Travel
  2. Click -Category: Travel filter pill
  3. Click Apply without any change
  4. Filter is gone <--- bug
Screen.Recording.2026-07-17.at.4.18.12.PM.mov

Bug 2:

  1. Search type:expense has:receipt has!=category
  2. Console error shows (React duplicate-key warning) <--- bug
  3. click Has: Receipt filter pill
  4. doesn't have filter type is selected with Category <--- bug
  5. Click Apply without any change
  6. has:receipt Filter is gone <--- bug
Screen.Recording.2026-07-17.at.4.25.15.PM.mov

@bernhardoj

bernhardoj commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

For the 1st bug, it's the result of showing all negated filters, even without the negation/Filter type switch. But I think even though it doesn't have that switch, we can just show the selection for the negated value. So, any update to the filter will update the negated value.
image

@JS00001 @JmillsExpensify let me know if you disagree.

For the 2nd bug, I think both non-negated and negated filters shouldn't be applied at the same time in the first place. When the user writes the same filter with both non-negated and negated types, I think we should take the rightmost one.

For example, if we type
category:Advertising -category:Home

then the final category filter is categoryNot: [Home]

(I think the 2nd bug shouldn't block this PR if we want to go forward because we can apply both non-negated and negated filters already on prod)

UPDATE: I just checked on prod, and when we write 2 same filter keys, for example, category:Advertising category:Home, both are sent to the BE, even though we only show the Home one on the FE, but the BE will return no result. I think we should fix this at the query parsing level, so the parsed query won't contain duplicate keys (or should we keep how the parsing works today?).

@JS00001 @JmillsExpensify what do you guys think?

(also please check this one 😄)

Copy link
Copy Markdown
Contributor

So, any update to the filter will update the negated value

That makes sense to me.

For the 2nd bug, I think both non-negated and negated filters shouldn't be applied at the same time in the first place. When the user writes the same filter with both non-negated and negated types, I think we should take the rightmost one.

Also makes sense to me, since the right-most is the most recent.

I just checked on prod, and when we write 2 same filter keys, for example, category:Advertising category:Home, both are sent to the BE, even though we only show the Home one on the FE, but the BE will return no result. I think we should fix this at the query parsing level, so the parsed query won't contain duplicate keys (or should we keep how the parsing works today?).

Given that this is already on production, I'd handle this separately and not block this PR on it.

@bernhardoj

Copy link
Copy Markdown
Contributor Author

@aimane-chnaif just merged with main, please continue the review

@JmillsExpensify bump for this one please 😄

Comment thread src/libs/SearchUIUtils.ts
value: SearchAdvancedFiltersForm[K] | undefined;
} {
const negatedFilterKey = `${filterKey}${CONST.SEARCH.NOT_MODIFIER}` as const;
if (!isFilterNegatable(filterKey) || !values || !hasKey(values, negatedFilterKey)) {

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.

Removing !isFilterNegatable(filterKey) can lead to a loop hole where users can type in -category:Car for example and land on a negated category query without any visual indicator in the pickers.

Screen.Recording.2026-07-21.at.1.26.35.PM.mov

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we discussed it here and decide to allow that

return filterKey.endsWith(CONST.SEARCH.NOT_MODIFIER);
}

function getFilterFormValues<K extends ListFilterContentProps['baseFilterKey'] | TextInputFilterContentProps['baseFilterKey']>(

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.

Can we add a test for this function? It's pretty central to the negated util.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

[FILTER_KEYS.HAS_NOT]: HasFilterValues;

[FILTER_KEYS.IS]: IsFilterValues;
[FILTER_KEYS.IS_NOT]: string[];

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.

Can we tighten the type for this one too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

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.