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

Fix adding new tags in tagging action #5378

Merged
merged 1 commit into from
Jan 14, 2025

Conversation

benjaminpkane
Copy link
Contributor

@benjaminpkane benjaminpkane commented Jan 12, 2025

What changes are proposed in this pull request?

Closes #5328

  • Stop event propagation in tagger input when creating a new tag entry
  • Clear input after creating a new tag entry
Screen.Recording.2025-01-12.at.2.22.04.PM.mov

Release Notes

  • Fix manual input of new tags in tagging action

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

  • Type Safety Improvements

    • Enhanced type definitions for Checker and Tagger components
    • Added null handling for active states and properties
    • Improved type annotations for utility functions
  • New Features

    • Introduced clear functionality in components
    • Added event tracking capabilities
  • Bug Fixes

    • Prevented event bubbling in input handling
    • Refined state management for component interactions
  • Performance

    • Updated dependency tracking for layout effects
    • Optimized state reset mechanisms

@benjaminpkane benjaminpkane added bug Bug fixes app Issues related to App features labels Jan 12, 2025
@benjaminpkane benjaminpkane requested review from lanzhenw, sashankaryal and a team January 12, 2025 19:23
@benjaminpkane benjaminpkane self-assigned this Jan 12, 2025
Copy link
Contributor

coderabbitai bot commented Jan 12, 2025

Walkthrough

The pull request focuses on enhancing type safety and resolving potential state management issues in several React components within the Actions module. The changes primarily involve modifying type definitions to allow null values for certain properties, updating event handling logic, and introducing a new clear functionality. These modifications aim to provide more flexible and robust state management, particularly in tag and check-related components.

Changes

File Changes
app/packages/core/src/components/Actions/Checker.tsx - Updated CheckProps and CheckerProps interfaces to allow null values for count and active
- Modified setActive function parameter type
- Added clear function to CheckerProps
app/packages/core/src/components/Actions/Tagger.tsx - Added useTrackEvent import
- Updated active state to allow null or string
- Modified onKeyDown event handler
- Added clear prop to Checker component
app/packages/core/src/components/Actions/utils.tsx - Added type annotations to useHighlightHover function parameters

Assessment against linked issues

Objective Addressed Explanation
Resolve random tag selection issue [#5328]

Possibly related PRs

Suggested reviewers

  • paularamo
  • imanjra

Poem

🐰 In the realm of code, where types dance and sway,
Null values now frolic, no longer astray
Checkers and taggers, with flexibility bright
Debugging magic, now clear as daylight!
A rabbit's delight in type-safe design 🔍


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

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
Contributor

@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

🧹 Nitpick comments (5)
app/packages/core/src/components/Actions/utils.tsx (1)

34-38: LGTM! Type annotations enhance type safety.

The added TypeScript type annotations improve type safety and make the function signature more explicit. This change aligns with React/TypeScript best practices.

Consider documenting the function parameters.

While the types are now explicit, adding JSDoc comments would further improve maintainability by documenting the purpose of each parameter.

Add documentation like this:

/**
 * Hook to manage hover state and styling for interactive elements
 * @param disabled - Whether the hover effect is disabled
 * @param override - Optional override for the hover state
 * @param color - Optional custom color for the hover effect
 */
export const useHighlightHover = (
app/packages/core/src/components/Actions/Checker.tsx (3)

97-97: Consider simplifying the count display logic

The current implementation can be more concise while maintaining the same functionality.

-          {(count ?? 0) > 0 ? numeral(count).format("0,0") : "0"}
+          {numeral(count ?? 0).format("0,0")}

182-194: Consider simplifying the index calculation logic

The index calculation and boundary checking can be simplified using the modulo operator.

-    let index: null | number = null;
-    if (e.key === "ArrowDown") {
-      index = active === null ? 0 : names.indexOf(active) + 1;
-    } else if (e.key === "ArrowUp") {
-      index = active === null ? names.length - 1 : names.indexOf(active) - 1;
-    }
-
-    if (typeof index === "number") {
-      if (index < 0) {
-        index = names.length - 1;
-      } else if (index > names.length - 1) {
-        index = 0;
-      }
+    if (e.key === "ArrowDown" || e.key === "ArrowUp") {
+      const currentIndex = active === null ? -1 : names.indexOf(active);
+      const delta = e.key === "ArrowDown" ? 1 : -1;
+      const index = ((currentIndex + delta) + names.length) % names.length;
       setActive(names[index]);
     }

67-69: Consider extracting the click target check to improve readability

The click handler's condition could be more readable by extracting the target check logic.

+      const isCheckboxClicked = (target: EventTarget | null): boolean => 
+        target === ref.current || ref.current?.contains(target as Node);
+
       onClick={(e) =>
-        !disabled &&
-        !(
-          e.target === ref.current || ref.current?.contains(e.target as Node)
-        ) &&
-        onCheck()
+        !disabled && !isCheckboxClicked(e.target) && onCheck()
       }
app/packages/core/src/components/Actions/Tagger.tsx (1)

188-193: Consider adding input validation.

The current implementation should validate the input value to prevent creating empty tags or tags with only whitespace.

 onKeyDown={(e) => {
   if (e.key === "Enter" && hasCreate) {
+    const trimmedValue = value.trim();
+    if (!trimmedValue) return;
     e.stopPropagation();
     setValue("");
     setActive(null);
-    setChanges({ ...changes, [value]: CheckState.ADD });
+    setChanges({ ...changes, [trimmedValue]: CheckState.ADD });
   }
 }}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5801576 and 9086d85.

📒 Files selected for processing (3)
  • app/packages/core/src/components/Actions/Checker.tsx (8 hunks)
  • app/packages/core/src/components/Actions/Tagger.tsx (5 hunks)
  • app/packages/core/src/components/Actions/utils.tsx (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
app/packages/core/src/components/Actions/utils.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/core/src/components/Actions/Tagger.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/core/src/components/Actions/Checker.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.11)
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.10)
  • GitHub Check: test / test-python (ubuntu-latest-m, 3.9)
  • GitHub Check: lint / eslint
  • GitHub Check: test / test-app
  • GitHub Check: build / build
  • GitHub Check: e2e / test-e2e
🔇 Additional comments (6)
app/packages/core/src/components/Actions/Checker.tsx (2)

33-37: Type safety improvements look good!

The explicit null handling in interface properties improves type safety and makes the component's behavior more predictable. The changes are consistent across both CheckProps and CheckerProps interfaces.

Also applies to: 154-161


207-207: LGTM! Clear functionality properly integrated

The clear functionality is correctly integrated with the keyboard navigation handler, maintaining consistency with the PR objectives of clearing input after actions.

app/packages/core/src/components/Actions/Tagger.tsx (4)

102-102: Great type safety improvement!

The explicit type definition for active state helps prevent potential runtime errors and improves code maintainability.


Line range hint 104-112: Good fix for hook dependencies!

Adding close to the useLayoutEffect dependencies list ensures proper cleanup when the tagging state changes.


189-191: Good fixes for event handling and state management!

The changes correctly prevent event bubbling and clear the input state after tag creation, improving the user experience.


205-205: Good addition of the clear prop!

The new clear prop follows React's unidirectional data flow pattern for resetting the input state.

@benjaminpkane benjaminpkane changed the base branch from develop to release/v1.3.0 January 14, 2025 16:41
@benjaminpkane benjaminpkane enabled auto-merge (squash) January 14, 2025 18:11
Copy link
Contributor

@sashankaryal sashankaryal left a comment

Choose a reason for hiding this comment

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

🍾

@benjaminpkane benjaminpkane merged commit 4745698 into release/v1.3.0 Jan 14, 2025
12 checks passed
@benjaminpkane benjaminpkane deleted the bugfix/app-tagging branch January 14, 2025 21:01
@lanzhenw
Copy link
Contributor

Was caught in meetings earlier.
🚀 Nice work adding more type definitions and safeguards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app Issues related to App features bug Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Random Tag from tag list is selected
3 participants