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

[Analytics] Format organisation dropdown text to uppercase #2308

Merged
merged 3 commits into from
Dec 10, 2024

Conversation

Codebmk
Copy link
Member

@Codebmk Codebmk commented Dec 10, 2024

Summary of Changes (What does this PR do?)

This pull request includes changes to the platform/src/common/components/Modal/dataDownload/components/CustomFields.jsx and platform/src/common/components/Dropdowns/OrganizationDropdown.jsx files to improve the formatting of names and provide more flexibility in text formatting. The most important changes include modifying the formatName function to support different text formats, updating the CustomFields component to accept a textFormat prop, and simplifying the formatGroupName function.

Improvements to name formatting:

Enhancements to CustomFields component:

Status of maturity (all need to be checked before merging):

  • I've tested this locally
  • I consider this code done
  • This change ready to hit production in its current state

How should this be manually tested?

  • Please include the steps to be done inorder to setup and test this PR.

What are the relevant tickets?

Screenshots (optional)

image
image

Summary by CodeRabbit

  • New Features

    • Introduced customizable text formatting options for the CustomFields component, allowing names to be displayed in uppercase or lowercase.
    • Added a new textFormat property for the "Network" field in the DataDownload component.
  • Bug Fixes

    • Improved handling of falsy values in the formatName function to prevent errors.
  • Documentation

    • Updated component props to reflect the new textFormat parameter in CustomFields.

Copy link

coderabbitai bot commented Dec 10, 2024

Warning

Rate limit exceeded

@Codebmk has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 22 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 3989a5a and 06938fd.

📒 Files selected for processing (1)
  • platform/src/common/components/Modal/dataDownload/components/CustomFields.jsx (5 hunks)
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request involve significant updates to the formatGroupName function in the OrganizationDropdown component, simplifying the formatting of group names to uppercase while removing title casing. Additionally, the CustomFields component has been modified to include a new textFormat parameter in the formatName function, allowing for customizable text formatting. The DataDownload component also incorporates this new parameter for the "Network" field. Overall, these changes enhance the flexibility and consistency of text formatting across components.

Changes

File Change Summary
platform/src/common/components/Dropdowns/OrganizationDropdown.jsx Updated formatGroupName function to convert all names to uppercase and replace underscores and hyphens with spaces. No changes to state management.
platform/src/common/components/Modal/dataDownload/components/CustomFields.jsx Modified formatName function to include textFormat parameter for customizable formatting. Added capitalize function and updated props accordingly.
platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx Added textFormat property to CustomFields for the "Network" field to allow uppercase formatting. No other significant changes.

Possibly related PRs

Suggested reviewers

  • Baalmart

🎉 In the land of code where functions play,
formatGroupName found a simpler way.
With uppercase letters, it shines so bright,
Custom fields now dance in text format light.
A dropdown, a modal, all in sync,
Together they flourish, no time to blink! 🌟


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. (Experiment)
  • @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 or @coderabbitai title 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: 2

🧹 Outside diff range and nitpick comments (3)
platform/src/common/components/Dropdowns/OrganizationDropdown.jsx (1)

Line range hint 21-29: Align formatting logic across utility functions

The getAbbreviation function still maintains special case handling for "airqo" while formatGroupName doesn't. Consider standardizing the approach across all text formatting utilities.

Consider updating the abbreviation logic to match the new formatting approach:

 const getAbbreviation = (name) => {
   if (!name || typeof name !== 'string') return 'NA';
   const trimmedName = name.trim();
   if (!trimmedName) return 'NA';
 
-  return trimmedName.toLowerCase() === 'airqo'
-    ? 'AQ'
-    : trimmedName.slice(0, 2).toUpperCase();
+  return trimmedName.slice(0, 2).toUpperCase();
 };
platform/src/common/components/Modal/dataDownload/components/CustomFields.jsx (2)

11-17: Improve readability of the formatName function

While the logic is correct, we can make it more maintainable by reducing nesting and avoiding code duplication.

 const formatName = (name, textFormat = 'lowercase') => {
   if (!name) return '';
-  return typeof name === 'string'
-    ? textFormat === 'uppercase'
-      ? name.toUpperCase().replace(/_/g, ' ').replace(/-/g, ' ')
-      : name.replace(/_/g, ' ').replace(/-/g, ' ')
-    : capitalize(name);
+  if (typeof name !== 'string') return capitalize(name);
+  
+  const formatted = name.replace(/[_-]/g, ' ');
+  return textFormat === 'uppercase' ? formatted.toUpperCase() : formatted;
 };

75-78: Consider memoizing formatted text values

Since formatName is called multiple times with the same inputs, consider memoizing the results to optimize performance.

+const formattedBtnText = useMemo(() => {
+  return formatName(btnText || selectedOption.name, textFormat);
+}, [btnText, selectedOption.name, textFormat]);

+const formattedOptionName = useCallback((name) => {
+  return formatName(name, textFormat);
+}, [textFormat]);

-btnText={formatName(btnText, textFormat) || formatName(selectedOption.name, textFormat)}
+btnText={formattedBtnText}

-<span>{formatName(option.name, textFormat)}</span>
+<span>{formattedOptionName(option.name)}</span>

Also applies to: 91-92

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a9716c0 and 3989a5a.

📒 Files selected for processing (3)
  • platform/src/common/components/Dropdowns/OrganizationDropdown.jsx (1 hunks)
  • platform/src/common/components/Modal/dataDownload/components/CustomFields.jsx (4 hunks)
  • platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • platform/src/common/components/Modal/dataDownload/modules/DataDownload.jsx
🔇 Additional comments (3)
platform/src/common/components/Dropdowns/OrganizationDropdown.jsx (2)

17-18: Consider preserving brand name formatting

While the simplified uppercase conversion makes the code cleaner, it removes special case handling for brand names like "AirQo". Consider maintaining brand-specific formatting to ensure consistent brand representation across the platform.

Let's check if there are any brand-specific formatting requirements:


17-18: Verify text formatting consistency

The uppercase transformation should align with the new textFormat prop in CustomFields component mentioned in the PR description.

Let's verify the text formatting implementation across components:

platform/src/common/components/Modal/dataDownload/components/CustomFields.jsx (1)

11-17: Verify consistent text formatting across the application

Let's ensure the new text formatting capabilities are used consistently across other components that display organization names.

Also applies to: 31-31

✅ Verification successful

The previous attempt failed because the --type jsx flag wasn't supported. Let's try without the file type restriction and use AST-based search to find similar formatting functions.


Let's try a more focused search for text formatting functions and organization name handling, specifically in JavaScript/TypeScript files. The previous search was too broad and included vendor files.


Let's narrow our search to the platform/src directory where the file under review is located, and remove the file type restrictions since they're not supported. This should help us find relevant text formatting patterns.


Text formatting is consistently implemented across the application

The codebase analysis shows that text formatting is already handled consistently:

  • The formatName function in CustomFields.jsx is the primary text formatter for organization names and similar data
  • Other components that need text formatting either:
    • Use this component directly
    • Follow the same pattern (e.g., OrganizationDropdown.jsx uses identical replace patterns)
    • Use standard capitalization for UI elements (first letter uppercase)

No inconsistencies were found in the text formatting implementation across the application.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other components that might need similar text formatting
rg -l "organization.*name" --type jsx

# Look for potential inconsistencies in text casing
rg "toUpperCase|toLowerCase" --type jsx

Length of output: 137


Script:

#!/bin/bash
# Search for other components that might need similar text formatting
rg -l "organization.*name"

# Look for potential inconsistencies in text casing
rg "toUpperCase|toLowerCase"

# Search for similar formatting functions
ast-grep --pattern 'const formatName = ($$$) => { $$$ }'

Length of output: 80466


Script:

#!/bin/bash
# Search for text formatting functions and their usage
rg -A 2 "formatName|toUpperCase|toLowerCase" --type js --type jsx --type ts --type tsx

# Search for organization name formatting
rg -A 2 "organization.*name.*\.(toUpperCase|toLowerCase|replace)" --type js --type jsx --type ts --type tsx

Length of output: 254


Script:

#!/bin/bash
# Search for text formatting functions and their usage without file type restrictions
rg -A 2 "formatName|toUpperCase|toLowerCase" platform/src/

# Search for organization name formatting
rg -A 2 "organization.*name.*\.(toUpperCase|toLowerCase|replace)" platform/src/

# Look for similar text formatting functions
ast-grep --pattern 'const formatName = ($$$) => { $$$ }' platform/src/

Length of output: 14826

Comment on lines 7 to 9
const capitalize = (name) => {
return name.charAt(0).toUpperCase() + name.slice(1);
};
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling to the capitalize function

The function should handle edge cases like null, undefined, or empty strings to prevent runtime errors.

 const capitalize = (name) => {
+  if (!name) return '';
+  if (typeof name !== 'string') return name;
   return name.charAt(0).toUpperCase() + name.slice(1);
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const capitalize = (name) => {
return name.charAt(0).toUpperCase() + name.slice(1);
};
const capitalize = (name) => {
if (!name) return '';
if (typeof name !== 'string') return name;
return name.charAt(0).toUpperCase() + name.slice(1);
};

@@ -22,6 +28,7 @@
useCalendar = false,
handleOptionSelect,
defaultOption,
textFormat = 'lowercase',
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add textFormat to PropTypes

The new textFormat prop should be documented in the component's PropTypes for better maintainability and type checking.

 CustomFields.propTypes = {
   field: PropTypes.bool,
   title: PropTypes.string,
   options: PropTypes.array,
   id: PropTypes.string,
   icon: PropTypes.node,
   btnText: PropTypes.string,
   edit: PropTypes.bool,
   useCalendar: PropTypes.bool,
   handleOptionSelect: PropTypes.func,
   defaultOption: PropTypes.object,
+  textFormat: PropTypes.oneOf(['uppercase', 'lowercase']),
 };

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor

New next-platform changes available for preview here

Copy link
Contributor

@OchiengPaul442 OchiengPaul442 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

New next-platform changes available for preview here

1 similar comment
Copy link
Contributor

New next-platform changes available for preview here

@Baalmart Baalmart merged commit 7960367 into staging Dec 10, 2024
30 of 31 checks passed
@Baalmart Baalmart deleted the hotfix-data-export branch December 10, 2024 12:09
@Baalmart Baalmart mentioned this pull request Dec 10, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants