MCPcopy
hub / github.com/umami-software/umami / useNavigation

Function useNavigation

src/components/hooks/useNavigation.ts:5–58  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3import { buildPath } from '@/lib/url';
4
5export function useNavigation() {
6 const router = useRouter();
7 const pathname = usePathname();
8 const searchParams = useSearchParams();
9 const [, teamId] = pathname.match(/\/teams\/([a-f0-9-]+)/) || [];
10 const [, websiteId] = pathname.match(/\/websites\/([a-f0-9-]+)/) || [];
11 const [, linkId] = pathname.match(/\/links\/([a-f0-9-]+)/) || [];
12 const [, pixelId] = pathname.match(/\/pixels\/([a-f0-9-]+)/) || [];
13 const [, boardId] = pathname.match(/\/boards\/([a-f0-9-]+)/) || [];
14 const [queryParams, setQueryParams] = useState(Object.fromEntries(searchParams));
15
16 const updateParams = useCallback(
17 (params?: Record<string, string | number>) => {
18 return buildPath(pathname, { ...queryParams, ...params });
19 },
20 [pathname, queryParams],
21 );
22
23 const replaceParams = useCallback(
24 (params?: Record<string, string | number>) => {
25 return buildPath(pathname, params);
26 },
27 [pathname],
28 );
29
30 const renderUrl = useCallback(
31 (path: string, params?: Record<string, string | number> | false) => {
32 return buildPath(
33 teamId ? `/teams/${teamId}${path}` : path,
34 params === false ? {} : { ...queryParams, ...params },
35 );
36 },
37 [teamId, queryParams],
38 );
39
40 useEffect(() => {
41 setQueryParams(Object.fromEntries(searchParams));
42 }, [searchParams.toString()]);
43
44 return {
45 router,
46 pathname,
47 searchParams,
48 query: queryParams,
49 teamId,
50 websiteId,
51 linkId,
52 pixelId,
53 boardId,
54 updateParams,
55 replaceParams,
56 renderUrl,
57 };
58}

Callers 15

MetricsTableFunction · 0.90
SortableLabelFunction · 0.90
FilterLinkFunction · 0.90
DataGridFunction · 0.90
useFilterParametersFunction · 0.90
useDateRangeFunction · 0.90
useWebsiteNavItemsFunction · 0.90
useFiltersFunction · 0.90
usePageParametersFunction · 0.90
usePagedQueryFunction · 0.90
UnitFilterFunction · 0.90
WebsiteFilterButtonFunction · 0.90

Calls 1

buildPathFunction · 0.90

Tested by

no test coverage detected