refactor: client side navigation (#135)
This commit is contained in:
70
apps/nextjs/src/components/layout/navigation-link.tsx
Normal file
70
apps/nextjs/src/components/layout/navigation-link.tsx
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import type { ReactNode } from "react";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { usePathname } from "next/navigation";
|
||||||
|
|
||||||
|
import { NavLink } from "@homarr/ui";
|
||||||
|
|
||||||
|
export const CommonNavLink = (props: ClientNavigationLink) =>
|
||||||
|
"href" in props ? (
|
||||||
|
<NavLinkHref {...props} />
|
||||||
|
) : (
|
||||||
|
<NavLinkWithItems {...props} />
|
||||||
|
);
|
||||||
|
|
||||||
|
const NavLinkHref = (props: NavigationLinkHref) => {
|
||||||
|
const pathname = usePathname();
|
||||||
|
return props.external ? (
|
||||||
|
<NavLink
|
||||||
|
component="a"
|
||||||
|
label={props.label}
|
||||||
|
leftSection={props.icon}
|
||||||
|
href={props.href}
|
||||||
|
style={{
|
||||||
|
borderRadius: 5,
|
||||||
|
}}
|
||||||
|
target="_blank"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<NavLink
|
||||||
|
component={Link}
|
||||||
|
label={props.label}
|
||||||
|
leftSection={props.icon}
|
||||||
|
href={props.href}
|
||||||
|
active={pathname === props.href}
|
||||||
|
style={{
|
||||||
|
borderRadius: 5,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const NavLinkWithItems = (props: NavigationLinkWithItems) => (
|
||||||
|
<NavLink
|
||||||
|
style={{
|
||||||
|
borderRadius: 5,
|
||||||
|
}}
|
||||||
|
label={props.label}
|
||||||
|
leftSection={props.icon}
|
||||||
|
>
|
||||||
|
{props.items.map((item) => (
|
||||||
|
<NavLinkHref key={item.label} {...item} />
|
||||||
|
))}
|
||||||
|
</NavLink>
|
||||||
|
);
|
||||||
|
|
||||||
|
interface CommonNavigationLinkProps {
|
||||||
|
label: string;
|
||||||
|
icon: ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NavigationLinkHref extends CommonNavigationLinkProps {
|
||||||
|
href: string;
|
||||||
|
external?: boolean;
|
||||||
|
}
|
||||||
|
interface NavigationLinkWithItems extends CommonNavigationLinkProps {
|
||||||
|
items: NavigationLinkHref[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ClientNavigationLink = NavigationLinkHref | NavigationLinkWithItems;
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
import Link from "next/link";
|
|
||||||
|
|
||||||
import {
|
|
||||||
AppShellNavbar,
|
|
||||||
AppShellSection,
|
|
||||||
NavLink,
|
|
||||||
ScrollArea,
|
|
||||||
} from "@homarr/ui";
|
|
||||||
import type { TablerIconsProps } from "@homarr/ui";
|
import type { TablerIconsProps } from "@homarr/ui";
|
||||||
|
import { AppShellNavbar, AppShellSection, ScrollArea } from "@homarr/ui";
|
||||||
|
|
||||||
|
import type { ClientNavigationLink } from "./navigation-link";
|
||||||
|
import { CommonNavLink } from "./navigation-link";
|
||||||
|
|
||||||
interface MainNavigationProps {
|
interface MainNavigationProps {
|
||||||
headerSection?: JSX.Element;
|
headerSection?: JSX.Element;
|
||||||
@@ -28,51 +24,31 @@ export const MainNavigation = ({
|
|||||||
mb={footerSection ? "md" : undefined}
|
mb={footerSection ? "md" : undefined}
|
||||||
component={ScrollArea}
|
component={ScrollArea}
|
||||||
>
|
>
|
||||||
{links.map((link) => (
|
{links.map((link, index) => {
|
||||||
<CommonNavLink key={link.label} {...link} />
|
const { icon: TablerIcon, ...props } = link;
|
||||||
))}
|
const Icon = <TablerIcon size={20} stroke={1.5} />;
|
||||||
|
let clientLink: ClientNavigationLink;
|
||||||
|
if ("items" in props) {
|
||||||
|
clientLink = {
|
||||||
|
...props,
|
||||||
|
items: props.items.map((item) => {
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
icon: <item.icon size={20} stroke={1.5} />,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
} as ClientNavigationLink;
|
||||||
|
} else {
|
||||||
|
clientLink = props as ClientNavigationLink;
|
||||||
|
}
|
||||||
|
return <CommonNavLink key={index} {...clientLink} icon={Icon} />;
|
||||||
|
})}
|
||||||
</AppShellSection>
|
</AppShellSection>
|
||||||
{footerSection && <AppShellSection>{footerSection}</AppShellSection>}
|
{footerSection && <AppShellSection>{footerSection}</AppShellSection>}
|
||||||
</AppShellNavbar>
|
</AppShellNavbar>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const CommonNavLink = (props: NavigationLink) =>
|
|
||||||
"href" in props ? (
|
|
||||||
<NavLinkHref {...props} />
|
|
||||||
) : (
|
|
||||||
<NavLinkWithItems {...props} />
|
|
||||||
);
|
|
||||||
|
|
||||||
const NavLinkHref = (props: NavigationLinkHref) =>
|
|
||||||
props.external ? (
|
|
||||||
<NavLink
|
|
||||||
component="a"
|
|
||||||
label={props.label}
|
|
||||||
leftSection={<props.icon size={20} stroke={1.5} />}
|
|
||||||
href={props.href}
|
|
||||||
target="_blank"
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<NavLink
|
|
||||||
component={Link}
|
|
||||||
label={props.label}
|
|
||||||
leftSection={<props.icon size={20} stroke={1.5} />}
|
|
||||||
href={props.href}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
const NavLinkWithItems = (props: NavigationLinkWithItems) => (
|
|
||||||
<NavLink
|
|
||||||
label={props.label}
|
|
||||||
leftSection={<props.icon size={20} stroke={1.5} />}
|
|
||||||
>
|
|
||||||
{props.items.map((item) => (
|
|
||||||
<NavLinkHref key={item.label} {...item} />
|
|
||||||
))}
|
|
||||||
</NavLink>
|
|
||||||
);
|
|
||||||
|
|
||||||
interface CommonNavigationLinkProps {
|
interface CommonNavigationLinkProps {
|
||||||
label: string;
|
label: string;
|
||||||
icon: (props: TablerIconsProps) => JSX.Element;
|
icon: (props: TablerIconsProps) => JSX.Element;
|
||||||
@@ -85,4 +61,5 @@ interface NavigationLinkHref extends CommonNavigationLinkProps {
|
|||||||
interface NavigationLinkWithItems extends CommonNavigationLinkProps {
|
interface NavigationLinkWithItems extends CommonNavigationLinkProps {
|
||||||
items: NavigationLinkHref[];
|
items: NavigationLinkHref[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NavigationLink = NavigationLinkHref | NavigationLinkWithItems;
|
export type NavigationLink = NavigationLinkHref | NavigationLinkWithItems;
|
||||||
|
|||||||
Reference in New Issue
Block a user