Cleanup code for passing data through to plugin panels

- Define interface type
- Shorten variable names
This commit is contained in:
Oliver Walters 2024-08-13 09:40:02 +00:00
parent 90c497e38a
commit 045f8fe069
15 changed files with 102 additions and 87 deletions

View File

@ -34,8 +34,9 @@ import { PanelType } from './Panel';
*
* @param pageKey - Unique key for this panel group
* @param panels - List of panels to display
* @param targetModel - The target model for this panel group
* @param targetId - The target ID for this panel group (set to *null* for groups which do not target a specific model instance)
* @param model - The target model for this panel group (e.g. 'part' / 'salesorder')
* @param id - The target ID for this panel group (set to *null* for groups which do not target a specific model instance)
* @param instance - The target model instance for this panel group
* @param selectedPanel - The currently selected panel
* @param onPanelChange - Callback when the active panel changes
* @param collapsible - If true, the panel group can be collapsed (defaults to true)
@ -43,9 +44,9 @@ import { PanelType } from './Panel';
export type PanelProps = {
pageKey: string;
panels: PanelType[];
targetInstance?: any;
targetModel?: ModelType | string;
targetId?: number | null;
instance?: any;
model?: ModelType | string;
id?: number | null;
selectedPanel?: string;
onPanelChange?: (panel: string) => void;
collapsible?: boolean;
@ -56,9 +57,9 @@ function BasePanelGroup({
panels,
onPanelChange,
selectedPanel,
targetInstance,
targetModel,
targetId,
instance,
model,
id,
collapsible = true
}: Readonly<PanelProps>): ReactNode {
const location = useLocation();
@ -67,9 +68,9 @@ function BasePanelGroup({
// Hook to load plugins for this panel
const pluginPanels = usePluginPanels({
targetModel: targetModel,
targetInstance: targetInstance,
targetId: targetId
model: model,
instance: instance,
id: id
});
const allPanels = useMemo(

View File

@ -0,0 +1,26 @@
import { AxiosInstance } from 'axios';
import { ModelType } from '../../enums/ModelType';
/*
* A set of properties which are passed to a plugin,
* for rendering an element in the user interface.
*
* @param target - The target HTML element to render the plugin into
* @param model - The model type for the plugin (e.g. 'part' / 'purchaseorder')
* @param id - The ID (primary key) of the model instance for the plugin
* @param instance - The model instance data (if available)
* @param api - The Axios API instance (see ../states/ApiState.tsx)
* @param user - The current user instance (see ../states/UserState.tsx)
* @param navigate - The navigation function (see react-router-dom)
*/
export interface PluginElementProps {
target: HTMLDivElement | undefined;
model?: ModelType | string;
id?: number | null;
instance?: any;
api: AxiosInstance;
user: any;
host: string;
navigate: any;
}

View File

@ -9,28 +9,14 @@ import { ModelType } from '../../enums/ModelType';
import { useLocalState } from '../../states/LocalState';
import { useUserState } from '../../states/UserState';
import { PanelType } from '../nav/Panel';
import { PluginElementProps } from './PluginElement';
interface PluginPanelProps extends PanelType {
source?: string;
params?: any;
targetInstance?: any;
targetModel?: ModelType | string;
targetId?: string | number | null;
}
/*
* Definition of what we pass into a plugin panel
*/
interface PluginPanelParameters {
target: HTMLDivElement;
props: PluginPanelProps;
targetModel?: ModelType | string;
targetId?: number | null;
targetInstance?: any;
api: AxiosInstance;
user: any;
host: string;
navigate: any;
instance?: any;
model?: ModelType | string;
id?: number | null;
}
// Placeholder content for a panel with no content
@ -93,17 +79,19 @@ export default function PluginPanel({ props }: { props: PluginPanelProps }) {
module.render_panel &&
typeof module.render_panel === 'function'
) {
module.render_panel({
// Set of attributes to pass through to the plugin for rendering
let attributes: PluginElementProps = {
target: ref.current,
props: props,
model: props.model,
id: props.id,
instance: props.instance,
api: api,
host: host,
user: user,
navigate: navigate,
targetModel: props.targetModel,
targetId: props.targetId,
targetInstance: props.targetInstance
});
host: host,
navigate: navigate
};
module.render_panel(attributes);
}
};

View File

@ -18,13 +18,13 @@ export type PluginPanelState = {
};
export function usePluginPanels({
targetInstance,
targetModel,
targetId
instance,
model,
id
}: {
targetInstance?: any;
targetModel?: ModelType | string;
targetId?: string | number | null;
instance?: any;
model?: ModelType | string;
id?: string | number | null;
}): PluginPanelState {
const globalSettings = useGlobalSettingsState();
@ -33,20 +33,20 @@ export function usePluginPanels({
[globalSettings]
);
// API query to fetch information on available plugin panels
// API query to fetch initial information on available plugin panels
const { isFetching, data } = useQuery({
enabled: pluginPanelsEnabled && !!targetModel && targetId != undefined,
queryKey: [targetModel, targetId],
enabled: pluginPanelsEnabled && !!model && id != undefined,
queryKey: [model, id],
queryFn: async () => {
if (!pluginPanelsEnabled || !targetModel) {
if (!pluginPanelsEnabled || !model) {
return Promise.resolve([]);
}
return api
.get(apiUrl(ApiEndpoints.plugin_panel_list), {
params: {
target_model: targetModel,
target_id: targetId
target_model: model,
target_id: id
}
})
.then((response: any) => response.data)
@ -62,23 +62,23 @@ export function usePluginPanels({
data?.map((panel: any) => {
const pluginKey = panel.plugin || 'plugin';
return {
name: identifierString(`${pluginKey}-${panel.name}`),
name: identifierString(`pluigin-${pluginKey}-${panel.name}`),
label: panel.label || t`Plugin Panel`,
icon: <InvenTreeIcon icon={panel.icon ?? 'plugin'} />,
content: (
<PluginPanel
props={{
...panel,
targetId: targetId,
targetModel: targetModel,
targetInstance: targetInstance
id: id,
model: model,
instance: instance
}}
/>
)
};
}) ?? []
);
}, [data, targetId, targetModel, targetInstance]);
}, [data, id, model, instance]);
return {
panels: panels

View File

@ -542,9 +542,9 @@ export default function BuildDetail() {
<PanelGroup
pageKey="build"
panels={buildPanels}
targetInstance={build}
targetModel={ModelType.build}
targetId={build.pk}
instance={build}
model={ModelType.build}
id={build.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -334,9 +334,9 @@ export default function CompanyDetail(props: Readonly<CompanyDetailProps>) {
<PanelGroup
pageKey="company"
panels={companyPanels}
targetInstance={company}
targetModel={ModelType.company}
targetId={company.pk}
instance={company}
model={ModelType.company}
id={company.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -287,9 +287,9 @@ export default function ManufacturerPartDetail() {
<PanelGroup
pageKey="manufacturerpart"
panels={panels}
targetInstance={manufacturerPart}
targetModel={ModelType.manufacturerpart}
targetId={manufacturerPart.pk}
instance={manufacturerPart}
model={ModelType.manufacturerpart}
id={manufacturerPart.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -379,9 +379,9 @@ export default function SupplierPartDetail() {
<PanelGroup
pageKey="supplierpart"
panels={panels}
targetInstance={supplierPart}
targetModel={ModelType.supplierpart}
targetId={supplierPart.pk}
instance={supplierPart}
model={ModelType.supplierpart}
id={supplierPart.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -315,9 +315,9 @@ export default function CategoryDetail() {
<PanelGroup
pageKey="partcategory"
panels={panels}
targetModel={ModelType.partcategory}
targetInstance={category}
targetId={category.pk}
model={ModelType.partcategory}
instance={category}
id={category.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -1098,9 +1098,9 @@ export default function PartDetail() {
<PanelGroup
pageKey="part"
panels={partPanels}
targetInstance={part}
targetModel={ModelType.part}
targetId={part.pk}
instance={part}
model={ModelType.part}
id={part.pk}
/>
{transferStockItems.modal}
{countStockItems.modal}

View File

@ -446,9 +446,9 @@ export default function PurchaseOrderDetail() {
<PanelGroup
pageKey="purchaseorder"
panels={orderPanels}
targetModel={ModelType.purchaseorder}
targetInstance={order}
targetId={order.pk}
model={ModelType.purchaseorder}
instance={order}
id={order.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -434,9 +434,9 @@ export default function ReturnOrderDetail() {
<PanelGroup
pageKey="returnorder"
panels={orderPanels}
targetModel={ModelType.returnorder}
targetInstance={order}
targetId={order.pk}
model={ModelType.returnorder}
instance={order}
id={order.pk}
/>
</Stack>
</InstanceDetail>

View File

@ -486,9 +486,9 @@ export default function SalesOrderDetail() {
<PanelGroup
pageKey="salesorder"
panels={orderPanels}
targetModel={ModelType.salesorder}
targetId={order.pk}
targetInstance={order}
model={ModelType.salesorder}
id={order.pk}
instance={order}
/>
</Stack>
</InstanceDetail>

View File

@ -396,9 +396,9 @@ export default function Stock() {
<PanelGroup
pageKey="stocklocation"
panels={locationPanels}
targetModel={ModelType.stocklocation}
targetId={location.pk}
targetInstance={location}
model={ModelType.stocklocation}
id={location.pk}
instance={location}
/>
{transferStockItems.modal}
{countStockItems.modal}

View File

@ -631,9 +631,9 @@ export default function StockDetail() {
<PanelGroup
pageKey="stockitem"
panels={stockPanels}
targetModel={ModelType.stockitem}
targetId={stockitem.pk}
targetInstance={stockitem}
model={ModelType.stockitem}
id={stockitem.pk}
instance={stockitem}
/>
{editStockItem.modal}
{duplicateStockItem.modal}