[plugin] Render tables (#11733)

* Move useFilterSet state to the @lib

* Refactor useTable hook into @lib

* Refactor string helper functions

* Refactor constructFormUrl func

* Refactor Boundary component

* Refactor StoredTableState

* More refactoring

* Refactor CopyButton and CopyableCell

* Pass table render func to plugins

* Provide internal wrapper function, while allowing the "api" and "navigate" functions to be provided by the caller

* Adds <InvenTreeTable /> component which is exposed to plugins

* Update frontend versioning

* Update docs

* Handle condition where UI does not provide table rendering function

* Move queryFilters out of custom state

* Fix exported type

* Extract searchParams

- Cannot be used outside of router component
- Only provide when the table is generated internally

* Bump UI version

* Fix for right-click context menu

- Function needs to be defined with the context menu provider
This commit is contained in:
Oliver authored and GitHub committed 2026-04-13 20:36:29 +10:00
1 parent 27ce60dea3
commit 23f43ffd33
120 files changed
+506 -304

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import type { ApiEndpoints } from '../enums/ApiEndpoints';
import type { PathParams } from '../types/Core';
import type { ApiFormFieldSet, ApiFormFieldType } from '../types/Forms';
import { apiUrl } from './Api';
/**
* Construct an API url from the provided ApiFormProps object
*/
export function constructFormUrl(
url: ApiEndpoints | string,
pk?: string | number,
pathParams?: PathParams,
queryParams?: URLSearchParams
): string {
let formUrl = apiUrl(url, pk, pathParams);
if (queryParams) {
formUrl += `?${queryParams.toString()}`;
}
return formUrl;
}
export type NestedDict = { [key: string]: string | number | NestedDict };
export function mapFields(
fields: ApiFormFieldSet,
fieldFunction: (path: string, value: ApiFormFieldType, key: string) => any,
_path?: string
): NestedDict {
const res: NestedDict = {};
for (const [k, v] of Object.entries(fields)) {
const path = _path ? `${_path}.${k}` : k;
let value: any;
if (v.field_type === 'nested object' && v.children) {
value = mapFields(v.children, fieldFunction, path);
} else {
value = fieldFunction(path, v, k);
}
if (value !== undefined) res[k] = value;
}
return res;
}
@@ -0,0 +1,50 @@
import { t } from '@lingui/core/macro';
import { notifications } from '@mantine/notifications';
/**
* Show a notification that the feature is not yet implemented
*/
export function notYetImplemented() {
notifications.hide('not-implemented');
notifications.show({
title: t`Not implemented`,
message: t`This feature is not yet implemented`,
color: 'red',
id: 'not-implemented'
});
}
/**
* Show a notification that the user does not have permission to perform the action
*/
export function permissionDenied() {
notifications.show({
title: t`Permission Denied`,
message: t`You do not have permission to perform this action`,
color: 'red'
});
}
/**
* Display a notification on an invalid return code
*/
export function invalidResponse(returnCode: number) {
// TODO: Specific return code messages
notifications.show({
title: t`Invalid Return Code`,
message: t`Server returned status ${returnCode}`,
color: 'red'
});
}
/**
* Display a notification on timeout
*/
export function showTimeoutNotification() {
notifications.show({
title: t`Timeout`,
message: t`The request timed out`,
color: 'red'
});
}
+38
View File
@@ -0,0 +1,38 @@
/**
* Reduce an input string to a given length, adding an ellipsis if necessary
* @param str - String to shorten
* @param len - Length to shorten to
*/
export function shortenString({
str,
len = 100
}: {
str: string | undefined;
len?: number;
}) {
// Ensure that the string is a string
str = str ?? '';
str = str.toString();
// If the string is already short enough, return it
if (str.length <= len) {
return str;
}
// Otherwise, shorten it
const N = Math.floor(len / 2 - 1);
return `${str.slice(0, N)} ... ${str.slice(-N)}`;
}
/**
* Generate a short hash from a long string
*/
export function hashString(str: string): string {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) - hash + str.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return hash.toString(36);
}