MCPcopy
hub / github.com/calcom/cal.diy / useUrlMatchesCurrentUrl

Function useUrlMatchesCurrentUrl

packages/lib/hooks/useUrlMatchesCurrentUrl.ts:7–24  ·  view source on GitHub ↗
(url: string, matchFullPath?: boolean)

Source from the content-addressed store, hash-verified

5import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
6
7export const useUrlMatchesCurrentUrl = (url: string, matchFullPath?: boolean) => {
8 // I don't know why usePathname ReturnType doesn't include null.
9 // It can certainly have null value https://nextjs.org/docs/app/api-reference/functions/use-pathname#:~:text=usePathname%20can%20return%20null%20when%20a%20fallback%20route%20is%20being%20rendered%20or%20when%20a%20pages%20directory%20page%20has%20been%20automatically%20statically%20optimized%20by%20Next.js%20and%20the%20router%20is%20not%20ready.
10 const pathname = usePathname() as null | string;
11 const searchParams = useCompatSearchParams();
12 const query = searchParams?.toString();
13 let pathnameWithQuery: string | null;
14 if (query) {
15 pathnameWithQuery = `${pathname}?${query}`;
16 } else {
17 pathnameWithQuery = pathname;
18 }
19 if (matchFullPath) {
20 return pathnameWithQuery ? pathnameWithQuery === url : false;
21 }
22 // TODO: It should actually re-order the params before comparing ?a=1&b=2 should match with ?b=2&a=1
23 return pathnameWithQuery ? pathnameWithQuery.includes(url) : false;
24};

Callers 2

HorizontalTabItemFunction · 0.90
VerticalTabItemFunction · 0.90

Calls 2

useCompatSearchParamsFunction · 0.90
usePathnameFunction · 0.85

Tested by

no test coverage detected