Skip to content

Commit

Permalink
chore: remove the link if the author is not available (#169)
Browse files Browse the repository at this point in the history
* chore: remove the link if the author is not available

* chore: extract collaborator hook
  • Loading branch information
lucas-datacamp authored Dec 31, 2024
1 parent 9cad8fc commit 3c57082
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions components/PackageSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Link from 'next/link';
import { useRouter } from 'next/router';
import { FaGithub, FaHome, FaUser } from 'react-icons/fa';

import useHasCollaborator from '../helpers/hooks/useHasCollaborator';
import { copyTextToClipboard } from '../lib/utils';

import MonthlyDownloadsChart from './MonthlyDownloadsChart';
Expand Down Expand Up @@ -70,6 +71,8 @@ export default function PackageSidebar({
router.push(`/packages/${packageName}/versions/${selectedVersion}`);
}

const availableAuthor = useHasCollaborator(maintainer.name);

return (
<div className="space-y-6">
<div>
Expand Down Expand Up @@ -218,9 +221,13 @@ export default function PackageSidebar({
<div className="w-1/2">
<SidebarHeader>Maintainer</SidebarHeader>
<SidebarValue Icon={FaUser}>
<Link href={`/collaborators/name/${encodeURI(maintainer.name)}`}>
{maintainer.name}
</Link>
{availableAuthor ? (
<Link href={`/collaborators/name/${encodeURI(maintainer.name)}`}>
{maintainer.name}
</Link>
) : (
maintainer.name
)}
</SidebarValue>
</div>
{lastPublished && (
Expand Down
18 changes: 18 additions & 0 deletions helpers/hooks/useHasCollaborator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect, useState } from 'react';

const useHasCollaborator = (name: string): boolean => {
const [availableAuthor, setAvailableAuthor] = useState(false);

useEffect(() => {
const fetchCollaborator = async () => {
const res = await fetch(`/collaborators/name/${encodeURI(name)}`);
setAvailableAuthor(res.status === 200);
};

fetchCollaborator();
}, [name]);

return availableAuthor;
};

export default useHasCollaborator;

0 comments on commit 3c57082

Please sign in to comment.