[Plugin] Plugin context (#9439)

* Pass more stuff to window

* Expose form functions to plugin context

* Breaking: Render plugin component in context tree

- Required due to createRoot function
- Adds necessary context providers

* Fix context

* Provide MantineThemeContext

* Bundle mantine/core

* Hack for useNavigate within ApiForm

- Errors out if called within plugin context
- Workaround to catch the error

* Update build cmd

* Define config for building "Library" mode

* Update package.json

* Add basic index file

* Factor out ApiEndpoints

* factor out ModelType

* Factor out role enums

* Further refactoring

* More refactoring

* Cleanup

* Expose apiUrl function

* Add instance data to plugin context type def

* Tweaks for loading plugin components

- LanguageContext must be on the inside

* Tweak StylishText

* Externalize notifications system

* Update lingui config

* Add functions for checking plugin interface version

* Extract package version at build time

* Enhance version checking

* Revert variable name change

* Public package

* Add README.md

* adjust packge name

* Adjust name to include org

* Update project files

* Add basic changelog info

* Refactoring to expose URL functions

* Refactor navigation functions

* Update package and README

* Improve navigateToLink function

* Refactor stylish text

- Move into ./lib
- Do not require user state

* Revert changes

- StylishText throws error in plugin
- Low priority, can work out later

* expose function to refresh page index

* Provide RemoteComponent with a method to reload itself

* Bump version

* Cleanup tests

* Prevent duplicate --emptyOutDir arg

* Tweak playwright tests

* Expose role and permission enums

* Fix imports

* Updated docs

* Fix spelling, typos, etc

* Include more package version information

* Expose more version context

* Cleanup

* Probably don't need hooks

* Fix links

* Docs updates

* Fix links
This commit is contained in:
Oliver authored and GitHub committed 2025-04-16 00:30:34 +10:00
1 parent f3d804d5ea
commit 5e7e258289
276 files changed
+2797 -1854

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import type { ApiEndpoints } from '../enums/ApiEndpoints';
import type { PathParams } from '../types/Core';
/**
* Function to return the API prefix.
* For now it is fixed, but may be configurable in the future.
*/
export function apiPrefix(): string {
return '/api/';
}
/**
* Construct an API URL with an endpoint and (optional) pk value
*/
export function apiUrl(
endpoint: ApiEndpoints | string,
pk?: any,
pathParams?: PathParams
): string {
let _url = endpoint;
// If the URL does not start with a '/', add the API prefix
if (!_url.startsWith('/')) {
_url = apiPrefix() + _url;
}
if (_url && pk) {
if (_url.indexOf(':id') >= 0) {
_url = _url.replace(':id', `${pk}`);
} else {
_url += `${pk}/`;
}
}
if (_url && pathParams) {
for (const key in pathParams) {
_url = _url.replace(`:${key}`, `${pathParams[key]}`);
}
}
return _url;
}
+6
View File
@@ -0,0 +1,6 @@
// Helper function to cancel event propagation
export function cancelEvent(event: any) {
event?.preventDefault();
event?.stopPropagation();
event?.nativeEvent?.stopImmediatePropagation();
}
+70
View File
@@ -0,0 +1,70 @@
import type { NavigateFunction } from 'react-router-dom';
import { ModelInformationDict } from '../enums/ModelInformation';
import type { ModelType } from '../enums/ModelType';
import { cancelEvent } from './Events';
export const getBaseUrl = (): string =>
(window as any).INVENTREE_SETTINGS?.base_url || 'web';
/**
* Returns the detail view URL for a given model type
*/
export function getDetailUrl(
model: ModelType,
pk: number | string,
absolute?: boolean
): string {
const modelInfo = ModelInformationDict[model];
if (pk === undefined || pk === null) {
return '';
}
if (!!pk && modelInfo && modelInfo.url_detail) {
const url = modelInfo.url_detail.replace(':pk', pk.toString());
const base = getBaseUrl();
if (absolute && base) {
return `/${base}${url}`;
} else {
return url;
}
}
console.error(`No detail URL found for model ${model} <${pk}>`);
return '';
}
/*
* Navigate to a provided link.
* - If the link is to be opened externally, open it in a new tab.
* - Otherwise, navigate using the provided navigate function.
*/
export const navigateToLink = (
link: string,
navigate: NavigateFunction,
event: any
) => {
cancelEvent(event);
const base = `/${getBaseUrl()}`;
if (event?.ctrlKey || event?.shiftKey) {
// Open the link in a new tab
let url = link;
if (link.startsWith('/') && !link.startsWith(base)) {
url = `${base}${link}`;
}
window.open(url, '_blank');
} else {
// Navigate internally
let url = link;
if (link.startsWith(base)) {
// Strip the base URL from the link
url = link.replace(base, '');
}
navigate(url);
}
};
+30
View File
@@ -0,0 +1,30 @@
import {
INVENTREE_PLUGIN_VERSION,
type InvenTreePluginContext
} from '../types/Plugins';
function extractVersion(version: string) {
// Extract the version number from the string
const [major, minor, patch] = version
.split('.')
.map((v) => Number.parseInt(v, 10));
return { major, minor, patch };
}
/**
* Check th e
*/
export function checkPluginVersion(context: InvenTreePluginContext) {
const pluginVersion = extractVersion(INVENTREE_PLUGIN_VERSION);
const systemVersion = extractVersion(context.version.inventree);
const mismatch = `Plugin version mismatch! Expected version ${INVENTREE_PLUGIN_VERSION}, got ${context.version}`;
// A major version mismatch indicates a potentially breaking change
if (pluginVersion.major !== systemVersion.major) {
console.warn(mismatch);
} else if (INVENTREE_PLUGIN_VERSION != context.version.inventree) {
console.info(mismatch);
}
}