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: Save the last selected rule. #64

Merged
merged 3 commits into from
Sep 6, 2024
Merged

Conversation

omer-biz
Copy link
Contributor

@omer-biz omer-biz commented Sep 4, 2024

This address the issue I created earlier #63.

I ended up using local_storage to solve this which meant I had to enable the Storage feature on web_sys which on my system add 1kb to the wasm module.

Summary by CodeRabbit

  • New Features

    • Introduced a "Storage" feature flag to enhance data persistence capabilities.
    • Added functionality to save and retrieve the last selected rule from local storage, improving user experience by remembering user preferences.
    • Implemented a dropdown menu in the editor interface to capture and save user selections.
  • Bug Fixes

    • Enhanced error handling when accessing local storage to prevent application crashes.

Copy link

coderabbitai bot commented Sep 4, 2024

Walkthrough

The changes introduce a new feature flag "Storage" in the Cargo.toml file, enabling storage capabilities. A new function storage() is added in src/lib.rs to access local storage, while the start() function is modified to retrieve the last selected rule. In static/scripts/editor.ts, a constant for a <select> element is defined, and a new function saveRule is implemented to save user selections to local storage, enhancing the application's state management.

Changes

Files Change Summary
Cargo.toml Added a new feature flag "Storage" to the features section.
src/lib.rs Added function storage() to access local storage; modified start() to retrieve last selected rule.
static/scripts/editor.ts Defined constant editorInputSelect for a dropdown; added saveRule function to save selections to local storage.

Poem

In a world of code, so bright and new,
A rabbit hops with joy, it's true!
With storage now, we save our way,
Last rules remembered, come what may.
So leap and bound, let data flow,
In this coding garden, watch it grow! 🐇✨


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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cff773b and 1504586.

Files selected for processing (3)
  • Cargo.toml (1 hunks)
  • src/lib.rs (2 hunks)
  • static/scripts/editor.ts (3 hunks)
Additional comments not posted (6)
Cargo.toml (1)

33-33: LGTM!

The addition of the "Storage" feature flag to the web-sys dependency is a valid change that enables storage capabilities, which aligns with the PR objective of saving the last selected rule using local_storage.

static/scripts/editor.ts (3)

20-20: LGTM!

The code changes are approved.


150-153: LGTM!

The code changes are approved.


195-195: LGTM!

The code changes are approved.

src/lib.rs (2)

44-50: LGTM!

The code changes are approved.


332-335: Verify the global variable usage in the codebase.

Ensure that all usages of the LAST_SELECTION global variable are safe and consistent with the new behavior.

Run the following script to verify the global variable usage:

Verification successful

Global variable usage is safe and consistent.

The LAST_SELECTION global variable is used within unsafe blocks, ensuring safe access and modification. The changes do not negatively impact other parts of the codebase.

  • LAST_SELECTION is declared as static mut and accessed within unsafe blocks.
  • All usages are consistent with Rust's safety requirements for mutable static variables.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of the `LAST_SELECTION` global variable are safe and consistent.

# Test: Search for the global variable usage. Expect: Only occurrences within unsafe blocks.
rg --type rust -A 5 $'LAST_SELECTION'

Length of output: 813

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1504586 and 687bf38.

Files selected for processing (1)
  • static/scripts/editor.ts (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • static/scripts/editor.ts

@huacnlee
Copy link
Member

huacnlee commented Sep 5, 2024

Why not just save and load sate in JavaScript?

There is no reason to manage storage in Wasm, it will complicate things.

@omer-biz
Copy link
Contributor Author

omer-biz commented Sep 5, 2024

Even if you set the selected rule in javascript, it will be reset in the wasm module here:

site/src/lib.rs

Line 289 in cff773b

fn add_rules_to_select(mut rules: Vec<&str>) {

Copy link
Member

@huacnlee huacnlee left a comment

Choose a reason for hiding this comment

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

I have run it in local, it looks work will.

src/lib.rs Outdated Show resolved Hide resolved
@tomtau tomtau linked an issue Sep 5, 2024 that may be closed by this pull request
@tomtau
Copy link
Contributor

tomtau commented Sep 5, 2024

Would it make sense to use session storage instead of local storage?

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 687bf38 and 6bb5419.

Files selected for processing (1)
  • src/lib.rs (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/lib.rs

@omer-biz
Copy link
Contributor Author

omer-biz commented Sep 5, 2024

Would it make sense to use session storage instead of local storage?

session storage will get cleared after the browser restarts which will reset the selected rule again, while local storage will persist.

Maybe I don't I understand your point though.

@LeoDog896
Copy link
Contributor

This is a playground so it doesn't make too much sense for the info to persist across sessions.

Also, will this PR include share URL support?

@omer-biz
Copy link
Contributor Author

omer-biz commented Sep 5, 2024

This is a playground so it doesn't make too much sense for the info to persist across sessions.

Also, will this PR include share URL support?

The grammar and the input are stored in local storage, that's why I think selected rules also should be stored in local storage.

No This PR doesn't include share URL support, but it will be easy to implement with the saved selected rule.

@tomtau tomtau merged commit 9da17f6 into pest-parser:master Sep 6, 2024
3 checks passed
@nathanielknight
Copy link
Contributor

Thanks for this contribution @omer-biz !

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.

Selected Rule is Not Saved When the Site Restarts
5 participants