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

Show social media handle #4843

Merged
merged 2 commits into from
Oct 20, 2024
Merged

Show social media handle #4843

merged 2 commits into from
Oct 20, 2024

Conversation

MohammadPCh
Copy link
Collaborator

@MohammadPCh MohammadPCh commented Oct 16, 2024

Summary by CodeRabbit

  • New Features
    • Introduced new functions for enhanced URL handling, including getSocialMediaHandle and ensureHttps.
  • Bug Fixes
    • Improved robustness in social media link display by utilizing optional chaining.
  • Documentation
    • Updated the URL reference for Next.js TypeScript documentation.

Copy link

vercel bot commented Oct 16, 2024

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

Name Status Preview Comments Updated (UTC)
giveth-dapps-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2024 2:55pm

Copy link
Contributor

coderabbitai bot commented Oct 16, 2024

Walkthrough

The pull request includes updates to the TypeScript documentation URL in next-env.d.ts, which does not impact functionality. In ProjectSocialItem.tsx, two functions were removed and replaced with imports from the @/helpers/url module, enhancing code robustness through optional chaining. The url.tsx file introduced new functions for improved URL handling, specifically for social media links. Overall, these changes focus on code optimization and documentation accuracy without altering existing functionality.

Changes

File Change Summary
next-env.d.ts Updated TypeScript documentation URL from https://nextjs.org/docs/basic-features/typescript to https://nextjs.org/docs/pages/building-your-application/configuring/typescript.
src/components/views/project/ProjectSocialItem.tsx Removed removeHttpsAndWwwFromUrl and ensureHttps functions; imported ensureHttps and getSocialMediaHandle from @/helpers/url. Updated logic to use getSocialMediaHandle with optional chaining for item?.name.
src/helpers/url.tsx Added getSocialMediaHandle, ensureHttps, and extractUsernameFromPattern functions for improved URL handling; existing functions remain unchanged.

Sequence Diagram(s)

sequenceDiagram
    participant ProjectSocialItem
    participant UrlHelper

    ProjectSocialItem->>UrlHelper: getSocialMediaHandle(socialMedia.link, socialMedia.type)
    UrlHelper-->>ProjectSocialItem: Returns formatted social media handle
Loading

🐇 "In the code where rabbits play,
Functions hop and dance today.
URLs now shine with glee,
Helpers join in harmony!
Documentation's path is bright,
Code is clean, oh what a sight!" 🐇


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

@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 (1)
src/components/views/project/ProjectSocialItem.tsx (1)

Line range hint 28-63: Consider enhancing type safety for 'item'

While the changes improve the component, there's an opportunity to further enhance type safety. Consider defining a type for the 'item' object and using type assertion or a type guard when finding it in the socialMediasArray. This would provide better IntelliSense support and catch potential type-related issues earlier.

Example:

type SocialMediaItem = {
  type: string;
  name: string;
  icon: React.ComponentType<{ color: string }>;
};

const item = socialMediasArray.find((item): item is SocialMediaItem => 
  item.type.toLowerCase() === socialMedia.type.toLowerCase()
);

This change would make the component more robust and easier to maintain in the long run.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between de88812 and a0806de.

📒 Files selected for processing (3)
  • next-env.d.ts (1 hunks)
  • src/components/views/project/ProjectSocialItem.tsx (3 hunks)
  • src/helpers/url.tsx (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • next-env.d.ts
🧰 Additional context used
🪛 Biome
src/helpers/url.tsx

[error] 212-212: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)


[error] 224-224: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (3)
src/components/views/project/ProjectSocialItem.tsx (3)

7-7: LGTM: Improved code organization with helper functions

The addition of this import statement enhances code organization by leveraging helper functions from a dedicated module. This change promotes reusability and maintainability.


41-41: LGTM: Improved code robustness with optional chaining

The use of optional chaining (item?.name) enhances code safety by preventing potential runtime errors if item is undefined.


54-58: LGTM: Enhanced social media link handling

The replacement of the previous link formatting logic with getSocialMediaHandle improves the component's functionality. This change allows for more flexible and potentially more accurate handling of different social media link formats.

cleanedUrl,
/lens\.xyz\/([^\/]+)/,
);
case 'website':
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove unnecessary 'website' case in switch statement.

The 'website' case in the switch statement is redundant because the default case already handles any cases not matched. Removing it simplifies the code without affecting functionality.

Apply this diff to remove the unnecessary case:

     case 'lens':
       // Assuming Lens uses a pattern like 'lens.xyz/username'
       return extractUsernameFromPattern(
         cleanedUrl,
         /lens\.xyz\/([^\/]+)/,
       );
-    case 'website':
     default:
       return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
   }
📝 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
case 'website':
case 'lens':
// Assuming Lens uses a pattern like 'lens.xyz/username'
return extractUsernameFromPattern(
cleanedUrl,
/lens\.xyz\/([^\/]+)/,
);
default:
return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
}
🧰 Tools
🪛 Biome

[error] 212-212: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)

regex: RegExp,
): string => {
const match = url.match(regex);
if (match && match[1]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify condition using optional chaining.

You can simplify the condition if (match && match[1]) by using optional chaining, making the code cleaner and more concise.

Apply this diff to use optional chaining:

 export const extractUsernameFromPattern = (
   url: string,
   regex: RegExp,
 ): string => {
   const match = url.match(regex);
-  if (match && match[1]) {
+  if (match?.[1]) {
     return `@${match[1]}`; // Return '@username'
   }
   return url; // Fallback to original URL if no match is found
 };
📝 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
if (match && match[1]) {
export const extractUsernameFromPattern = (
url: string,
regex: RegExp,
): string => {
const match = url.match(regex);
if (match?.[1]) {
return `@${match[1]}`; // Return '@username'
}
return url; // Fallback to original URL if no match is found
};
🧰 Tools
🪛 Biome

[error] 224-224: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

Comment on lines +160 to +215
switch (lowerCaseType) {
case 'github':
return extractUsernameFromPattern(
cleanedUrl,
/github\.com\/([^\/]+)/,
);
case 'x': // Former Twitter
return extractUsernameFromPattern(cleanedUrl, /x\.com\/([^\/]+)/);
case 'facebook':
return extractUsernameFromPattern(
cleanedUrl,
/facebook\.com\/([^\/]+)/,
);
case 'instagram':
return extractUsernameFromPattern(
cleanedUrl,
/instagram\.com\/([^\/]+)/,
);
case 'linkedin':
return extractUsernameFromPattern(
cleanedUrl,
/linkedin\.com\/(?:in|company)\/([^\/]+)/,
);
case 'youtube':
return extractUsernameFromPattern(
cleanedUrl,
/youtube\.com\/channel\/([^\/]+)/,
);
case 'reddit':
return extractUsernameFromPattern(
cleanedUrl,
/reddit\.com\/r\/([^\/]+)/,
);
case 'telegram':
return extractUsernameFromPattern(cleanedUrl, /t\.me\/([^\/]+)/);
case 'discord':
return extractUsernameFromPattern(
cleanedUrl,
/discord\.gg\/([^\/]+)/,
);
case 'farcaster':
// Assuming Farcaster uses a pattern like 'farcaster.xyz/username'
return extractUsernameFromPattern(
cleanedUrl,
/farcaster\.xyz\/([^\/]+)/,
);
case 'lens':
// Assuming Lens uses a pattern like 'lens.xyz/username'
return extractUsernameFromPattern(
cleanedUrl,
/lens\.xyz\/([^\/]+)/,
);
case 'website':
default:
return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor switch statement to use a mapping for better maintainability.

By using an object to map social media types to their corresponding regex patterns, you can simplify the code, reduce duplication, and make it easier to maintain and extend in the future.

Apply this diff to refactor the switch statement:

+  const socialMediaPatterns: { [key: string]: RegExp } = {
+    github: /github\.com\/([^\/]+)/,
+    x: /x\.com\/([^\/]+)/, // Former Twitter
+    facebook: /facebook\.com\/([^\/]+)/,
+    instagram: /instagram\.com\/([^\/]+)/,
+    linkedin: /linkedin\.com\/(?:in|company)\/([^\/]+)/,
+    youtube: /youtube\.com\/channel\/([^\/]+)/,
+    reddit: /reddit\.com\/r\/([^\/]+)/,
+    telegram: /t\.me\/([^\/]+)/,
+    discord: /discord\.gg\/([^\/]+)/,
+    farcaster: /farcaster\.xyz\/([^\/]+)/,
+    lens: /lens\.xyz\/([^\/]+)/,
+    // Add more mappings as needed
+  };
+
+  const pattern = socialMediaPatterns[lowerCaseType];
+
+  if (pattern) {
+    return extractUsernameFromPattern(cleanedUrl, pattern);
+  } else {
+    return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
+  }
-
-  switch (lowerCaseType) {
-    case 'github':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /github\.com\/([^\/]+)/,
-      );
-    case 'x': // Former Twitter
-      return extractUsernameFromPattern(cleanedUrl, /x\.com\/([^\/]+)/);
-    case 'facebook':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /facebook\.com\/([^\/]+)/,
-      );
-    case 'instagram':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /instagram\.com\/([^\/]+)/,
-      );
-    case 'linkedin':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /linkedin\.com\/(?:in|company)\/([^\/]+)/,
-      );
-    case 'youtube':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /youtube\.com\/channel\/([^\/]+)/,
-      );
-    case 'reddit':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /reddit\.com\/r\/([^\/]+)/,
-      );
-    case 'telegram':
-      return extractUsernameFromPattern(cleanedUrl, /t\.me\/([^\/]+)/);
-    case 'discord':
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /discord\.gg\/([^\/]+)/,
-      );
-    case 'farcaster':
-      // Assuming Farcaster uses a pattern like 'farcaster.xyz/username'
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /farcaster\.xyz\/([^\/]+)/,
-      );
-    case 'lens':
-      // Assuming Lens uses a pattern like 'lens.xyz/username'
-      return extractUsernameFromPattern(
-        cleanedUrl,
-        /lens\.xyz\/([^\/]+)/,
-      );
-    default:
-      return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
-  }
📝 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
switch (lowerCaseType) {
case 'github':
return extractUsernameFromPattern(
cleanedUrl,
/github\.com\/([^\/]+)/,
);
case 'x': // Former Twitter
return extractUsernameFromPattern(cleanedUrl, /x\.com\/([^\/]+)/);
case 'facebook':
return extractUsernameFromPattern(
cleanedUrl,
/facebook\.com\/([^\/]+)/,
);
case 'instagram':
return extractUsernameFromPattern(
cleanedUrl,
/instagram\.com\/([^\/]+)/,
);
case 'linkedin':
return extractUsernameFromPattern(
cleanedUrl,
/linkedin\.com\/(?:in|company)\/([^\/]+)/,
);
case 'youtube':
return extractUsernameFromPattern(
cleanedUrl,
/youtube\.com\/channel\/([^\/]+)/,
);
case 'reddit':
return extractUsernameFromPattern(
cleanedUrl,
/reddit\.com\/r\/([^\/]+)/,
);
case 'telegram':
return extractUsernameFromPattern(cleanedUrl, /t\.me\/([^\/]+)/);
case 'discord':
return extractUsernameFromPattern(
cleanedUrl,
/discord\.gg\/([^\/]+)/,
);
case 'farcaster':
// Assuming Farcaster uses a pattern like 'farcaster.xyz/username'
return extractUsernameFromPattern(
cleanedUrl,
/farcaster\.xyz\/([^\/]+)/,
);
case 'lens':
// Assuming Lens uses a pattern like 'lens.xyz/username'
return extractUsernameFromPattern(
cleanedUrl,
/lens\.xyz\/([^\/]+)/,
);
case 'website':
default:
return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
}
const socialMediaPatterns: { [key: string]: RegExp } = {
github: /github\.com\/([^\/]+)/,
x: /x\.com\/([^\/]+)/, // Former Twitter
facebook: /facebook\.com\/([^\/]+)/,
instagram: /instagram\.com\/([^\/]+)/,
linkedin: /linkedin\.com\/(?:in|company)\/([^\/]+)/,
youtube: /youtube\.com\/channel\/([^\/]+)/,
reddit: /reddit\.com\/r\/([^\/]+)/,
telegram: /t\.me\/([^\/]+)/,
discord: /discord\.gg\/([^\/]+)/,
farcaster: /farcaster\.xyz\/([^\/]+)/,
lens: /lens\.xyz\/([^\/]+)/,
// Add more mappings as needed
};
const pattern = socialMediaPatterns[lowerCaseType];
if (pattern) {
return extractUsernameFromPattern(cleanedUrl, pattern);
} else {
return cleanedUrl; // Return cleaned URL for generic websites or unsupported social media
}
🧰 Tools
🪛 Biome

[error] 212-212: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)

Copy link
Collaborator

@kkatusic kkatusic left a comment

Choose a reason for hiding this comment

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

LGTM ;) , thx @MohammadPCh

@MohammadPCh MohammadPCh merged commit 467dc02 into develop Oct 20, 2024
3 checks passed
@MohammadPCh MohammadPCh deleted the show-social-media-handle branch October 20, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: QA
Development

Successfully merging this pull request may close these issues.

2 participants