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

Refactor: Reduce unnecessary map #47

Open
wit03 opened this issue Oct 9, 2024 · 0 comments
Open

Refactor: Reduce unnecessary map #47

wit03 opened this issue Oct 9, 2024 · 0 comments

Comments

@wit03
Copy link
Member

wit03 commented Oct 9, 2024

🛠️ Refactor suggestion_

Refactor rendering logic to reduce duplication and improve type safety.

The current implementation handles both object and string types in the data.github array, which is flexible. However, this leads to code duplication and potential type safety issues. Consider refactoring to improve maintainability and type safety.

Here's a suggested refactor:

import { ReactNode } from 'react';

type GitHubItem = string | { url: string };

const GitHubLink = ({ href, children }: { href: string; children: ReactNode }) => (
  <Link
    href={href}
    target="_blank"
    className="flex items-center gap-2 rounded-lg bg-slate-50 px-6 py-4 min-w-72 flex-grow text-base font-medium line-clamp-2 hover:text-primaryRed"
  >
    <Image
      src="/logo/github.svg"
      alt="github logo"
      className="rounded-full mb-1 bg-white"
      width={30}
      height={24}
    />
    {children}
  </Link>
);

// In the component's return statement:
<div className="flex flex-row flex-wrap gap-4">
  {data.github.length === 0 ? (
    <p className="text-base font-normal text-gray-600">
      There is no github repository for this project.
    </p>
  ) : (
    data.github.map((item, i) => {
      const url = typeof item === 'string' ? item : item.url;
      return (
        <GitHubLink key={i} href={url}>
          {formatGithubLink(url)}
        </GitHubLink>
      );
    })
  )}
</div>

This refactoring:

  1. Creates a reusable GitHubLink component.
  2. Uses a type union GitHubItem to properly type the data.github array.
  3. Simplifies the rendering logic by handling both cases in a single map function.
  4. Improves readability and maintainability of the code.

Originally posted by @coderabbitai[bot] in #46 (comment)

@wit03 wit03 changed the title _:hammer_and_wrench: Refactor suggestion Refactor: Refactor suggestion Oct 9, 2024
@wit03 wit03 changed the title Refactor: Refactor suggestion Refactor: Reduce unnecessary map Oct 9, 2024
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

No branches or pull requests

1 participant