Skip to content

Comments

simplify the javascript code for sanitizing and validating the dose table, and apply similar rules to other handsontables (the ability to convert 5:80 -> 06:20, disallowing typing illegal characters, removing extra colons or periods, accepting either clock or relative times depending on the mode)#188

Open
daattali wants to merge 1 commit intomasterfrom
feat-handsontables-sanitize

Conversation

@daattali
Copy link
Collaborator

No description provided.

…able, and apply similar rules to other handsontables (the ability to convert 5:80 -> 06:20, disallowing typing illegal characters, removing extra colons or periods, accepting either clock or relative times depending on the mode)
@gemini-code-assist
Copy link

Summary of Changes

Hello @daattali, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors and enhances the input validation and sanitization mechanisms for Handsontable components across the application. By centralizing JavaScript hook management and introducing dedicated functions for filtering keys and sanitizing input, the changes ensure more robust and consistent data entry, particularly for time and dose values. This improves user experience by preventing invalid input and automatically formatting data, while also making the codebase more modular and easier to maintain.

Highlights

  • Centralized Handsontable Hook Management: Introduced a new R function addHotHooks to streamline the application of JavaScript hooks (beforeKeyDown, beforeChange, afterChange) to Handsontable instances, improving code reusability and maintainability.
  • Enhanced Input Validation and Sanitization: Implemented new JavaScript hooks (hookFilterKeys, hookSanitize) to prevent illegal characters from being typed into 'Time' and 'Dose' columns and to automatically clean/format input values upon change.
  • Flexible Time Input Handling: The 'Time' column now supports both clock-based (e.g., '5:80' -> '06:20') and relative time inputs, with appropriate character filtering and sanitization based on the selected time mode.
  • Refactored Dose Table Logic: The complex logic for the main dose table (handling drug changes, unit updates, and adding new rows) was encapsulated into a dedicated hookDoseTableUpdate JavaScript function, replacing the previous changeHot function.
  • Consistent Handsontable Configuration: Applied type = "numeric" to relevant columns ('Dose', 'Target') and integrated the new filterKeys and sanitize hooks across editPriorDosesTable, editEventsTable, and targetTable for consistent behavior.
Changelog
  • R/app_server.R
    • Configured 'Dose' column in editPriorDosesTable as numeric and applied filterKeys and sanitize hooks.
    • Removed format = "0" from the 'Time' column in editEventsTable.
    • Configured 'Target' column in targetTable as numeric and applied filterKeys and sanitize hooks.
  • R/createHOT.R
    • Removed the custom JavaScript validator for the 'Dose' column.
    • Replaced manual htmlwidgets::onRender setup with a call to the new addHotHooks function, enabling filterKeys, sanitize, and hookDoseTableUpdate for afterChange.
  • R/shiny-utils.R
    • Added addHotHooks function, an R utility to programmatically attach JavaScript hooks to rhandsontable objects.
  • inst/www/hot_funs.js
    • Introduced findColByHeader to dynamically get column indices by header name.
    • Added isUserEditSource helper to check if a change originated from user input.
    • Implemented hookFilterKeys to prevent invalid key presses in 'Time' and 'Dose' columns based on time mode.
    • Implemented hookSanitize to clean 'Time' and 'Dose' values after changes.
    • Refactored the previous changeHot logic into hookDoseTableUpdate, which now handles the main dose table's dynamic updates (drug changes, unit updates, new row addition).
    • Moved removeExtraDecimal, removeExtraColon, cleanNumeric functions to a more logical position.
    • Updated addEmptyRowIfNeeded to use findColByHeader for robustness.
    • Removed the deprecated changeHot, beforeChangeHot, and beforeKeyDownHot functions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link

Preview Deployment

This PR has been deployed to: https://steveshafer.shinyapps.io/stanpumpr_PR_188/

This deployment will be automatically cleaned up when the PR is closed.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request simplifies the JavaScript code for sanitizing and validating the dose table and applies similar rules to other Handsontable instances. The changes introduce a new addHotHooks function in R/shiny-utils.R to centralize the application of beforeKeyDown and beforeChange hooks, improving code reusability and maintainability. The JavaScript logic in inst/www/hot_funs.js has been refactored to use these new hooks, and the cleanTime function has been enhanced to handle both relative and clock time formats more robustly. Additionally, type = "numeric" has been added to 'Dose' and 'Target' columns in R/app_server.R, and the format = "0" has been removed from the 'Time' column in the event table, which is a good change as time values are not strictly numeric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants