Skip to content

Commit c292e80

Browse files
Enhance MonitorDataOutput (#11458)
* Enhance MonitorDataOutput - Optionally pass QueryClient - Required when running outside of <ApiContext /> * Fix default base URL if not specified * Remove "allow_null" from Mantine props * Bump lib version to 0.8.2 * Bump CHANGELOG.md
1 parent f7da51b commit c292e80

File tree

5 files changed

+82
-70
lines changed

5 files changed

+82
-70
lines changed

src/frontend/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This file contains historical changelog information for the InvenTree UI components library.
44

5+
### 0.8.2 - March 2026
6+
7+
Bug fixes for the `monitorDataOutput` hook - https://github.com/inventree/InvenTree/pull/11458
8+
59
### 0.8.0 - March 2026
610

711
Exposes the `monitorDataOutput` hook, which allows plugins to monitor the output of a long-running task and display notifications when the task is complete. This is useful for plugins that need to perform long-running tasks and want to provide feedback to the user when the task is complete.

src/frontend/lib/hooks/MonitorDataOutput.tsx

Lines changed: 75 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { t } from '@lingui/core/macro';
22
import { useDocumentVisibility } from '@mantine/hooks';
33
import { notifications, showNotification } from '@mantine/notifications';
44
import { IconCircleCheck, IconExclamationCircle } from '@tabler/icons-react';
5-
import { useQuery } from '@tanstack/react-query';
5+
import { type QueryClient, useQuery } from '@tanstack/react-query';
66
import type { AxiosInstance } from 'axios';
77
import { useEffect, useState } from 'react';
88
import { ProgressBar } from '../components/ProgressBar';
@@ -14,11 +14,13 @@ import { apiUrl } from '../functions/Api';
1414
*/
1515
export default function monitorDataOutput({
1616
api,
17+
queryClient,
1718
title,
1819
hostname,
1920
id
2021
}: {
2122
api: AxiosInstance;
23+
queryClient?: QueryClient;
2224
title: string;
2325
hostname?: string;
2426
id?: number;
@@ -41,82 +43,86 @@ export default function monitorDataOutput({
4143
} else setLoading(false);
4244
}, [id, title]);
4345

44-
useQuery({
45-
enabled: !!id && loading && visibility === 'visible',
46-
refetchInterval: 500,
47-
queryKey: ['data-output', id, title],
48-
queryFn: () =>
49-
api
50-
.get(apiUrl(ApiEndpoints.data_output, id))
51-
.then((response) => {
52-
const data = response?.data ?? {};
46+
useQuery(
47+
{
48+
enabled: !!id && loading && visibility === 'visible',
49+
refetchInterval: 500,
50+
queryKey: ['data-output', id, title],
51+
queryFn: () =>
52+
api
53+
.get(apiUrl(ApiEndpoints.data_output, id))
54+
.then((response) => {
55+
const data = response?.data ?? {};
5356

54-
if (!!data.errors || !!data.error) {
55-
setLoading(false);
57+
if (!!data.errors || !!data.error) {
58+
setLoading(false);
5659

57-
const error: string =
58-
data?.error ?? data?.errors?.error ?? t`Process failed`;
60+
const error: string =
61+
data?.error ?? data?.errors?.error ?? t`Process failed`;
5962

60-
notifications.update({
61-
id: `data-output-${id}`,
62-
loading: false,
63-
icon: <IconExclamationCircle />,
64-
autoClose: 2500,
65-
title: title,
66-
message: error,
67-
color: 'red'
68-
});
69-
} else if (data.complete) {
70-
setLoading(false);
71-
notifications.update({
72-
id: `data-output-${id}`,
73-
loading: false,
74-
autoClose: 2500,
75-
title: title,
76-
message: t`Process completed successfully`,
77-
color: 'green',
78-
icon: <IconCircleCheck />
79-
});
63+
notifications.update({
64+
id: `data-output-${id}`,
65+
loading: false,
66+
icon: <IconExclamationCircle />,
67+
autoClose: 2500,
68+
title: title,
69+
message: error,
70+
color: 'red'
71+
});
72+
} else if (data.complete) {
73+
setLoading(false);
74+
notifications.update({
75+
id: `data-output-${id}`,
76+
loading: false,
77+
autoClose: 2500,
78+
title: title,
79+
message: t`Process completed successfully`,
80+
color: 'green',
81+
icon: <IconCircleCheck />
82+
});
8083

81-
if (data.output) {
82-
const url = data.output;
83-
const base = hostname ?? window.location.hostname;
84+
if (data.output) {
85+
const url = data.output;
86+
const base = hostname ?? window.location.origin;
8487

85-
const downloadUrl = new URL(url, base);
88+
const downloadUrl = new URL(url, base);
8689

87-
window.open(downloadUrl.toString(), '_blank');
90+
window.open(downloadUrl.toString(), '_blank');
91+
}
92+
} else {
93+
notifications.update({
94+
id: `data-output-${id}`,
95+
loading: true,
96+
autoClose: false,
97+
withCloseButton: false,
98+
message: (
99+
<ProgressBar
100+
size='lg'
101+
maximum={data.total}
102+
value={data.progress}
103+
progressLabel={data.total > 0}
104+
animated
105+
/>
106+
)
107+
});
88108
}
89-
} else {
109+
110+
return data;
111+
})
112+
.catch((error: Error) => {
113+
console.error('Error in monitorDataOutput:', error);
114+
setLoading(false);
90115
notifications.update({
91116
id: `data-output-${id}`,
92-
loading: true,
93-
autoClose: false,
94-
withCloseButton: false,
95-
message: (
96-
<ProgressBar
97-
size='lg'
98-
maximum={data.total}
99-
value={data.progress}
100-
progressLabel={data.total > 0}
101-
animated
102-
/>
103-
)
117+
loading: false,
118+
autoClose: 2500,
119+
title: title,
120+
message: error.message || t`Process failed`,
121+
color: 'red'
104122
});
105-
}
106-
107-
return data;
108-
})
109-
.catch(() => {
110-
setLoading(false);
111-
notifications.update({
112-
id: `data-output-${id}`,
113-
loading: false,
114-
autoClose: 2500,
115-
title: title,
116-
message: t`Process failed`,
117-
color: 'red'
118-
});
119-
return {};
120-
})
121-
});
123+
return {};
124+
})
125+
},
126+
queryClient
127+
);
122128
}

src/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@inventreedb/ui",
33
"description": "UI components for the InvenTree project",
4-
"version": "0.8.0",
4+
"version": "0.8.2",
55
"private": false,
66
"type": "module",
77
"license": "MIT",

src/frontend/src/components/forms/fields/ApiFormField.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export function ApiFormField({
8585
onValueChange: undefined,
8686
adjustFilters: undefined,
8787
adjustValue: undefined,
88+
allow_null: undefined,
8889
read_only: undefined,
8990
children: undefined,
9091
exclude: undefined

src/frontend/src/components/forms/fields/RelatedModelField.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ export function RelatedModelField({
380380
onValueChange: undefined,
381381
adjustFilters: undefined,
382382
exclude: undefined,
383+
allow_null: undefined,
383384
read_only: undefined
384385
};
385386
}, [definition]);

0 commit comments

Comments
 (0)