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: Ratelimit identifier component changes #2712

Merged
merged 5 commits into from
Dec 9, 2024

Conversation

ogzhanolguncu
Copy link
Contributor

@ogzhanolguncu ogzhanolguncu commented Dec 6, 2024

What does this PR do?

Fixes # (issue)
ENG-1587

If there is not an issue for this, please create one first. This is used to tracking purposes and also helps use understand why this PR exists

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

Component works and causes no other issues

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features

    • Enhanced the ArrayInput component for improved item management, allowing users to add and remove items more intuitively.
    • Introduced a custom button for adding items, improving UI consistency.
  • Bug Fixes

    • Fixed synchronization issues between selected items and the input state.
  • Refactor

    • Updated internal state handling for better performance and user experience.
    • Improved layout and structure of the Filters component for enhanced presentation.

Copy link

changeset-bot bot commented Dec 6, 2024

⚠️ No Changeset found

Latest commit: 4f47372

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Dec 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:07am
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:07am
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:07am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 9:07am

Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in the ArrayInput component involve a shift from using the selected prop for item management to a local state variable items. The update includes new functions for handling item addition and unselection, ensuring synchronization between items and selected. The input handling has been improved, and the rendering logic has been updated to reflect these changes. A custom button component has replaced the previous button for adding items, resulting in enhanced UI consistency. Additionally, the Filters component layout has been modified for improved presentation without affecting its functionality.

Changes

File Change Summary
apps/dashboard/components/array-input.tsx Updated ArrayInput to manage items with local state instead of selected prop; added handleAdd function; modified handleUnselect; updated rendering logic and input handling; replaced button with a custom Button component.
apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx Adjusted CSS classes in Filters component for layout improvements; updated wrapper divs for ArrayInput and Select components without changing functionality.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ArrayInput
    participant State

    User->>ArrayInput: Enter item
    ArrayInput->>State: Update input value
    User->>ArrayInput: Press Enter
    ArrayInput->>State: Call handleAdd
    State->>ArrayInput: Add item to items
    ArrayInput->>User: Display updated items
    User->>ArrayInput: Unselect item
    ArrayInput->>State: Call handleUnselect
    State->>ArrayInput: Remove item from items
    ArrayInput->>User: Display updated items
Loading

Possibly related PRs

Suggested labels

Bug, Good first issue, 🕹️ oss.gg, :joystick: 150 points

Suggested reviewers

  • chronark
  • mcstepp
  • perkinsjr

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 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
Contributor

github-actions bot commented Dec 6, 2024

Thank you for following the naming conventions for pull request titles! 🙏

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: 3

🧹 Outside diff range and nitpick comments (4)
apps/dashboard/components/array-input.tsx (4)

15-15: Remove console.log statement

Debug statements should not be committed to production code.

-  console.log(selected);

25-32: Add input validation and memoize handler

The handleAdd function should validate input more strictly and be memoized for performance.

-  const handleAdd = () => {
+  const handleAdd = useCallback(() => {
+    const trimmedValue = inputValue.trim();
-    if (inputValue.trim()) {
+    if (trimmedValue && trimmedValue.length >= 2) {
-      const newItems = Array.from(new Set([...items, inputValue.trim()]));
+      const newItems = Array.from(new Set([...items, trimmedValue]));
       setItems(newItems);
       setSelected(newItems);
       setInputValue("");
     }
-  };
+  }, [inputValue, items, setSelected]);

65-71: Add ARIA labels and input validation attributes

The input field needs proper accessibility attributes and input validation.

          <Input
            value={inputValue}
            onChange={(e) => setInputValue(e.target.value)}
            onKeyDown={handleKeyDown}
            placeholder={placeholder}
+            aria-label={title || "Add item"}
+            minLength={2}
+            role="combobox"
+            aria-expanded="false"
            className="flex-1 w-full bg-transparent outline-none placeholder:text-content-subtle"
          />

73-75: Add aria-label to add button

The add button needs an accessible label.

-        <Button size="icon" variant="secondary" onClick={handleAdd}>
+        <Button 
+          size="icon" 
+          variant="secondary" 
+          onClick={handleAdd}
+          aria-label="Add item"
+        >
           <CornerDownLeft className="w-4 h-4" />
         </Button>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e622204 and ad525aa.

📒 Files selected for processing (1)
  • apps/dashboard/components/array-input.tsx (2 hunks)

apps/dashboard/components/array-input.tsx Outdated Show resolved Hide resolved
apps/dashboard/components/array-input.tsx Show resolved Hide resolved
apps/dashboard/components/array-input.tsx Outdated Show resolved Hide resolved
@MichaelUnkey MichaelUnkey changed the title fix: add logic fix: Ratelimit identifier component changes Dec 6, 2024
@chronark
Copy link
Collaborator

chronark commented Dec 6, 2024

CleanShot 2024-12-06 at 18 07 11@2x
it works, but it's not too pretty

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: 1

🧹 Outside diff range and nitpick comments (3)
apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx (1)

49-49: Improve layout structure with semantic HTML

The layout changes improve the visual structure, but consider adding semantic HTML elements and ARIA labels for better accessibility.

-    <div className="flex flex-row items-end justify-between gap-2">
+    <section className="flex flex-row items-end justify-between gap-2" aria-label="Filter controls">
       {props.identifier ? (
-        <div className="flex-col align-end">
+        <div className="flex-col align-end" role="group" aria-label="Identifier filters">
         ...
       {props.interval ? (
-        <div className="flex flex-col">
+        <div className="flex flex-col" role="group" aria-label="Time interval filter">

Also applies to: 51-51, 63-63

apps/dashboard/components/array-input.tsx (2)

28-35: Add input validation and error handling

The handleAdd function should validate the input format and handle potential duplicates more explicitly.

   const handleAdd = () => {
-    if (inputValue.trim()) {
+    const trimmedValue = inputValue.trim();
+    if (!trimmedValue) {
+      return;
+    }
+    if (items.includes(trimmedValue)) {
+      // Consider showing a user-friendly message
+      return;
+    }
-      const newItems = Array.from(new Set([...items, inputValue.trim()]));
+      const newItems = [...items, trimmedValue];
       setItems(newItems);
       setSelected(newItems);
       setInputValue("");
-    }
   };

79-81: Add tooltip for the add button

The button's purpose might not be immediately clear from just the icon.

-        <Button size="icon" variant="secondary" onClick={handleAdd}>
+        <Button 
+          size="icon" 
+          variant="secondary" 
+          onClick={handleAdd}
+          title="Add item"
+          aria-label="Add item to list"
+        >
           <CornerDownLeft className="w-4 h-4" />
         </Button>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ad525aa and 97d4548.

📒 Files selected for processing (2)
  • apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx (2 hunks)
  • apps/dashboard/components/array-input.tsx (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
apps/dashboard/components/array-input.tsx

[error] 53-53: The elements with the following roles can be changed to the following elements:

      For examples and more information, see WAI-ARIA Roles

      (lint/a11y/useSemanticElements)


      [error] 55-55: The elements with the following roles can be changed to the following elements:

    • For examples and more information, see WAI-ARIA Roles

      (lint/a11y/useSemanticElements)

🔇 Additional comments (2)
apps/dashboard/components/array-input.tsx (2)

15-19: LGTM: State synchronization implemented correctly

The useEffect hook correctly synchronizes the local state with the selected prop, addressing the previous review comment.


37-44: LGTM: Keyboard handling dependency fixed

The useCallback dependency array has been fixed to only include handleAdd, addressing the previous review comment.

apps/dashboard/components/array-input.tsx Outdated Show resolved Hide resolved
@MichaelUnkey
Copy link
Collaborator

CleanShot 2024-12-06 at 18 07 11@2x it works, but it's not too pretty

It is not. But we can make another with new dashboard components. Can always change it sooner, But at least it works for now.

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

🧹 Outside diff range and nitpick comments (2)
apps/dashboard/components/array-input.tsx (2)

28-35: Optimize handleAdd with useCallback

Consider memoizing the handleAdd function to prevent unnecessary recreations and optimize performance.

- const handleAdd = () => {
+ const handleAdd = useCallback(() => {
    if (inputValue.trim()) {
      const newItems = Array.from(new Set([...items, inputValue.trim()]));
      setItems(newItems);
      setSelected(newItems);
      setInputValue("");
    }
-  };
+  }, [inputValue, items, setItems, setSelected]);

73-79: Enhance input validation and user feedback

Consider adding the following improvements to the input field:

  1. Maximum length validation to prevent extremely long entries
  2. Visual feedback when attempting to add duplicate items
 <Input
   value={inputValue}
   onChange={(e) => setInputValue(e.target.value)}
   onKeyDown={handleKeyDown}
   placeholder={placeholder}
+  maxLength={100}
+  aria-invalid={items.includes(inputValue.trim())}
   className="flex-1 w-full bg-transparent outline-none placeholder:text-content-subtle"
 />

Also consider adding a helper text to show validation messages:

 <div className="flex flex-wrap items-center w-full gap-1">
   <Input
     /* ... existing props ... */
   />
+  {inputValue.trim() && items.includes(inputValue.trim()) && (
+    <span className="text-xs text-red-500">This item already exists</span>
+  )}
 </div>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 97d4548 and 097dc23.

📒 Files selected for processing (1)
  • apps/dashboard/components/array-input.tsx (2 hunks)
🔇 Additional comments (1)
apps/dashboard/components/array-input.tsx (1)

15-19: LGTM! State synchronization implemented correctly.

The implementation properly synchronizes the local items state with the selected prop through useEffect, addressing the previous review comment.

@chronark
Copy link
Collaborator

chronark commented Dec 9, 2024

fair point

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.

3 participants