mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-07 20:32:12 +00:00
111 lines
2.6 KiB
TypeScript
111 lines
2.6 KiB
TypeScript
import { t } from '@lingui/core/macro';
|
|
import { Badge } from '@mantine/core';
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { ModelType } from '@lib/enums/ModelType';
|
|
import { formatDecimal } from '@lib/functions/Formatting';
|
|
import { getDetailUrl } from '@lib/functions/Navigation';
|
|
import { ApiIcon } from '../items/ApiIcon';
|
|
import { type InstanceRenderInterface, RenderInlineModel } from './Instance';
|
|
|
|
/**
|
|
* Inline rendering of a single Part instance
|
|
*/
|
|
export function RenderPart(
|
|
props: Readonly<InstanceRenderInterface>
|
|
): ReactNode {
|
|
const { instance } = props;
|
|
|
|
let badgeText = '';
|
|
let badgeColor = '';
|
|
|
|
const stock: number | null = instance.total_in_stock ?? null;
|
|
|
|
if (instance.active == false) {
|
|
badgeColor = 'red';
|
|
badgeText = t`Inactive`;
|
|
} else if (stock != null && stock <= 0) {
|
|
badgeColor = 'orange';
|
|
badgeText = t`No stock`;
|
|
} else if (stock != null) {
|
|
badgeText = `${t`Stock`}: ${formatDecimal(stock)}`;
|
|
badgeColor = instance.minimum_stock > stock ? 'yellow' : 'green';
|
|
}
|
|
|
|
const badge = !!badgeText ? (
|
|
<Badge size='xs' color={badgeColor}>
|
|
{badgeText}
|
|
</Badge>
|
|
) : null;
|
|
|
|
return (
|
|
<RenderInlineModel
|
|
{...props}
|
|
primary={instance.full_name ?? instance.name}
|
|
secondary={instance.description}
|
|
suffix={badge}
|
|
image={instance.thumbnail || instance.image}
|
|
url={props.link ? getDetailUrl(ModelType.part, instance.pk) : undefined}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Inline rendering of a PartCategory instance
|
|
*/
|
|
export function RenderPartCategory(
|
|
props: Readonly<InstanceRenderInterface>
|
|
): ReactNode {
|
|
const { instance } = props;
|
|
|
|
return (
|
|
<RenderInlineModel
|
|
{...props}
|
|
tooltip={instance.pathstring}
|
|
prefix={
|
|
<>
|
|
{instance.level > 0 && `${'- '.repeat(instance.level)}`}
|
|
{instance.icon && <ApiIcon name={instance.icon} />}
|
|
</>
|
|
}
|
|
primary={instance.pathstring}
|
|
secondary={instance.description}
|
|
url={
|
|
props.link
|
|
? getDetailUrl(ModelType.partcategory, instance.pk)
|
|
: undefined
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Inline rendering of a PartParameterTemplate instance
|
|
*/
|
|
export function RenderPartParameterTemplate({
|
|
instance
|
|
}: Readonly<{
|
|
instance: any;
|
|
}>): ReactNode {
|
|
return (
|
|
<RenderInlineModel
|
|
primary={instance.name}
|
|
secondary={instance.description}
|
|
suffix={instance.units}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function RenderPartTestTemplate({
|
|
instance
|
|
}: Readonly<{
|
|
instance: any;
|
|
}>): ReactNode {
|
|
return (
|
|
<RenderInlineModel
|
|
primary={instance.test_name}
|
|
secondary={instance.description}
|
|
/>
|
|
);
|
|
}
|