mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-18 14:07:41 +00:00
feat: add plugin support for custom page integration (#12900)
* Added plugin UI route functionality. * Updated Sample UI plugin with route example. * Added UI route tests. * Plugin name added to UI Route URL. * Updated docs. * Docs fix. * Updated sample_route text. * Update src/backend/InvenTree/plugin/base/ui/mixins.py Co-authored-by: Matthias Mair <code@mjmair.com> * Fixed nav dynamic link highlighting. * Added changelog entry. --------- Co-authored-by: Matthias Mair <code@mjmair.com> Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
11 files changed
+199
-5
No files matched your search
@@ -14,7 +14,12 @@ import { useDisclosure, useDocumentVisibility } from '@mantine/hooks';
|
||||
import { IconBell, IconSearch, IconUserBolt } 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 {
|
||||
matchPath,
|
||||
useLocation,
|
||||
useMatch,
|
||||
useNavigate
|
||||
} from 'react-router-dom';
|
||||
|
||||
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
|
||||
import { apiUrl } from '@lib/functions/Api';
|
||||
@@ -252,6 +257,8 @@ function NavTabs() {
|
||||
const tabValue = match?.params.tabName;
|
||||
const navTabs = getNavTabs(user);
|
||||
const userSettings = useUserSettingsState();
|
||||
// Get the current URL
|
||||
const location = useLocation();
|
||||
|
||||
const withIcons: boolean = useMemo(
|
||||
() => userSettings.isSet('ICONS_IN_NAVBAR', false),
|
||||
@@ -263,6 +270,21 @@ function NavTabs() {
|
||||
context: {}
|
||||
});
|
||||
|
||||
// Find dynamic navigation URLs that match the current location, with a preference towards more specific URLs.
|
||||
const dynamicTabValue = extraNavs
|
||||
.filter((nav) =>
|
||||
matchPath(
|
||||
{
|
||||
path: `/${nav.options.options.url}`,
|
||||
end: false
|
||||
},
|
||||
location.pathname
|
||||
)
|
||||
)
|
||||
.sort(
|
||||
(a, b) => b.options.options.url.length - a.options.options.url.length
|
||||
)[0]?.options.key;
|
||||
|
||||
const tabs: ReactNode[] = useMemo(() => {
|
||||
const _tabs: ReactNode[] = [];
|
||||
|
||||
@@ -301,7 +323,7 @@ function NavTabs() {
|
||||
extraNavs.forEach((nav) => {
|
||||
_tabs.push(
|
||||
<Tabs.Tab
|
||||
value={nav.options.title}
|
||||
value={nav.options.key}
|
||||
key={nav.options.key}
|
||||
onClick={(event: any) =>
|
||||
navigateToLink(nav.options.options.url, navigate, event)
|
||||
@@ -323,7 +345,8 @@ function NavTabs() {
|
||||
list: classes.tabsList,
|
||||
tab: classes.tab
|
||||
}}
|
||||
value={tabValue}
|
||||
// Select either a static or dynamic tab to be highlighted.
|
||||
value={dynamicTabValue ?? tabValue}
|
||||
>
|
||||
<Tabs.List>{tabs.map((tab) => tab)}</Tabs.List>
|
||||
</Tabs>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Route, Routes } from 'react-router-dom';
|
||||
|
||||
import { usePluginUIFeature } from '../../hooks/UsePluginUIFeature';
|
||||
import type { RouteUIFeature } from './PluginUIFeatureTypes';
|
||||
import RemoteComponent from './RemoteComponent';
|
||||
|
||||
import { useInvenTreeContext } from './PluginContext';
|
||||
|
||||
import NotFound from '../errors/NotFound';
|
||||
|
||||
export function PluginRoutes() {
|
||||
const routes = usePluginUIFeature<RouteUIFeature>({
|
||||
featureType: 'route',
|
||||
context: {}
|
||||
});
|
||||
|
||||
const pluginContext = useInvenTreeContext();
|
||||
|
||||
return (
|
||||
<Routes>
|
||||
{routes.map((route) => (
|
||||
<Route
|
||||
key={route.options.key}
|
||||
path={`${route.options.plugin_name}/${route.options.options.path}`}
|
||||
element={
|
||||
<RemoteComponent
|
||||
source={route.options.source}
|
||||
defaultFunctionName='getFeature'
|
||||
context={pluginContext}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
))}
|
||||
|
||||
<Route path='*' element={<NotFound />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
@@ -31,7 +31,8 @@ export enum PluginUIFeatureType {
|
||||
template_editor = 'template_editor',
|
||||
template_preview = 'template_preview',
|
||||
navigation = 'navigation',
|
||||
primary_action = 'primary_action'
|
||||
primary_action = 'primary_action',
|
||||
route = 'route'
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -92,3 +92,13 @@ export type PrimaryActionUIFeature = {
|
||||
featureContext: {};
|
||||
featureReturnType: undefined;
|
||||
};
|
||||
|
||||
export type RouteUIFeature = BaseUIFeature & {
|
||||
featureType: 'route';
|
||||
requestContext: {};
|
||||
responseOptions: {
|
||||
path: string;
|
||||
};
|
||||
featureContext: {};
|
||||
featureReturnType: any;
|
||||
};
|
||||
@@ -4,6 +4,8 @@ import { Navigate, Route, Routes } from 'react-router-dom';
|
||||
import { EagerLoadable, Loadable } from './functions/loading';
|
||||
import { onLocaleReady } from './functions/localeReady';
|
||||
|
||||
import { PluginRoutes } from './components/plugins/PluginRoutes';
|
||||
|
||||
// Lazy loaded pages
|
||||
// These two are mutually exclusive and one of them is always needed
|
||||
// immediately on initial load, so they're loaded eagerly rather than via
|
||||
@@ -220,6 +222,7 @@ export const routes = (
|
||||
<Route path='user/:id/*' element={<UserDetail />} />
|
||||
<Route path='group/:id/*' element={<GroupDetail />} />
|
||||
</Route>
|
||||
<Route path='plugin/*' element={<PluginRoutes />} />
|
||||
</Route>
|
||||
<Route
|
||||
path='/'
|
||||
|
||||
Reference in new issue
Block a user