Skip to content

Commit

Permalink
Improve plugin panels (#8523)
Browse files Browse the repository at this point in the history
* Improve plugin panels

- Do not prepend plugin name unless necessary
- Allows for "cleaner" navigation URLs

* Fix typo
  • Loading branch information
SchrodingersGat authored Nov 19, 2024
1 parent 4aa7c59 commit 3a81e03
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
28 changes: 24 additions & 4 deletions src/frontend/src/components/panels/PanelGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,30 @@ function BasePanelGroup({
id: id
});

const allPanels = useMemo(
() => [...panels, ...pluginPanels],
[panels, pluginPanels]
);
// Rebuild the list of panels
const allPanels = useMemo(() => {
const _panels = [...panels];

// Add plugin panels
pluginPanels?.forEach((panel) => {
let panelKey = panel.name;

// Check if panel with this name already exists
const existingPanel = _panels.find((p) => p.name === panelKey);

if (existingPanel) {
// Create a unique key for the panel which includes the plugin slug
panelKey = identifierString(`${panel.pluginName}-${panel.name}`);
}

_panels.push({
...panel,
name: panelKey
});
});

return _panels;
}, [panels, pluginPanels]);

const activePanels = useMemo(
() => allPanels.filter((panel) => !panel.hidden && !panel.disabled),
Expand Down
19 changes: 12 additions & 7 deletions src/frontend/src/hooks/UsePluginPanels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from '../components/plugins/PluginUIFeature';
import { ApiEndpoints } from '../enums/ApiEndpoints';
import type { ModelType } from '../enums/ModelType';
import { identifierString } from '../functions/conversion';
import { apiUrl } from '../states/ApiState';
import { useGlobalSettingsState } from '../states/SettingsState';

Expand All @@ -30,6 +29,14 @@ export type PluginPanelContext = InvenTreeContext & {
instance?: any;
};

/**
* Type definition for a plugin panel which extends the standard PanelType
* @param pluginName - The name of the plugin which provides this panel
*/
export type PluginPanelType = PanelType & {
pluginName: string;
};

export function usePluginPanels({
instance,
model,
Expand All @@ -38,7 +45,7 @@ export function usePluginPanels({
instance?: any;
model?: ModelType | string;
id?: string | number | null;
}): PanelType[] {
}): PluginPanelType[] {
const globalSettings = useGlobalSettingsState();

const pluginPanelsEnabled: boolean = useMemo(
Expand Down Expand Up @@ -86,21 +93,19 @@ export function usePluginPanels({
};
}, [model, id, instance, inventreeContext]);

const pluginPanels: PanelType[] = useMemo(() => {
const pluginPanels: PluginPanelType[] = useMemo(() => {
return (
pluginData?.map((props: PluginUIFeature) => {
const iconName: string = props?.icon || 'ti:plug:outline';
const identifier = identifierString(
`${props.plugin_name}-${props.key}`
);

const pluginContext: any = {
...contextData,
context: props.context
};

return {
name: identifier,
name: props.key,
pluginName: props.plugin_name,
label: props.title,
icon: <ApiIcon name={iconName} />,
content: (
Expand Down

0 comments on commit 3a81e03

Please sign in to comment.