Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/frontend/lib/functions/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { NavigateFunction } from 'react-router-dom';
import { ModelInformationDict } from '../enums/ModelInformation';
import type { ModelType } from '../enums/ModelType';
import { apiUrl } from './Api';
import { cancelEvent } from './Events';

// NavigateFunction type compatible with both React Router and TanStack Router
export type NavigateFunction = (to: string | number, options?: any) => void;

export const getBaseUrl = (): string =>
(window as any).INVENTREE_SETTINGS?.base_url || 'web';

Expand Down
14 changes: 14 additions & 0 deletions src/frontend/lib/functions/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Navigation utilities for TanStack Router
* Provides compatibility layer between React Router v6 and TanStack Router
*/

// Export types and functions from custom hooks
export type { NavigateFunction, NavigateOptions } from '../hooks/UseNavigate';
export { useNavigate } from '../hooks/UseNavigate';
export { useSearchParams } from '../hooks/UseSearchParams';

/**
* Type for Link 'to' prop
*/
export type To = string;
40 changes: 40 additions & 0 deletions src/frontend/lib/hooks/UseNavigate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Custom hook that provides React Router v6-like useNavigate behavior
* This makes migration from React Router to TanStack Router easier
*/

import { useNavigate as useTanstackNavigate } from '@tanstack/react-router';
import { useCallback } from 'react';

export type NavigateOptions = {
replace?: boolean;
state?: any;
};

export type NavigateFunction = (
to: string | number,
options?: NavigateOptions
) => void;

/**
* Hook that returns a navigate function compatible with React Router v6
*/
export function useNavigate(): NavigateFunction {
const tanstackNavigate = useTanstackNavigate();

return useCallback(
((to: string | number, options?: NavigateOptions) => {
if (typeof to === 'number') {
window.history.go(to);
return;
}

tanstackNavigate({
to: to as any,
replace: options?.replace,
state: options?.state
} as any);
}) as NavigateFunction,
[tanstackNavigate]
);
}
39 changes: 39 additions & 0 deletions src/frontend/lib/hooks/UseSearchParams.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Custom hook that provides React Router v6-like useSearchParams behavior
* This makes migration from React Router to TanStack Router easier
*/

import { useNavigate as useTanstackNavigate, useSearch } from '@tanstack/react-router';
import { useCallback, useMemo } from 'react';

/**
* Hook that returns search params compatible with React Router v6
* Returns [URLSearchParams, setSearchParams] tuple
*/
export function useSearchParams(): [URLSearchParams, (params: Record<string, any>) => void] {
const search = useSearch({ strict: false }) as Record<string, any>;
const navigate = useTanstackNavigate();

const searchParams = useMemo(() => {
const params = new URLSearchParams();
if (search) {
Object.entries(search).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
params.set(key, String(value));
}
});
}
return params;
}, [search]);

const setSearchParams = useCallback(
(params: Record<string, any>) => {
navigate({
search: params as any
} as any);
},
[navigate]
);

return [searchParams, setSearchParams];
}
2 changes: 1 addition & 1 deletion src/frontend/lib/types/Plugins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { I18n } from '@lingui/core';
import type { MantineColorScheme, MantineTheme } from '@mantine/core';
import type { QueryClient } from '@tanstack/react-query';
import type { AxiosInstance } from 'axios';
import type { NavigateFunction } from 'react-router-dom';
import type { NavigateFunction } from '../functions/Navigation';
import type { ModelDict } from '../enums/ModelInformation';
import type { ModelType } from '../enums/ModelType';
import type {
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/lib/types/Tables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import type {
DataTableRowExpansionProps
} from 'mantine-datatable';
import type { ReactNode } from 'react';
import type { NavigateFunction, SetURLSearchParams } from 'react-router-dom';
import type { ModelType } from '../enums/ModelType';
import type { FilterSetState, TableFilter } from './Filters';
import type { ApiFormFieldType } from './Forms';
import type { NavigateFunction } from '../functions/navigation';

// Type for setting search params - compatible with React Router v6 and our wrapper
type SetURLSearchParams = (params: Record<string, any>) => void;

/*
* Type definition for representing the state of a table:
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
"@sentry/react": "^10.7.0",
"@tabler/icons-react": "^3.17.0",
"@tanstack/react-query": "^5.56.2",
"@tanstack/react-router": "^1.150.0",
"@tanstack/router-devtools": "^1.150.0",
"@uiw/codemirror-theme-vscode": "4.25.1",
"@uiw/react-codemirror": "4.25.1",
"@uiw/react-split": "^5.9.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { t } from '@lingui/core/macro';
import { Box, Divider, Modal } from '@mantine/core';
import { hideNotification, showNotification } from '@mantine/notifications';
import { useCallback, useState } from 'react';
import { type NavigateFunction, useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';
import { api } from '../../App';
import type { NavigateFunction } from '@lib/functions/navigation';
import { extractErrorMessage } from '../../functions/api';
import { useUserState } from '../../states/UserState';
import { StylishText } from '../items/StylishText';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/calendar/OrderCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@tabler/icons-react';
import dayjs from 'dayjs';
import { useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';
import { api } from '../../App';
import useCalendar from '../../hooks/UseCalendar';
import { useUserState } from '../../states/UserState';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ActionIcon, Anchor, Group, Loader } from '@mantine/core';
import { IconExclamationCircle } from '@tabler/icons-react';
import { useQuery } from '@tanstack/react-query';
import { type ReactNode, useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import { ModelInformationDict } from '@lib/enums/ModelInformation';
import type { ModelType } from '@lib/enums/ModelType';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { useQuery } from '@tanstack/react-query';
import { getValueAtPath } from 'mantine-datatable';
import { useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import { ProgressBar } from '@lib/components/ProgressBar';
import { YesNoButton } from '@lib/components/YesNoButton';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/errors/GenericErrorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Text
} from '@mantine/core';
import { IconArrowBack, IconExclamationCircle } from '@tabler/icons-react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import { LanguageContext } from '../../contexts/LanguageContext';

Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/components/forms/ApiForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
type SubmitHandler,
useForm
} from 'react-hook-form';
import { type NavigateFunction, useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';
import type { NavigateFunction } from '@lib/functions/navigation';

import { isTrue } from '@lib/functions/Conversion';
import { getDetailUrl } from '@lib/functions/Navigation';
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/components/forms/AuthenticationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { useForm } from '@mantine/form';
import { useDisclosure } from '@mantine/hooks';
import { showNotification } from '@mantine/notifications';
import { useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useLocation } from '@tanstack/react-router';
import { useNavigate } from '@lib/functions/navigation';
import { useShallow } from 'zustand/react/shallow';
import { api } from '../../App';
import {
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/components/items/InfoItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Trans } from '@lingui/react/macro';
import { Code, Flex, Group, Text } from '@mantine/core';
import { Link, type To } from 'react-router-dom';
import { Link } from '@tanstack/react-router';
import type { To } from '@lib/functions/navigation';

import { YesNoButton } from '@lib/components/YesNoButton';
import { DetailDrawerLink } from '../nav/DetailDrawer';
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/src/components/items/InvenTreeLogo.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { t } from '@lingui/core/macro';
import { ActionIcon } from '@mantine/core';
import { forwardRef } from 'react';
import { NavLink } from 'react-router-dom';
import { Link } from '@tanstack/react-router';

import InvenTreeIcon from './inventree.svg';

export const InvenTreeLogoHomeButton = forwardRef<HTMLDivElement>(
(props, ref) => {
return (
<div ref={ref} {...props}>
<NavLink to={'/'}>
<Link to={'/' as any}>
<ActionIcon size={28} variant='transparent'>
<InvenTreeLogo />
</ActionIcon>
</NavLink>
</Link>
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/items/MenuLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
UnstyledButton
} from '@mantine/core';
import { type JSX, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import { navigateToLink } from '@lib/functions/Navigation';
import type { InvenTreeIconType } from '@lib/types/Icons';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/modals/QrModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {} from '@mantine/core';
import type { ContextModalProps } from '@mantine/modals';
import type { NavigateFunction } from 'react-router-dom';
import type { NavigateFunction } from '@lib/functions/navigation';
import { ScanInputHandler } from '../barcodes/BarcodeScanDialog';

export function QrModal({
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/nav/BreadcrumbList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@mantine/core';
import { IconMenu2 } from '@tabler/icons-react';
import { useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import { identifierString } from '@lib/functions/Conversion';
import { getBaseUrl, navigateToLink } from '@lib/functions/Navigation';
Expand Down
17 changes: 7 additions & 10 deletions src/frontend/src/components/nav/DetailDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ActionIcon, Divider, Drawer, Group, Stack, Text } from '@mantine/core';
import { IconChevronLeft } from '@tabler/icons-react';
import { useCallback, useMemo } from 'react';
import { Link, Route, Routes, useNavigate, useParams } from 'react-router-dom';
import type { To } from 'react-router-dom';
import { Link, useNavigate, useParams } from '@tanstack/react-router';
import type { To } from '@lib/functions/navigation';

import type { UiSizeType } from '@lib/types/Core';
import { useShallow } from 'zustand/react/shallow';
Expand Down Expand Up @@ -33,7 +33,8 @@ function DetailDrawerComponent({
renderContent
}: Readonly<DrawerProps>) {
const navigate = useNavigate();
const { id } = useParams();
const params = useParams({ strict: false }) as any;
const id = params?.id;

const content = renderContent(id);
const opened = useMemo(() => !!id && !!content, [id, content]);
Expand All @@ -46,7 +47,7 @@ function DetailDrawerComponent({
<Drawer
opened={opened}
onClose={() => {
navigate('../');
navigate({ to: '../' } as any);
addDetailDrawer(false);
}}
position={position}
Expand All @@ -60,7 +61,7 @@ function DetailDrawerComponent({
<ActionIcon
variant='outline'
onClick={() => {
navigate(-1);
window.history.go(-1);
addDetailDrawer(-1);
}}
>
Expand All @@ -80,11 +81,7 @@ function DetailDrawerComponent({
}

export function DetailDrawer(props: Readonly<DrawerProps>) {
return (
<Routes>
<Route path=':id?/' element={<DetailDrawerComponent {...props} />} />
</Routes>
);
return <DetailDrawerComponent {...props} />;
}

export function DetailDrawerLink({
Expand Down
11 changes: 8 additions & 3 deletions src/frontend/src/components/nav/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
import { IconBell, IconSearch } from '@tabler/icons-react';
import { useQuery } from '@tanstack/react-query';
import { type ReactNode, useEffect, useMemo, useState } from 'react';
import { useMatch, useNavigate } from 'react-router-dom';
import { useLocation } from '@tanstack/react-router';
import { useNavigate } from '@lib/functions/navigation';

import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
import { apiUrl } from '@lib/functions/Api';
Expand Down Expand Up @@ -207,8 +208,12 @@ export function Header() {
function NavTabs() {
const user = useUserState();
const navigate = useNavigate();
const match = useMatch(':tabName/*');
const tabValue = match?.params.tabName;
const location = useLocation();

// Extract the first path segment as tabName
const pathSegments = location.pathname.split('/').filter(Boolean);
const tabValue = pathSegments[0] || undefined;

const navTabs = getNavTabs(user);
const userSettings = useUserSettingsState();

Expand Down
13 changes: 8 additions & 5 deletions src/frontend/src/components/nav/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
} from '@mantine/spotlight';
import { IconSearch } from '@tabler/icons-react';
import { type JSX, useEffect, useMemo, useState } from 'react';
import { Navigate, Outlet, useLocation, useNavigate } from 'react-router-dom';
import { Outlet, Navigate, useLocation } from '@tanstack/react-router';
import { useNavigate } from '@lib/functions/navigation';

import { identifierString } from '@lib/functions/Conversion';
import { ApiEndpoints, apiUrl } from '@lib/index';
Expand Down Expand Up @@ -40,10 +41,12 @@ export const ProtectedRoute = ({ children }: { children: JSX.Element }) => {
<Navigate
to='/logged-in'
state={{
redirectUrl: location.pathname,
queryParams: location.search,
anchor: location.hash
}}
redirect: {
redirectUrl: location.pathname,
queryParams: location.search,
anchor: location.hash
}
} as any}
/>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/components/nav/MainMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
IconUserBolt,
IconUserCog
} from '@tabler/icons-react';
import { Link, useNavigate } from 'react-router-dom';
import { Link } from '@tanstack/react-router';
import { useNavigate } from '@lib/functions/navigation';
import { useShallow } from 'zustand/react/shallow';
import { aboutInvenTree } from '../../defaults/links';
import { doLogout } from '../../functions/auth';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/nav/NavigationTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@tabler/icons-react';
import { useQuery } from '@tanstack/react-query';
import { useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import type { ApiEndpoints } from '@lib/enums/ApiEndpoints';
import type { ModelType } from '@lib/enums/ModelType';
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/nav/NotificationDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@tabler/icons-react';
import { useQuery } from '@tanstack/react-query';
import { useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate } from '@lib/functions/navigation';

import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
import { ModelInformationDict } from '@lib/enums/ModelInformation';
Expand Down
Loading
Loading