Skip to content

Commit 56177e6

Browse files
committed
Add useImageRegistry hook and integrate in controllers
Introduced a new useImageRegistry hook to centralize image registry data fetching and fallback logic. Updated ChaosStudioClone, ChaosStudioEdit, and StudioOverview components to use this hook, replacing direct queries and improving code reuse.. Signed-off-by: Harshit Panchbhai <[email protected]>
1 parent 92e8e95 commit 56177e6

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

chaoscenter/web/src/controllers/ChaosStudioClone/ChaosStudioClone.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { listExperiment, runChaosExperiment, saveChaosExperiment } from '@api/co
88
import experimentYamlService from '@services/experiment';
99
import { InfrastructureType } from '@api/entities';
1010
import Loader from '@components/Loader';
11-
import { useSearchParams, useUpdateSearchParams } from '@hooks';
11+
import { useSearchParams, useUpdateSearchParams, useImageRegistry } from '@hooks';
1212
import { ExperimentManifest, StudioMode } from '@models';
1313

1414
export default function ChaosStudioCloneController(): React.ReactElement {
@@ -17,6 +17,7 @@ export default function ChaosStudioCloneController(): React.ReactElement {
1717
const searchParams = useSearchParams();
1818
const updateSearchParams = useUpdateSearchParams();
1919
const hasUnsavedChangesInURL = searchParams.get('unsavedChanges') === 'true';
20+
const { imageRegistry } = useImageRegistry();
2021

2122
// <!-- counting state since we have 2 async functions and need to flip state when both of said functions have resolved their promises -->
2223
const [showStudio, setShowStudio] = React.useState<number>(0);
@@ -56,7 +57,8 @@ export default function ChaosStudioCloneController(): React.ReactElement {
5657
id: experimentData?.infra?.infraID,
5758
namespace: experimentData.infra?.infraNamespace,
5859
environmentID: experimentData.infra?.environmentID
59-
}
60+
},
61+
imageRegistry: imageRegistry
6062
})
6163
.then(() => setShowStudio(oldState => oldState + 1));
6264
const parsedManifest = parse(experimentData.experimentManifest) as ExperimentManifest;
@@ -66,7 +68,7 @@ export default function ChaosStudioCloneController(): React.ReactElement {
6668
.then(() => setShowStudio(oldState => oldState + 1));
6769
}
6870
// eslint-disable-next-line react-hooks/exhaustive-deps
69-
}, [experimentData, experimentID, hasUnsavedChangesInURL]);
71+
}, [experimentData, experimentID, hasUnsavedChangesInURL, imageRegistry]);
7072

7173
const [saveChaosExperimentMutation, { loading: saveChaosExperimentLoading }] = saveChaosExperiment({
7274
onError: error => showError(error.message)

chaoscenter/web/src/controllers/ChaosStudioEdit/ChaosStudioEdit.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { listExperiment, runChaosExperiment, saveChaosExperiment } from '@api/co
88
import experimentYamlService from '@services/experiment';
99
import { ExperimentType, InfrastructureType, RecentExperimentRun } from '@api/entities';
1010
import Loader from '@components/Loader';
11-
import { useSearchParams, useUpdateSearchParams } from '@hooks';
11+
import { useSearchParams, useUpdateSearchParams, useImageRegistry } from '@hooks';
1212
import RightSideBarV2 from '@components/RightSideBarV2';
1313
import { StudioMode } from '@models';
1414
import { cronEnabled } from 'utils';
@@ -20,6 +20,7 @@ export default function ChaosStudioEditController(): React.ReactElement {
2020
const updateSearchParams = useUpdateSearchParams();
2121
const hasUnsavedChangesInURL = searchParams.get('unsavedChanges') === 'true';
2222
const experimentType = searchParams.get('experimentType');
23+
const { imageRegistry } = useImageRegistry();
2324

2425
// <!-- counting state since we have 2 async functions and need to flip state when both of said functions have resolved their promises -->
2526
const [showStudio, setShowStudio] = React.useState<number>(0);
@@ -61,7 +62,8 @@ export default function ChaosStudioEditController(): React.ReactElement {
6162
id: experimentData?.infra?.infraID,
6263
namespace: experimentData.infra?.infraNamespace,
6364
environmentID: experimentData?.infra?.environmentID
64-
}
65+
},
66+
imageRegistry: imageRegistry
6567
})
6668
.then(() => setShowStudio(oldState => oldState + 1));
6769
experimentHandler
@@ -75,7 +77,7 @@ export default function ChaosStudioEditController(): React.ReactElement {
7577
setIsCronEnabled(validateCron);
7678
}
7779
// eslint-disable-next-line react-hooks/exhaustive-deps
78-
}, [experimentData, experimentID, hasUnsavedChangesInURL]);
80+
}, [experimentData, experimentID, hasUnsavedChangesInURL, imageRegistry]);
7981

8082
const [saveChaosExperimentMutation, { loading: saveChaosExperimentLoading }] = saveChaosExperiment({
8183
onError: error => showError(error.message),

chaoscenter/web/src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './useTrackedRef';
1010
export * from './useDocumentTitle';
1111
export * from './useDeepCompareEffect';
1212
export * from './useProjectFilters';
13+
export * from './useImageRegistry';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { getImageRegistry } from '@api/core/ImageRegistry';
2+
import { getScope } from '@utils';
3+
import type { ImageRegistry } from '@db';
4+
5+
// Default image registry configuration
6+
const DEFAULT_IMAGE_REGISTRY = {
7+
name: 'docker.io/litmuschaos',
8+
repo: 'litmuschaos',
9+
secret: ''
10+
} as const;
11+
12+
export function useImageRegistry(): { imageRegistry: ImageRegistry | undefined; loading: boolean } {
13+
const scope = getScope();
14+
15+
// Fetch imageRegistry data
16+
const { data: getImageRegistryData, loading } = getImageRegistry({
17+
projectID: scope.projectID
18+
});
19+
20+
// Create imageRegistry object with fallback values
21+
const imageRegistry = getImageRegistryData?.getImageRegistry ? {
22+
name: getImageRegistryData.getImageRegistry.imageRegistryInfo.imageRegistryName,
23+
repo: getImageRegistryData.getImageRegistry.imageRegistryInfo.imageRepoName,
24+
secret: getImageRegistryData.getImageRegistry.imageRegistryInfo.secretName,
25+
} : DEFAULT_IMAGE_REGISTRY;
26+
27+
return { imageRegistry, loading };
28+
}

chaoscenter/web/src/views/StudioOverview/StudioOverview.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ import { isEqual } from 'lodash-es';
1717
import type { ExperimentMetadata } from '@db';
1818
import { useStrings } from '@strings';
1919
import FormErrorListener from '@components/FormErrorListener';
20-
import { useUpdateSearchParams } from '@hooks';
20+
import { useUpdateSearchParams, useImageRegistry } from '@hooks';
2121
import NameDescriptionTags from '@components/NameIdDescriptionTags';
2222
import { ChaosInfrastructureReferenceFieldProps, StudioErrorState, StudioTabs } from '@models';
2323
import experimentYamlService from 'services/experiment';
2424
import KubernetesChaosInfrastructureReferenceFieldController from '@controllers/KubernetesChaosInfrastructureReferenceField';
2525
import { InfrastructureType } from '@api/entities';
26-
import { getImageRegistry } from '@api/core/ImageRegistry';
27-
import { getScope } from '@utils';
2826
import css from './StudioOverview.module.scss';
2927

3028
interface StudioOverviewViewProps {
@@ -54,19 +52,7 @@ export default function StudioOverviewView({
5452

5553
const [currentExperiment, setCurrentExperiment] = React.useState<ExperimentMetadata | undefined>();
5654

57-
const scope = getScope();
58-
59-
// Fetch the image registry data using Apollo's useQuery hook
60-
const { data: getImageRegistryData, loading: imageRegistryLoading } = getImageRegistry({
61-
projectID: scope.projectID,
62-
});
63-
64-
const imageRegistry = getImageRegistryData?.getImageRegistry?{
65-
name: getImageRegistryData.getImageRegistry.imageRegistryInfo.imageRegistryName,
66-
repo: getImageRegistryData.getImageRegistry.imageRegistryInfo.imageRepoName,
67-
secret: getImageRegistryData.getImageRegistry.imageRegistryInfo.secretName,
68-
}
69-
: undefined;
55+
const { imageRegistry, loading: imageRegistryLoading } = useImageRegistry();
7056

7157
React.useEffect(() => {
7258
experimentHandler?.getExperiment(experimentKey).then(experiment => {

0 commit comments

Comments
 (0)