Skip to content

Commit

Permalink
Render links based on location
Browse files Browse the repository at this point in the history
  • Loading branch information
camilovegag committed Aug 1, 2024
1 parent 4af06aa commit 635771a
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions packages/berlin/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Menu, User } from 'lucide-react';
// API
import {
fetchEventNavLinks,
fetchEvents,
fetchNavLinks,
fetchUserRegistrations,
GetUserResponse,
Expand Down Expand Up @@ -86,12 +85,6 @@ export default function Header() {
const HeaderLinks = ({ user }: { user: GetUserResponse }) => {
const { eventId } = useParams();

const { data: events } = useQuery({
queryKey: ['events'],
queryFn: () => fetchEvents({ serverUrl: import.meta.env.VITE_SERVER_URL }),
enabled: !!user,
});

const { data: registrationsData } = useQuery({
queryKey: [user?.id, 'registrations'],
queryFn: () =>
Expand All @@ -109,7 +102,6 @@ const HeaderLinks = ({ user }: { user: GetUserResponse }) => {
refetchInterval: 10000,
});

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { data: eventNavLinks } = useQuery({
queryKey: ['eventNavLinks'],
queryFn: () =>
Expand All @@ -119,37 +111,45 @@ const HeaderLinks = ({ user }: { user: GetUserResponse }) => {
});

const links = useMemo(() => {
const baseLinks = [
{
title: 'My Proposals',
link: events ? `/events/${events?.[0].id}/register` : '',
},
{
title: 'Agenda',
link: events ? `/events/${events?.[0].id}/cycles` : '',
},
];

if (
registrationsData?.some((registration) => registration.status === 'APPROVED') &&
navLinks &&
navLinks.length > 0
) {
const additionalNavLinks = navLinks.map((eventNavLink) => ({
title: eventNavLink.title,
link: eventNavLink.link || '',
}));
return [...baseLinks, ...additionalNavLinks];
if (eventId) {
// User is within an event
const approvedRegistration =
registrationsData?.some((registration) => registration.status === 'APPROVED') ?? false;

const eventBaseLinks = [
{
title: 'My Proposals',
link: `/events/${eventId}/register`,
},
{
title: 'Agenda',
link: `/events/${eventId}/cycles`,
},
];

const fetchedEventNavLinks = eventNavLinks?.map(({ title, link }) => ({ title, link })) ?? [];

return approvedRegistration ? [...eventBaseLinks, ...fetchedEventNavLinks] : [];
} else {
// User is outside of an event
const baseLinks = [
{
title: 'Events',
link: `/events`,
},
];

const fetchedNavLinks = navLinks?.map(({ title, link }) => ({ title, link })) ?? [];

return [...baseLinks, ...fetchedNavLinks];
}

return baseLinks;
}, [events, registrationsData, navLinks]);
}, [eventId, eventNavLinks, navLinks, registrationsData]);

return links.map(({ title, link }) => (
<NavigationMenuItem key={title}>
<NavigationMenuLink asChild>
<NavLink
to={link}
to={link || ''}
className="border-secondary aria-[current=page]:border-b-2 aria-[current=page]:pb-1"
>
{title}
Expand Down

0 comments on commit 635771a

Please sign in to comment.