Skip to content

Commit

Permalink
fix: add permission check for requests count badge; extract into a co…
Browse files Browse the repository at this point in the history
…mponent
  • Loading branch information
AkashRajpurohit committed Jan 6, 2025
1 parent f3931da commit 5702b75
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/components/Layout/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ const SidebarLinks: SidebarLinkProps[] = [
},
];

const PendingRequestsBadge = ({
requests,
hasPermission,
}: {
requests: RequestCountResponse | undefined;
hasPermission: (
permission: Permission | Permission[],
options?: { type: 'and' | 'or' }
) => boolean;
}) => {
if (
!hasPermission([Permission.MANAGE_REQUESTS, Permission.ADMIN], {
type: 'or',
}) ||
!requests ||
requests.pending === 0
) {
return null;
}

return (
<span className="ml-3">
<Badge badgeType="default">{requests.pending}</Badge>
</span>
);
};

const Sidebar = ({ open, setClosed }: SidebarProps) => {
const navRef = useRef<HTMLDivElement>(null);
const router = useRouter();
Expand Down Expand Up @@ -210,15 +237,12 @@ const Sidebar = ({ open, setClosed }: SidebarProps) => {
{intl.formatMessage(
menuMessages[sidebarLink.messagesKey]
)}
{sidebarLink.messagesKey === 'requests' &&
requests &&
requests.pending > 0 && (
<span className="ml-3">
<Badge badgeType="default">
{requests.pending}
</Badge>
</span>
)}
{sidebarLink.messagesKey === 'requests' && (
<PendingRequestsBadge
requests={requests}
hasPermission={hasPermission}
/>
)}
</Link>
);
})}
Expand Down Expand Up @@ -280,15 +304,12 @@ const Sidebar = ({ open, setClosed }: SidebarProps) => {
{intl.formatMessage(
menuMessages[sidebarLink.messagesKey]
)}
{sidebarLink.messagesKey === 'requests' &&
requests &&
requests.pending > 0 && (
<span className="ml-3">
<Badge badgeType="default">
{requests.pending}
</Badge>
</span>
)}
{sidebarLink.messagesKey === 'requests' && (
<PendingRequestsBadge
requests={requests}
hasPermission={hasPermission}
/>
)}
</Link>
);
})}
Expand Down

0 comments on commit 5702b75

Please sign in to comment.