Skip to content

Commit 183ed6b

Browse files
committed
Remove useEffect
1 parent db11a7e commit 183ed6b

File tree

7 files changed

+50
-13
lines changed

7 files changed

+50
-13
lines changed

packages/maas/frontend/src/odh/modelServingExtensions/modelDeploymentWizard/MaaSEndpointCheckbox.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type MaaSFieldProps = {
2424
isDisabled?: boolean;
2525
};
2626

27-
const MaaSField: React.FC<MaaSFieldProps> = ({ id, value, onChange }) => {
27+
const MaaSField: React.FC<MaaSFieldProps> = ({ id, value, onChange, isDisabled }) => {
2828
const handleCheckboxChange = (_: React.FormEvent<HTMLInputElement>, checked: boolean): void => {
2929
onChange({ isChecked: checked });
3030
};
@@ -71,6 +71,12 @@ export const MaaSEndpointFieldWizardField: MaaSFieldType = {
7171
wizardFormData.modelServer?.data?.name === LLMD_SERVING_ID,
7272
reducerFunctions: {
7373
setFieldData: setMaaSFieldData,
74+
getFieldData: (storedValue, wizardState) => {
75+
if (!wizardState.modelAvailability?.data?.saveAsAiAsset) {
76+
return { isChecked: false };
77+
}
78+
return storedValue;
79+
},
7480
getInitialFieldData: getInitialMaaSFieldData,
7581
validationSchema: maasFieldSchema,
7682
},

packages/model-serving/src/components/deploymentWizard/fields/GenericFieldRenderer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import type { WizardField } from '../types';
2+
import { resolveFieldValue, type WizardField } from '../types';
33
import type { UseModelDeploymentWizardState } from '../useDeploymentWizard';
44
import type { ExternalDataMap } from '../ExternalDataLoader';
55

@@ -26,7 +26,7 @@ export const GenericFieldRenderer: React.FC<GenericFieldRendererProps> = ({
2626
<React.Fragment key={field.id}>
2727
{field.component({
2828
id: field.id,
29-
value: wizardState.state[field.id],
29+
value: resolveFieldValue(field, wizardState.state),
3030
onChange: (value) => {
3131
wizardState.dispatch({
3232
type: 'setFieldData',

packages/model-serving/src/components/deploymentWizard/steps/AdvancedOptionsStep.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { type UseModelDeploymentWizardState } from '../useDeploymentWizard';
2424
import { AvailableAiAssetsFieldsComponent } from '../fields/ModelAvailabilityFields';
2525
import { showAuthWarning } from '../hooks/useAuthWarning';
2626
import type { ExternalDataMap } from '../ExternalDataLoader';
27+
import { resolveFieldValue } from '../types';
2728

2829
export const accessReviewResource: AccessReviewResourceAttributes = {
2930
group: 'rbac.authorization.k8s.io',
@@ -47,9 +48,13 @@ export const AdvancedSettingsStepContent: React.FC<AdvancedSettingsStepContentPr
4748
const { isExternalRouteVisible, shouldAutoCheckTokens } = wizardState.advancedOptions;
4849

4950
const isMaaSChecked = (() => {
50-
const field = wizardState.state['maas/save-as-maas-checkbox'];
51-
if (typeof field === 'object' && field !== null && 'isChecked' in field) {
52-
return !!field.isChecked;
51+
const fieldDef = wizardState.fields.find((f) => f.id === 'maas/save-as-maas-checkbox');
52+
if (!fieldDef) {
53+
return false;
54+
}
55+
const resolved = resolveFieldValue(fieldDef, wizardState.state);
56+
if (typeof resolved === 'object' && resolved !== null && 'isChecked' in resolved) {
57+
return !!resolved.isChecked;
5358
}
5459
return false;
5560
})();

packages/model-serving/src/components/deploymentWizard/steps/ReviewStep.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ import {
1717
isConnectionTypeDataField,
1818
} from '@odh-dashboard/internal/concepts/connectionTypes/utils';
1919
import { UseModelDeploymentWizardState } from '../useDeploymentWizard';
20-
import { ModelLocationType, ModelTypeLabel, WizardReviewSection, WizardStepTitle } from '../types';
20+
import {
21+
ModelLocationType,
22+
ModelTypeLabel,
23+
WizardReviewSection,
24+
WizardStepTitle,
25+
resolveFieldValue,
26+
} from '../types';
2127
import { deploymentStrategyRecreate } from '../fields/DeploymentStrategyField';
2228
import { ExternalDataMap } from '../ExternalDataLoader';
2329
import { isWizardStepTitle } from '../utils';
@@ -368,7 +374,7 @@ export const ReviewStepContent: React.FC<ReviewStepContentProps> = ({
368374
if (!field.getReviewSections) {
369375
return [];
370376
}
371-
const value = wizardState.state[field.id];
377+
const value = resolveFieldValue(field, wizardState.state);
372378
const fieldExternalData = externalData?.[field.id];
373379
return field.getReviewSections(value, wizardState.state, fieldExternalData?.data);
374380
});

packages/model-serving/src/components/deploymentWizard/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export type WizardField<
206206
reducerFunctions: {
207207
// TODO: make dispatch function that clears if this field's dependencies are changing
208208
setFieldData: (fieldData: FieldData) => FieldData;
209+
getFieldData?: (storedValue: FieldData, wizardState: WizardFormData['state']) => FieldData;
209210
getInitialFieldData: (fieldData?: FieldData, externalData?: ExternalData) => FieldData;
210211
validationSchema?: z.ZodSchema<FieldData>;
211212
};
@@ -228,6 +229,13 @@ export type WizardField<
228229
) => WizardReviewSection[];
229230
};
230231

232+
export const resolveFieldValue = (field: WizardField, state: WizardFormData['state']): unknown => {
233+
const storedValue: unknown = state[field.id];
234+
return field.reducerFunctions.getFieldData
235+
? field.reducerFunctions.getFieldData(storedValue, state)
236+
: storedValue;
237+
};
238+
231239
// actual fields
232240

233241
export type ModelServerTemplateField = DeploymentWizardFieldBase<'modelServerTemplate'> & {

packages/model-serving/src/components/deploymentWizard/useDeploymentWizardValidation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useZodFormValidation } from '@odh-dashboard/internal/hooks/useZodFormVa
44
import { isK8sNameDescriptionDataValid } from '@odh-dashboard/internal/concepts/k8s/K8sNameDescriptionField/utils';
55
import { useValidation } from '@odh-dashboard/internal/utilities/useValidation';
66
import { hardwareProfileValidationSchema } from '@odh-dashboard/internal/concepts/hardwareProfiles/validationUtils';
7-
import type { WizardField, WizardFormData } from './types';
7+
import { resolveFieldValue, type WizardField, type WizardFormData } from './types';
88
import { modelSourceStepSchema, type ModelSourceStepData } from './steps/ModelSourceStep';
99
import { externalRouteFieldSchema } from './fields/ExternalRouteField';
1010
import { tokenAuthenticationFieldSchema } from './fields/TokenAuthenticationField';
@@ -79,7 +79,7 @@ export const useModelDeploymentWizardValidation = (
7979
runtimeArgs: state.runtimeArgs.data,
8080
environmentVariables: state.environmentVariables.data,
8181
...step3Fields.reduce<Record<string, unknown>>((acc, field) => {
82-
acc[field.id] = state[field.id];
82+
acc[field.id] = resolveFieldValue(field, state);
8383
return acc;
8484
}, {}),
8585
},

packages/model-serving/src/components/deploymentWizard/useWizardFieldApply.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import type { ResolvedExtension } from '@openshift/dynamic-plugin-sdk';
33
import type { K8sResourceCommon } from '@openshift/dynamic-plugin-sdk-utils';
44
import { useResolvedExtensions } from '@odh-dashboard/plugin-core';
5-
import type { WizardFormData } from './types';
5+
import type { WizardField, WizardFormData } from './types';
66
import {
77
isWizardFieldApplyExtension,
88
isWizardField2Extension,
@@ -47,13 +47,25 @@ export const useWizardFieldApply = (
4747
return applyExtensions.filter((ext) => activeFieldIds.has(ext.properties.fieldId));
4848
}, [applyExtensions, activeFieldIds]);
4949

50+
const fieldMap = React.useMemo((): Map<string, WizardField> => {
51+
const map = new Map<string, WizardField>();
52+
for (const ext of fieldExtensions) {
53+
map.set(ext.properties.field.id, ext.properties.field);
54+
}
55+
return map;
56+
}, [fieldExtensions]);
57+
5058
const applyFieldData = React.useCallback(
5159
(deployment: Deployment): Deployment => {
5260
let result = deployment;
5361

5462
for (const applyExt of activeApplyExtensions) {
5563
const { fieldId } = applyExt.properties;
56-
const fieldData: unknown = wizardState[fieldId];
64+
const rawFieldData: unknown = wizardState[fieldId];
65+
const field = fieldMap.get(fieldId);
66+
const fieldData: unknown = field?.reducerFunctions.getFieldData
67+
? field.reducerFunctions.getFieldData(rawFieldData, wizardState)
68+
: rawFieldData;
5769

5870
if (fieldData !== undefined) {
5971
result = applyExt.properties.apply(result, fieldData, wizardState);
@@ -76,7 +88,7 @@ export const useWizardFieldApply = (
7688

7789
return result;
7890
},
79-
[activeApplyExtensions, wizardState, navSourceMetadata],
91+
[activeApplyExtensions, fieldMap, wizardState, navSourceMetadata],
8092
);
8193

8294
return React.useMemo(

0 commit comments

Comments
 (0)