Skip to content

Commit a625a00

Browse files
aufisshveta
authored andcommitted
🐛 Fix multiple custom rules upload (konveyor#2636)
Resolves: https://issues.redhat.com/browse/MTA-6029 Resolves: konveyor#2654 When uploading multiple custom rules in analyzer wizard, only last one was corectly processed for it s rules, updating code to make sure each uploaded file processed. Used claude code to assist with this change. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Rule files now begin processing immediately after selection with initial progress/status visible. * Each file carries its own per-file context through the upload lifecycle for clearer handling and callbacks. * **Bug Fixes** * Upload progress and status updates are more consistent and visible. * Success and error callbacks reliably map to the correct file, preventing stale state and improving error attribution. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Marek Aufart <[email protected]>
1 parent 8de85fd commit a625a00

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

client/src/app/components/CustomRuleFilesUpload.tsx

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,16 @@ export const CustomRuleFilesUpload: React.FC<CustomRuleFilesUploadProps> = ({
129129
);
130130
}
131131
}
132-
onChangeRuleFile({
132+
// Upload the file to hub!
133+
// TODO: Provide an onUploadProgress handler so the actual upload can be tracked from 20% to 100%
134+
const updatedRuleFile: UploadFile = {
133135
...ruleFile,
134136
uploadProgress: 20,
135137
status: "validated",
136138
contents: fileContents,
137-
});
138-
139-
// Upload the file to hub!
140-
// TODO: Provide an onUploadProgress handler so the actual upload can be tracked from 20% to 100%
141-
uploadFile(file, taskgroupId);
139+
};
140+
onChangeRuleFile(updatedRuleFile);
141+
uploadFile(file, taskgroupId, updatedRuleFile);
142142
} catch (error) {
143143
onChangeRuleFile({
144144
...ruleFile,
@@ -149,24 +149,22 @@ export const CustomRuleFilesUpload: React.FC<CustomRuleFilesUploadProps> = ({
149149
};
150150

151151
const { uploadFile } = useFileUploader(
152-
(file, hubFile) => {
153-
const ruleFile = ruleFileByName(file.name);
154-
if (ruleFile) {
152+
(file, hubFile, ruleFileContext) => {
153+
if (ruleFileContext) {
155154
onChangeRuleFile({
156-
...ruleFile,
155+
...ruleFileContext,
157156
fileId: hubFile?.id,
158157
uploadProgress: 100,
159158
status: "uploaded",
160159
});
161160
}
162161
},
163-
(error, file) => {
162+
(error, file, ruleFileContext) => {
164163
const msg = getAxiosErrorMessage(error);
165164

166-
const ruleFile = ruleFileByName(file.name);
167-
if (ruleFile) {
165+
if (ruleFileContext) {
168166
onChangeRuleFile({
169-
...ruleFile,
167+
...ruleFileContext,
170168
loadError: msg,
171169
status: "failed",
172170
});
@@ -250,20 +248,52 @@ export const CustomRuleFilesUpload: React.FC<CustomRuleFilesUploadProps> = ({
250248
};
251249

252250
function useFileUploader(
253-
onSuccess: (file: File, hubFile?: HubFile) => void,
254-
onError: (e: AxiosError, file: File) => void
251+
onSuccess: (
252+
file: File,
253+
hubFile?: HubFile,
254+
ruleFileContext?: UploadFile
255+
) => void,
256+
onError: (e: AxiosError, file: File, ruleFileContext?: UploadFile) => void
255257
) {
258+
// Store file context to pass to callbacks
259+
const fileContextRef = React.useRef(new Map<string, UploadFile>());
260+
256261
const { mutate: createRuleFile } = useCreateFileMutation(
257-
(hubFile, file) => onSuccess(file, hubFile),
258-
(e, file) => onError(e, file)
262+
(hubFile, file) => {
263+
const ruleFileContext = fileContextRef.current.get(file.name);
264+
onSuccess(file, hubFile, ruleFileContext);
265+
fileContextRef.current.delete(file.name);
266+
},
267+
(e, file) => {
268+
const ruleFileContext = fileContextRef.current.get(file.name);
269+
onError(e, file, ruleFileContext);
270+
fileContextRef.current.delete(file.name);
271+
}
259272
);
260273

261274
const { mutate: uploadTaskgroupFile } = useUploadTaskgroupFileMutation(
262-
(_, { file }) => onSuccess(file),
263-
(e, { file }) => onError(e, file)
275+
(_, { file }) => {
276+
const ruleFileContext = fileContextRef.current.get(file.name);
277+
onSuccess(file, undefined, ruleFileContext);
278+
fileContextRef.current.delete(file.name);
279+
},
280+
(e, { file }) => {
281+
const ruleFileContext = fileContextRef.current.get(file.name);
282+
onError(e, file, ruleFileContext);
283+
fileContextRef.current.delete(file.name);
284+
}
264285
);
265286

266-
const uploadFile = (file: File, taskgroupId?: number) => {
287+
const uploadFile = (
288+
file: File,
289+
taskgroupId?: number,
290+
ruleFileContext?: UploadFile
291+
) => {
292+
// Store the ruleFile context for retrieval in callbacks
293+
if (ruleFileContext) {
294+
fileContextRef.current.set(file.name, ruleFileContext);
295+
}
296+
267297
if (taskgroupId === undefined) {
268298
createRuleFile({ file });
269299
} else {

0 commit comments

Comments
 (0)