Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/WP-72: Highlight matching search terms #873

Merged
merged 12 commits into from
Oct 24, 2023

Conversation

asimregmi
Copy link
Contributor

@asimregmi asimregmi commented Oct 4, 2023

Overview

PR FP-319 introduced the job history search feature. This PR achieves highlighting/bolding of the matching search terms/ query in the Job History Search.

Related

Changes

  • Created a new component HighlightSearchTerm that bolds the matching queries
  • Updated new props in InfiniteScrollTable component
  • Added a new logic in InfiniteScrollTable.jsx for rendering the HighlightSearchTerm component

Testing

  1. Go to the History page in the portal
  2. Type query in the searchbar and confirm if the queries are bold
  3. When no matching term is found in the Job Name and/or Output Location, it will only bold the 'View Details' button since the query is inside the View Details (the query inside the View Details Modal isn't bold)

UI

image
image

Notes

@codecov
Copy link

codecov bot commented Oct 4, 2023

Codecov Report

Merging #873 (373aa9d) into main (31af29e) will increase coverage by 0.02%.
The diff coverage is 78.26%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #873      +/-   ##
==========================================
+ Coverage   63.34%   63.36%   +0.02%     
==========================================
  Files         428      429       +1     
  Lines       12250    12271      +21     
  Branches     2521     2532      +11     
==========================================
+ Hits         7760     7776      +16     
- Misses       4284     4288       +4     
- Partials      206      207       +1     
Flag Coverage Δ
javascript 69.73% <78.26%> (+0.02%) ⬆️
unittests 56.99% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...common/HighlightSearchTerm/HighlightSearchTerm.jsx 100.00% <100.00%> (ø)
...common/InfiniteScrollTable/InfiniteScrollTable.jsx 97.29% <ø> (ø)
client/src/components/Jobs/Jobs.jsx 85.50% <44.44%> (-6.43%) ⬇️

@asimregmi asimregmi marked this pull request as ready for review October 4, 2023 16:32
@asimregmi asimregmi requested a review from chandra-tacc October 4, 2023 16:32
Copy link
Member

@wesleyboar wesleyboar left a comment

Choose a reason for hiding this comment

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

New stylesheets for React components should avoid SASS, because it is global and offers limited benefits1.

Prefer a CSS Module to global CSS.

I.E. Rename to HighlightSearchTerm.scss.module.css and update how class is set.

Footnotes

  1. The benefits of SASS, for now, are only @mixin's. Native CSS supports variables and nesting.

Copy link
Member

@wesleyboar wesleyboar left a comment

Choose a reason for hiding this comment

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

I approve the rendered UI.

Regarding arguably-redundant font-weight: bold CSS, read my comment about it.

Copy link
Contributor

@shayanaijaz shayanaijaz left a comment

Choose a reason for hiding this comment

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

This solution works pretty well. I did have some concerns over code structuring and the new component that was created.

  • Whenever we create a new component in _common the goal is to have the component be as generic as possible so it can easily plug in anywhere we want to use it with little change/configuration. With the way HighlightSearchTerm is coded it is very specific to the job search functionality. If we wanted to add search term highlighting into another component in the future (e.g data files) then it would require a bit of rework to make it happen.

  • In a table each column type can have a different cell rendering structure which is taken care of in HighlightSearchTerm using if statements. The issue I see is that cell rendering structures for these columns are already defined in Jobs.jsx when we setup the columns so redefining those with slightly different html (adding bold tags and class) seems a little redundant to me. I think I would better prefer a solution that maybe uses that column definition in Jobs.jsx and for each column that needs search term highlighting we can add the HighlightSearchTerm component.

@chandra-tacc @rstijerina I'd be curious to know what y'all think

@asimregmi
Copy link
Contributor Author

@shayanaijaz I agree. I changed the implementation of HighlightSearchTerm component to make it more general and reusable. This new component is now being used by Jobs.jsx instead of InfiniteScrollTable.jsx unlike previous solution.

@asimregmi asimregmi requested a review from shayanaijaz October 6, 2023 20:11
@wesleyboar wesleyboar self-requested a review October 11, 2023 21:34
Copy link
Collaborator

@chandra-tacc chandra-tacc left a comment

Choose a reason for hiding this comment

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

Great work here. Looks good.
Please see if the additional feedback can be addressed to make this even better.

client/src/components/Jobs/Jobs.jsx Outdated Show resolved Hide resolved
const highlightParts = () => {
const parts = content.split(new RegExp(`(${searchTerm})`, 'gi'));
return parts.map((part, i) =>
part.toLowerCase() === searchTerm.toLowerCase() ? (
Copy link
Collaborator

Choose a reason for hiding this comment

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

This look great.
Minor suggestions to help improve perf.

  1. Instantiate a regex and use it for split and comparison in line 9. Doing toLowerCase on searchTerm for each part is unnecessary. You have the regex defined for it already. Regex works better on shorter strings which is the split part.
  2. If searchTerm is not available, return immediately.

I tried to see if just string replace with regex would work instead of split and replace, I did not get one working. But, may be you can try to see if that feasible?

suggestion code:

const highlightParts = () => {
    if (!searchTerm) {
      return content;
    }
    const searchTermRegex = new RegExp(`(${searchTerm})`, 'gi');
    const parts = content.split(searchTermRegex);
    return parts.map((part, i) =>
    part.match(searchTermRegex) ? (
        <b className={styles['highlight']} key={i}>
          {part}
        </b>
      ) : (
        part
      )
    );
  };

Copy link
Contributor Author

@asimregmi asimregmi Oct 23, 2023

Choose a reason for hiding this comment

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

I changed logic to use match instead of toLowerCase. Tried the other suggestion to use .replace() function instead but couldn't get it to work properly. I am getting [object] [object' inside the string that needs to be highlighted. Is it because .replace() isn't good for returning the <b>...</b> and only good for string? Let me know.

import PropTypes from 'prop-types';
import styles from './HighlightSearchTerm.module.scss';

const HighlightSearchTerm = ({ searchTerm, content }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For common components, can you please add unit tests. This helps to learn the behavior for usages and not break existing use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by adding test cases for HighlightSearchTerm. Thank you.

Copy link
Collaborator

@chandra-tacc chandra-tacc left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for adding tests and addressing comments.

@asimregmi asimregmi requested a review from shayanaijaz October 23, 2023 20:15
Copy link
Contributor

@shayanaijaz shayanaijaz left a comment

Choose a reason for hiding this comment

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

Just a small comment to fix

client/src/components/Jobs/Jobs.jsx Outdated Show resolved Hide resolved
@asimregmi asimregmi force-pushed the task/WP-72--highlight-matching-search-terms branch from d30770a to 6b24fb0 Compare October 24, 2023 18:28
@asimregmi asimregmi requested a review from shayanaijaz October 24, 2023 18:31
Copy link
Contributor

@shayanaijaz shayanaijaz left a comment

Choose a reason for hiding this comment

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

LGTM! Good work

@chandra-tacc chandra-tacc merged commit f3e7f18 into main Oct 24, 2023
6 checks passed
@chandra-tacc chandra-tacc deleted the task/WP-72--highlight-matching-search-terms branch October 24, 2023 20:16
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.

4 participants