Update hook to auto-magically load plugin panels

This commit is contained in:
Oliver Walters 2024-08-11 07:00:06 +00:00
parent e8ae6410ce
commit 4ed2773bb0
2 changed files with 35 additions and 8 deletions

View File

@ -20,8 +20,10 @@ import {
useParams useParams
} from 'react-router-dom'; } from 'react-router-dom';
import { ModelType } from '../../enums/ModelType';
import { identifierString } from '../../functions/conversion'; import { identifierString } from '../../functions/conversion';
import { navigateToLink } from '../../functions/navigation'; import { navigateToLink } from '../../functions/navigation';
import { usePluginPanels } from '../../hooks/UsePluginPanels';
import { useLocalState } from '../../states/LocalState'; import { useLocalState } from '../../states/LocalState';
import { Boundary } from '../Boundary'; import { Boundary } from '../Boundary';
import { StylishText } from '../items/StylishText'; import { StylishText } from '../items/StylishText';
@ -30,6 +32,8 @@ import { PanelType } from './Panel';
export type PanelProps = { export type PanelProps = {
pageKey: string; pageKey: string;
panels: PanelType[]; panels: PanelType[];
targetModel?: ModelType | string;
targetId?: number;
selectedPanel?: string; selectedPanel?: string;
onPanelChange?: (panel: string) => void; onPanelChange?: (panel: string) => void;
collapsible?: boolean; collapsible?: boolean;
@ -40,15 +44,28 @@ function BasePanelGroup({
panels, panels,
onPanelChange, onPanelChange,
selectedPanel, selectedPanel,
targetModel,
targetId,
collapsible = true collapsible = true
}: Readonly<PanelProps>): ReactNode { }: Readonly<PanelProps>): ReactNode {
const location = useLocation(); const location = useLocation();
const navigate = useNavigate(); const navigate = useNavigate();
const { panel } = useParams(); const { panel } = useParams();
// Hook to load plugins for this panel
const pluginPanels = usePluginPanels({
targetModel: targetModel,
targetId: targetId
});
const allPanels = useMemo(
() => [...panels, ...pluginPanels.panels],
[panels, pluginPanels.panels]
);
const activePanels = useMemo( const activePanels = useMemo(
() => panels.filter((panel) => !panel.hidden && !panel.disabled), () => allPanels.filter((panel) => !panel.hidden && !panel.disabled),
[panels] [allPanels]
); );
const setLastUsedPanel = useLocalState((state) => const setLastUsedPanel = useLocalState((state) =>
@ -112,7 +129,7 @@ function BasePanelGroup({
keepMounted={false} keepMounted={false}
> >
<Tabs.List justify="left"> <Tabs.List justify="left">
{panels.map( {allPanels.map(
(panel) => (panel) =>
!panel.hidden && ( !panel.hidden && (
<Tooltip <Tooltip
@ -155,7 +172,7 @@ function BasePanelGroup({
</ActionIcon> </ActionIcon>
)} )}
</Tabs.List> </Tabs.List>
{panels.map( {allPanels.map(
(panel) => (panel) =>
!panel.hidden && ( !panel.hidden && (
<Tabs.Panel <Tabs.Panel

View File

@ -1,16 +1,16 @@
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Alert, Text } from '@mantine/core'; import { Alert, Text } from '@mantine/core';
import { useTimeout } from '@mantine/hooks';
import { Icon24Hours } from '@tabler/icons-react';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { ReactNode, useEffect, useMemo, useState } from 'react'; import { useMemo } from 'react';
import { api } from '../App'; import { api } from '../App';
import { PanelType } from '../components/nav/Panel'; import { PanelType } from '../components/nav/Panel';
import { ApiEndpoints } from '../enums/ApiEndpoints'; import { ApiEndpoints } from '../enums/ApiEndpoints';
import { ModelType } from '../enums/ModelType';
import { identifierString } from '../functions/conversion'; import { identifierString } from '../functions/conversion';
import { InvenTreeIcon } from '../functions/icons'; import { InvenTreeIcon } from '../functions/icons';
import { apiUrl } from '../states/ApiState'; import { apiUrl } from '../states/ApiState';
import { useGlobalSettingsState } from '../states/SettingsState';
export type PluginPanelState = { export type PluginPanelState = {
panels: PanelType[]; panels: PanelType[];
@ -29,12 +29,22 @@ export function usePluginPanels({
targetModel, targetModel,
targetId targetId
}: { }: {
targetModel: string; targetModel?: ModelType | string;
targetId?: string | number | null; targetId?: string | number | null;
}): PluginPanelState { }): PluginPanelState {
const globalSettings = useGlobalSettingsState();
const pluginPanelsEnabled = useMemo(
() => globalSettings.isSet('ENABLE_PLUGINS_INTERFACE'),
[globalSettings]
);
const { isFetching, data } = useQuery({ const { isFetching, data } = useQuery({
enabled: pluginPanelsEnabled && !!targetModel,
queryKey: [targetModel, targetId], queryKey: [targetModel, targetId],
queryFn: () => { queryFn: () => {
console.log('Fetching plugin panels:', targetModel, targetId);
return api return api
.get(apiUrl(ApiEndpoints.plugin_panel_list), { .get(apiUrl(ApiEndpoints.plugin_panel_list), {
params: { params: {