Skip to content

Commit

Permalink
Fix LinkWithParams with Suspense
Browse files Browse the repository at this point in the history
This is because of the useSearchParams() requirement
  • Loading branch information
ruchernchong committed Dec 2, 2024
1 parent b286d6e commit 0a874a3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { type ReactNode } from "react";
import React, { type ReactNode, Suspense } from "react";
import { Inter } from "next/font/google";
import Script from "next/script";
import { GoogleAnalytics } from "@next/third-parties/google";
Expand Down
10 changes: 5 additions & 5 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
const makes = await fetchApi<Make[]>(`${API_URL}/make`);

return [
{
url: SITE_URL,
lastModified: new Date(),
changeFrequency: "monthly" as const,
},
// {
// url: SITE_URL,
// lastModified: new Date(),
// changeFrequency: "monthly" as const,
// },
{
url: `${SITE_URL}/cars`,
lastModified: new Date(),
Expand Down
14 changes: 4 additions & 10 deletions components/AppSidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import Link from "next/link";
import { usePathname, useSearchParams } from "next/navigation";
import { usePathname } from "next/navigation";
import {
type IconType,
SiBluesky,
Expand All @@ -28,6 +28,7 @@ import {
} from "lucide-react";

import { BrandLogo } from "@/components/BrandLogo";
import { LinkWithParams } from "@/components/LinkWithParams";
import { NavSocialMedia } from "@/components/NavSocialMedia";
import {
Sidebar,
Expand Down Expand Up @@ -69,8 +70,6 @@ type Nav = {

export const AppSidebar = () => {
const pathname = usePathname();
const searchParams = useSearchParams();

const { setOpenMobile } = useSidebar();

return (
Expand Down Expand Up @@ -132,15 +131,10 @@ export const AppSidebar = () => {
isActive={subItem.url === pathname}
onClick={() => setOpenMobile(false)}
>
<Link
href={{
pathname: subItem.url,
query: searchParams.toString(),
}}
>
<LinkWithParams href={subItem.url}>
{subItem.icon && <subItem.icon />}
<span>{subItem.title}</span>
</Link>
</LinkWithParams>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
Expand Down
25 changes: 19 additions & 6 deletions components/LinkWithParams.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
"use client";

import type { PropsWithChildren } from "react";
import { type ComponentType, type PropsWithChildren, Suspense } from "react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";

interface Props extends PropsWithChildren {
href: string;
}

export const LinkWithParams = ({ href, children }: Props) => {
const params = useSearchParams();
const query = params.toString();

return <Link href={{ pathname: href, query }}>{children}</Link>;
const withSuspense = <P extends object>(Component: ComponentType<P>) => {
return function WithSuspenseWrapper(props: P) {
return (
<Suspense fallback={null}>
<Component {...props} />
</Suspense>
);
};
};

export const LinkWithParams = withSuspense(({ href, ...props }: Props) => {
const searchParams = useSearchParams();
return (
<Link
href={{ pathname: href, query: searchParams.toString() }}
{...props}
/>
);
});

0 comments on commit 0a874a3

Please sign in to comment.