From 360ecaa93d677f3d98fa852386d908a17d0e9bcc Mon Sep 17 00:00:00 2001 From: Roberto Jimenez Sanchez Date: Tue, 11 Mar 2025 16:41:28 +0100 Subject: [PATCH] Improve MigrateStep and PullStep --- .betterer.results | 3 -- .../provisioning/Wizard/MigrateStep.tsx | 40 +++++++++---------- .../features/provisioning/Wizard/PullStep.tsx | 39 +++++++++--------- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/.betterer.results b/.betterer.results index 33c7f167b9a..0f785f57c76 100644 --- a/.betterer.results +++ b/.betterer.results @@ -5709,9 +5709,6 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings. Wrap text with ", "1"], [0, 0, 0, "No untranslated strings. Wrap text with ", "2"] ], - "public/app/features/provisioning/GettingStarted/BootstrapStep.tsx:5381": [ - [0, 0, 0, "No untranslated strings. Wrap text with ", "0"] - ], "public/app/features/provisioning/GettingStarted/EnhancedFeatures.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "1"], diff --git a/public/app/features/provisioning/Wizard/MigrateStep.tsx b/public/app/features/provisioning/Wizard/MigrateStep.tsx index 96e111dc888..04ebc7f9e85 100644 --- a/public/app/features/provisioning/Wizard/MigrateStep.tsx +++ b/public/app/features/provisioning/Wizard/MigrateStep.tsx @@ -23,48 +23,46 @@ export function MigrateStep({ onStatusChange, onRunningChange, onErrorChange }: const history = watch('migrate.history'); const migrateName = migrateQuery.data?.metadata?.name; - // Handle initial migration useEffect(() => { + // Early return conditions if (!repositoryName || hasInitialized.current) { return; } - let isMounted = true; hasInitialized.current = true; + let isMounted = true; + + const handleError = (error: unknown) => { + if (!isMounted) { + return; + } + const errorMessage = error instanceof Error ? error.message : 'Failed to start migration operation'; + onErrorChange(errorMessage); + onStatusChange(false); + onRunningChange(false); + }; const startMigrate = async () => { try { - if (!isMounted) { - return; - } - onRunningChange(true); const response = await migrateRepo({ name: repositoryName, - body: { - identifier, - history, - }, + body: { identifier, history }, }).unwrap(); - if (response?.metadata?.name) { - } else { - onErrorChange('Invalid response from migration operation'); - onStatusChange(false); - onRunningChange(false); - } - } catch (error) { if (!isMounted) { return; } - onErrorChange(error instanceof Error ? error.message : 'Failed to start migration operation'); - onStatusChange(false); - onRunningChange(false); + + if (!response?.metadata?.name) { + handleError(new Error('Invalid response from migration operation')); + } + } catch (error) { + handleError(error); } }; startMigrate(); - return () => { isMounted = false; }; diff --git a/public/app/features/provisioning/Wizard/PullStep.tsx b/public/app/features/provisioning/Wizard/PullStep.tsx index e65b9afcd6c..8c666e2446e 100644 --- a/public/app/features/provisioning/Wizard/PullStep.tsx +++ b/public/app/features/provisioning/Wizard/PullStep.tsx @@ -21,47 +21,46 @@ export function PullStep({ onStatusChange, onRunningChange, onErrorChange }: Pul const repositoryName = watch('repositoryName'); const syncName = syncQuery.data?.metadata?.name; - // Handle initial sync useEffect(() => { + // Early return conditions if (!repositoryName || hasInitialized.current) { return; } - let isMounted = true; hasInitialized.current = true; + let isMounted = true; + + const handleError = (error: unknown) => { + if (!isMounted) { + return; + } + const errorMessage = error instanceof Error ? error.message : 'Failed to start sync operation'; + onErrorChange(errorMessage); + onStatusChange(false); + onRunningChange(false); + }; const startSync = async () => { try { - if (!isMounted) { - return; - } - onRunningChange(true); const response = await syncRepo({ name: repositoryName, - body: { - incremental: false, - }, + body: { incremental: false }, }).unwrap(); - if (response?.metadata?.name) { - } else { - onErrorChange('Invalid response from sync operation'); - onStatusChange(false); - onRunningChange(false); - } - } catch (error) { if (!isMounted) { return; } - onErrorChange(error instanceof Error ? error.message : 'Failed to start sync operation'); - onStatusChange(false); - onRunningChange(false); + + if (!response?.metadata?.name) { + handleError(new Error('Invalid response from sync operation')); + } + } catch (error) { + handleError(error); } }; startSync(); - return () => { isMounted = false; };