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

feat(tools): add search preview functionality #12470

Merged
merged 8 commits into from
Oct 3, 2024

Conversation

meel-hd
Copy link
Contributor

@meel-hd meel-hd commented Sep 11, 2024

Proposed changes

This commit adds the functionality to display a search preview when typing in the search input field. The search preview shows a list of search results that match the user's input. It also highlights the matching text in the search results. The search preview is displayed below the search input field and disappears after a delay when the input field loses focus.

  • The search also uses filters if provided.
    The commit includes the following changes:
  • Added a new file weblate/static/editor/tools/search.js that contains the JavaScript code for the search preview functionality.
  • Added CSS styles in weblate/static/styles/main.css to position and style the search preview element.

Related: #4106

Checklist

  • Lint and unit tests pass locally with my changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added documentation to describe my feature.
  • I have squashed my commits into logic units.
  • I have described the changes in the commit messages.

Other information

Note

GET api/units with the query q is case insensitive. In contrast to the underlying search in Search and Replace. If the preview consumed it solely, its results are inaccurate. This is avoided with client side filtering for accurate preview considering case sensitivity.

I think we should consider this. as case sensitive search is important for some parts of the application.

Preview

image

Summary by CodeRabbit

  • New Features

    • Introduced a dynamic search preview feature that provides real-time feedback on search results as users type.
    • Enhanced search experience with clickable results that open in new tabs and highlight relevant text.
  • Style Enhancements

    • Added new CSS styles for the search preview component to improve aesthetics and user interaction, including hover effects for search results.
  • Documentation

    • Updated HTML template to include the new search functionality script, enhancing the web application’s capabilities.

Copy link

coderabbitai bot commented Sep 11, 2024

Walkthrough

This pull request introduces a new search functionality within the web application, implemented through a JavaScript file (search.js). It includes a dynamic search preview feature that displays real-time results based on user input. The changes also involve the addition of CSS styles to enhance the visual presentation of the search results and the inclusion of the new JavaScript file in the main HTML template.

Changes

Files Change Summary
weblate/static/editor/tools/search.js Added functions for dynamic search preview, query building, and result highlighting.
weblate/static/styles/main.css Added CSS styles for the #search-preview component and its child elements to enhance UI.
weblate/templates/base.html Included a script tag to load the search.js file, enabling the new search functionality.

Tip

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8e4a6d3 and 0f9b535.

Files selected for processing (1)
  • weblate/static/editor/tools/search.js (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • weblate/static/editor/tools/search.js

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
weblate/static/editor/tools/search.js (1)

18-122: Consider breaking down the function into smaller functions for better readability and maintainability.

The searchPreview function is quite large and could be broken down into smaller functions for better readability and maintainability. For example, you could extract the logic for fetching search results, filtering irrelevant results, and highlighting matching text into separate functions.

Tools
GitHub Check: CodeQL

[failure] 70-70: Inefficient regular expression
This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '.'.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2d4298e and 8e4a6d3.

Files selected for processing (3)
  • weblate/static/editor/tools/search.js (1 hunks)
  • weblate/static/styles/main.css (1 hunks)
  • weblate/templates/base.html (1 hunks)
Additional context used
GitHub Check: CodeQL
weblate/static/editor/tools/search.js

[failure] 70-70: Inefficient regular expression
This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '.'.

Additional comments not posted (9)
weblate/static/editor/tools/search.js (4)

18-122: The debounce implementation, client-side filtering, and highlighting of matching text are well implemented.

  • The debounce implementation correctly prevents too many requests while the user is typing.
  • The client-side filtering of irrelevant results is a good optimization to improve the search preview accuracy.
  • The highlighting of matching text in the search results enhances the user experience.
Tools
GitHub Check: CodeQL

[failure] 70-70: Inefficient regular expression
This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '.'.


136-154: LGTM!

The buildSearchQuery function correctly builds the search query string in the expected format by concatenating the user input with the path lookup and filters (if provided).


164-184: LGTM!

The highlightMatch function correctly highlights the matching text in the search results using a regular expression.


170-170: The regular expression is not likely to cause performance issues in this case.

The static analysis tool CodeQL has flagged the regular expression at line 170-170 as potentially inefficient due to the use of the '.' character. However, this is not likely to cause performance issues in this case because:

  • The search queries are typically short.
  • The number of search results is limited by the fetchMax parameter in the searchPreview function.
weblate/templates/base.html (1)

80-80: LGTM!

The script tag to load search.js is correctly added with the necessary attributes.

weblate/static/styles/main.css (4)

2114-2126: LGTM!

The CSS styles for #search-preview are well-defined and enhance the visual presentation of the search results section. The changes are approved.


2128-2135: Looks good!

The CSS styles for #search-preview > #search-result are properly defined. The changes are approved.


2137-2139: Hover styles look good!

The hover styles for #search-preview > #search-result are properly defined to provide visual feedback. The changes are approved.


2141-2143: Margin styles are good!

The left margin styles for #search-preview > #search-result > div are properly defined. The changes are approved.

Copy link
Member

@nijel nijel left a comment

Choose a reason for hiding this comment

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

The preview really makes sense for the filter field above, not this one. The filter supports all queries, while the search is a simple string to replace in the translation.

weblate/static/editor/tools/search.js Fixed Show resolved Hide resolved
weblate/static/editor/tools/search.js Show resolved Hide resolved
@meel-hd
Copy link
Contributor Author

meel-hd commented Sep 13, 2024

Oh!? I think the main operation here is concerned with the strings to replace. And filters are additional. In the current solution the preview is mainly based on the search string while considering the filters. I don't think adding search preview to filters is the best option. as users usually don't search for filters but for strings.

@meel-hd
Copy link
Contributor Author

meel-hd commented Sep 23, 2024

@nijel this commit e3bd4d moved the search preview to filters as you suggested.

Preview

Recording.2024-09-23.125605.mp4

@meel-hd meel-hd requested a review from nijel September 26, 2024 09:05
nijel added a commit to nijel/weblate that referenced this pull request Sep 26, 2024
weblate/static/editor/tools/search.js Outdated Show resolved Hide resolved
weblate/static/editor/tools/search.js Outdated Show resolved Hide resolved
weblate/static/editor/tools/search.js Show resolved Hide resolved
weblate/static/editor/tools/search.js Outdated Show resolved Hide resolved
nijel added a commit to nijel/weblate that referenced this pull request Sep 26, 2024
nijel added a commit that referenced this pull request Sep 26, 2024
@nijel nijel added this to the 5.8 milestone Oct 3, 2024
@nijel nijel linked an issue Oct 3, 2024 that may be closed by this pull request
@nijel nijel force-pushed the add-search-preview-4106 branch from dd73d08 to 01eab90 Compare October 3, 2024 08:34
@nijel nijel requested a review from orangesunny as a code owner October 3, 2024 08:34
meel-hd and others added 6 commits October 3, 2024 10:36
This commit adds the functionality to display a search preview when typing in the search input field. The search preview shows a list of search results that match the user's input. It also highlights the matching text in the search results. The search preview is displayed below the search input field and disappears after a delay when the input field loses focus.

The commit includes the following changes:
- Added a new file `weblate/static/editor/tools/search.js` that contains the JavaScript code for the search preview functionality.
- Added CSS styles in `weblate/static/styles/main.css` to position and style the search preview element.
…function

- Improved RegEx while Making the search result url relative. because the regular expression may cause exponential backtracking on strings containing many repetitions of '.'

- Extracted the logic for rendering search results into a separate function called "showResults". This improves code readability and maintainability by separating concerns and reducing the complexity of the main function.
- Instead of displaying the total number of search results, it now shows the number of matching strings found.
- Instead of getting project path from url get it from the Form data.

- Update the `showResults` function to accept the `count` parameter, which represents the number of search results.
- Modify the logic in the `showResults` function to use the `count` parameter instead of the length of the `results` array.
- Adjust the CSS style for the `#results-num` element to increase the font size to 16px.
@nijel nijel force-pushed the add-search-preview-4106 branch from 01eab90 to eea451f Compare October 3, 2024 08:36
Copy link
Member

@nijel nijel left a comment

Choose a reason for hiding this comment

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

Looks good. I've fixed merge conflicts and added a changelog entry.

if (count > 0) {
const t = ngettext("%s matching string", "%s matching strings", count);
$searchPreview.append(
`<h4 id="results-num">${interpolate(t, [count])}</h4>`,
Copy link
Member

Choose a reason for hiding this comment

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

This could be a link to /search/?q=... with the search used in the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah! okay

@nijel nijel enabled auto-merge (squash) October 3, 2024 10:21
@nijel nijel merged commit 89dd315 into WeblateOrg:main Oct 3, 2024
33 of 34 checks passed
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.

Live preview of search queries
3 participants