mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-17 18:26:32 +00:00
[plugin] Instance rendering (#10006)
* Move instance render types into @lib - Expose type definitions * Expose rendering function to the plugin context
This commit is contained in:
@@ -6,6 +6,7 @@ import type { ModelDict } from '../enums/ModelInformation';
|
|||||||
import type { ModelType } from '../enums/ModelType';
|
import type { ModelType } from '../enums/ModelType';
|
||||||
import type { ApiFormModalProps, BulkEditApiFormModalProps } from './Forms';
|
import type { ApiFormModalProps, BulkEditApiFormModalProps } from './Forms';
|
||||||
import type { UseModalReturn } from './Modals';
|
import type { UseModalReturn } from './Modals';
|
||||||
|
import type { RenderInstanceProps } from './Rendering';
|
||||||
import type { SettingsStateProps } from './Settings';
|
import type { SettingsStateProps } from './Settings';
|
||||||
import type { UserStateProps } from './User';
|
import type { UserStateProps } from './User';
|
||||||
|
|
||||||
@@ -45,6 +46,7 @@ export type InvenTreeFormsContext = {
|
|||||||
* @param locale - The current locale string (e.g. 'en' / 'de')
|
* @param locale - The current locale string (e.g. 'en' / 'de')
|
||||||
* @param model - The model type associated with the rendered component (if applicable)
|
* @param model - The model type associated with the rendered component (if applicable)
|
||||||
* @param modelInformation - A dictionary of available model information
|
* @param modelInformation - A dictionary of available model information
|
||||||
|
* @param renderInstance - A component function for rendering a model instance
|
||||||
* @param id - The ID (primary key) of the model instance for the plugin (if applicable)
|
* @param id - The ID (primary key) of the model instance for the plugin (if applicable)
|
||||||
* @param instance - The model instance data (if available)
|
* @param instance - The model instance data (if available)
|
||||||
* @param reloadContent - A function which can be called to reload the plugin content
|
* @param reloadContent - A function which can be called to reload the plugin content
|
||||||
@@ -59,6 +61,7 @@ export type InvenTreePluginContext = {
|
|||||||
userSettings: SettingsStateProps;
|
userSettings: SettingsStateProps;
|
||||||
globalSettings: SettingsStateProps;
|
globalSettings: SettingsStateProps;
|
||||||
modelInformation: ModelDict;
|
modelInformation: ModelDict;
|
||||||
|
renderInstance: (props: Readonly<RenderInstanceProps>) => React.ReactNode;
|
||||||
host: string;
|
host: string;
|
||||||
locale: string;
|
locale: string;
|
||||||
navigate: NavigateFunction;
|
navigate: NavigateFunction;
|
||||||
|
24
src/frontend/lib/types/Rendering.tsx
Normal file
24
src/frontend/lib/types/Rendering.tsx
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import type { ModelType } from '../enums/ModelType';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for rendering a model instance.
|
||||||
|
*/
|
||||||
|
export interface InstanceRenderInterface {
|
||||||
|
instance: any;
|
||||||
|
link?: boolean;
|
||||||
|
navigate?: any;
|
||||||
|
showSecondary?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
type EnumDictionary<T extends string | symbol | number, U> = {
|
||||||
|
[K in T]: U;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ModelRendererDict = EnumDictionary<
|
||||||
|
ModelType,
|
||||||
|
(props: Readonly<InstanceRenderInterface>) => React.ReactNode
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type RenderInstanceProps = {
|
||||||
|
model: ModelType | undefined;
|
||||||
|
} & InstanceRenderInterface;
|
@@ -23,6 +23,7 @@ import {
|
|||||||
useDeleteApiFormModal,
|
useDeleteApiFormModal,
|
||||||
useEditApiFormModal
|
useEditApiFormModal
|
||||||
} from '../../hooks/UseForm';
|
} from '../../hooks/UseForm';
|
||||||
|
import { RenderInstance } from '../render/Instance';
|
||||||
|
|
||||||
export const useInvenTreeContext = () => {
|
export const useInvenTreeContext = () => {
|
||||||
const [locale, host] = useLocalState(useShallow((s) => [s.language, s.host]));
|
const [locale, host] = useLocalState(useShallow((s) => [s.language, s.host]));
|
||||||
@@ -50,6 +51,7 @@ export const useInvenTreeContext = () => {
|
|||||||
globalSettings: globalSettings,
|
globalSettings: globalSettings,
|
||||||
userSettings: userSettings,
|
userSettings: userSettings,
|
||||||
modelInformation: ModelInformationDict,
|
modelInformation: ModelInformationDict,
|
||||||
|
renderInstance: RenderInstance,
|
||||||
theme: theme,
|
theme: theme,
|
||||||
colorScheme: colorScheme,
|
colorScheme: colorScheme,
|
||||||
forms: {
|
forms: {
|
||||||
|
@@ -7,6 +7,11 @@ import { ModelInformationDict } from '@lib/enums/ModelInformation';
|
|||||||
import { ModelType } from '@lib/enums/ModelType';
|
import { ModelType } from '@lib/enums/ModelType';
|
||||||
import { apiUrl } from '@lib/functions/Api';
|
import { apiUrl } from '@lib/functions/Api';
|
||||||
import { navigateToLink } from '@lib/functions/Navigation';
|
import { navigateToLink } from '@lib/functions/Navigation';
|
||||||
|
import type {
|
||||||
|
ModelRendererDict,
|
||||||
|
RenderInstanceProps
|
||||||
|
} from '@lib/types/Rendering';
|
||||||
|
export type { InstanceRenderInterface } from '@lib/types/Rendering';
|
||||||
import { useApi } from '../../contexts/ApiContext';
|
import { useApi } from '../../contexts/ApiContext';
|
||||||
import { shortenString } from '../../functions/tables';
|
import { shortenString } from '../../functions/tables';
|
||||||
import { Thumbnail } from '../images/Thumbnail';
|
import { Thumbnail } from '../images/Thumbnail';
|
||||||
@@ -47,24 +52,10 @@ import {
|
|||||||
} from './Stock';
|
} from './Stock';
|
||||||
import { RenderGroup, RenderOwner, RenderUser } from './User';
|
import { RenderGroup, RenderOwner, RenderUser } from './User';
|
||||||
|
|
||||||
type EnumDictionary<T extends string | symbol | number, U> = {
|
|
||||||
[K in T]: U;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface InstanceRenderInterface {
|
|
||||||
instance: any;
|
|
||||||
link?: boolean;
|
|
||||||
navigate?: any;
|
|
||||||
showSecondary?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lookup table for rendering a model instance
|
* Lookup table for rendering a model instance
|
||||||
*/
|
*/
|
||||||
const RendererLookup: EnumDictionary<
|
export const RendererLookup: ModelRendererDict = {
|
||||||
ModelType,
|
|
||||||
(props: Readonly<InstanceRenderInterface>) => ReactNode
|
|
||||||
> = {
|
|
||||||
[ModelType.address]: RenderAddress,
|
[ModelType.address]: RenderAddress,
|
||||||
[ModelType.build]: RenderBuildOrder,
|
[ModelType.build]: RenderBuildOrder,
|
||||||
[ModelType.buildline]: RenderBuildLine,
|
[ModelType.buildline]: RenderBuildLine,
|
||||||
@@ -100,10 +91,6 @@ const RendererLookup: EnumDictionary<
|
|||||||
[ModelType.error]: RenderError
|
[ModelType.error]: RenderError
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RenderInstanceProps = {
|
|
||||||
model: ModelType | undefined;
|
|
||||||
} & InstanceRenderInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render an instance of a database model, depending on the provided data
|
* Render an instance of a database model, depending on the provided data
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user