Skip to content

Commit

Permalink
update useGenericObjectState types
Browse files Browse the repository at this point in the history
  • Loading branch information
christianvogt committed Sep 30, 2024
1 parent 228e221 commit 6799579
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const useRunFormData = (
const { project } = usePipelinesAPI();
const { pipeline, version, experiment, nameDesc } = initialFormData || {};

const formState = useGenericObjectState<RunFormData>({
const formState = useGenericObjectState<RunFormData>(() => ({
project,
nameDesc: nameDesc ?? { name: '', description: '' },
pipeline: pipeline ?? null,
Expand All @@ -161,7 +161,7 @@ const useRunFormData = (
{},
),
...initialFormData,
});
}));
const [, setFormValue] = formState;

useUpdateExperimentFormData(formState, experiment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { Link } from 'react-router-dom';
import FormSection from '~/components/pf-overrides/FormSection';
import ApplicationsPage from '~/pages/ApplicationsPage';
import { modelRegistryUrl, registeredModelUrl } from '~/pages/modelRegistry/screens/routeUtils';
import { ValueOf } from '~/typeHelpers';
import { useRegisterModelData, RegistrationCommonFormData } from './useRegisterModelData';
import { useRegisterModelData } from './useRegisterModelData';
import { isRegisterModelSubmitDisabled, registerModel } from './utils';
import RegistrationCommonFormSections from './RegistrationCommonFormSections';
import { useRegistrationCommonState } from './useRegistrationCommonState';
Expand Down Expand Up @@ -90,10 +89,7 @@ const RegisterModel: React.FC = () => {
</FormSection>
<RegistrationCommonFormSections
formData={formData}
setData={(
propKey: keyof RegistrationCommonFormData,
propValue: ValueOf<RegistrationCommonFormData>,
) => setData(propKey, propValue)}
setData={(propKey, propValue) => setData(propKey, propValue)}
isFirstVersion
/>
</StackItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import ApplicationsPage from '~/pages/ApplicationsPage';
import { modelRegistryUrl, registeredModelUrl } from '~/pages/modelRegistry/screens/routeUtils';
import useRegisteredModels from '~/concepts/modelRegistry/apiHooks/useRegisteredModels';
import { filterLiveModels } from '~/concepts/modelRegistry/utils';
import { ValueOf } from '~/typeHelpers';
import { RegistrationCommonFormData, useRegisterVersionData } from './useRegisterModelData';
import { useRegisterVersionData } from './useRegisterModelData';
import { isRegisterVersionSubmitDisabled, registerVersion } from './utils';
import RegistrationCommonFormSections from './RegistrationCommonFormSections';
import { useRegistrationCommonState } from './useRegistrationCommonState';
Expand Down Expand Up @@ -119,10 +118,7 @@ const RegisterVersion: React.FC = () => {
<StackItem>
<RegistrationCommonFormSections
formData={formData}
setData={(
propKey: keyof RegistrationCommonFormData,
propValue: ValueOf<RegistrationCommonFormData>,
) => setData(propKey, propValue)}
setData={(propKey, propValue) => setData(propKey, propValue)}
isFirstVersion={false}
latestVersion={latestVersion}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ const InferenceServiceServingRuntimeSection: React.FC<
}
onChange={(option) => {
setData('servingRuntimeName', option);
setData('format', '');
setData('format', {
name: '',
});
}}
/>
</FormGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ const DeployNIMServiceModal: React.FC<DeployNIMServiceModalProps> = ({
</StackItem>
<ServingRuntimeSizeSection
data={createDataInferenceService}
setData={setCreateDataInferenceService}
setData={(key, value) => setCreateDataInferenceService(key, value)}

Check warning on line 324 in frontend/src/pages/modelServing/screens/projects/NIMServiceModal/DeployNIMServiceModal.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelServing/screens/projects/NIMServiceModal/DeployNIMServiceModal.tsx#L324

Added line #L324 was not covered by tests
sizes={sizes}
servingRuntimeSelected={servingRuntimeSelected}
acceleratorProfileState={acceleratorProfileState}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ import {
} from '@patternfly/react-core';
import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons';
import { UpdateObjectAtPropAndValue } from '~/pages/projects/types';
import {
CreatingInferenceServiceObject,
CreatingServingRuntimeObject,
} from '~/pages/modelServing/screens/types';
import { CreatingModelServingObjectCommon } from '~/pages/modelServing/screens/types';

import ServingRuntimeTokenSection from './ServingRuntimeTokenSection';

type AuthServingRuntimeSectionProps = {
data: CreatingServingRuntimeObject | CreatingInferenceServiceObject;
setData:
| UpdateObjectAtPropAndValue<CreatingServingRuntimeObject>
| UpdateObjectAtPropAndValue<CreatingInferenceServiceObject>;
data: CreatingModelServingObjectCommon;
setData: UpdateObjectAtPropAndValue<CreatingModelServingObjectCommon>;
allowCreate: boolean;
publicRoute?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const ManageServingRuntimeModal: React.FC<ManageServingRuntimeModalProps> = ({
<StackItem>
<ServingRuntimeSizeSection
data={createData}
setData={setCreateData}
setData={(key, value) => setCreateData(key, value)}
sizes={sizes}
servingRuntimeSelected={servingRuntimeSelected}
acceleratorProfileState={initialAcceleratorProfile}
Expand All @@ -226,7 +226,7 @@ const ManageServingRuntimeModal: React.FC<ManageServingRuntimeModalProps> = ({
</StackItem>
<AuthServingRuntimeSection
data={createData}
setData={setCreateData}
setData={(key, value) => setCreateData(key, value)}
allowCreate={allowCreate}
publicRoute
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ import * as React from 'react';
import { FormGroup, Grid } from '@patternfly/react-core';
import IndentSection from '~/pages/projects/components/IndentSection';
import { UpdateObjectAtPropAndValue } from '~/pages/projects/types';
import {
CreatingInferenceServiceObject,
CreatingServingRuntimeObject,
} from '~/pages/modelServing/screens/types';
import { CreatingModelServingObjectCommon } from '~/pages/modelServing/screens/types';
import { ContainerResourceAttributes, ContainerResources } from '~/types';
import CPUField from '~/components/CPUField';
import MemoryField from '~/components/MemoryField';

type ServingRuntimeSizeExpandedFieldProps = {
data: CreatingServingRuntimeObject | CreatingInferenceServiceObject;
setData:
| UpdateObjectAtPropAndValue<CreatingServingRuntimeObject>
| UpdateObjectAtPropAndValue<CreatingInferenceServiceObject>;
data: CreatingModelServingObjectCommon;
setData: UpdateObjectAtPropAndValue<CreatingModelServingObjectCommon>;
};

type ResourceKeys = keyof ContainerResources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { FormGroup, Stack, StackItem, Popover, Icon } from '@patternfly/react-co
import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons';
import { UpdateObjectAtPropAndValue } from '~/pages/projects/types';
import {
CreatingInferenceServiceObject,
CreatingServingRuntimeObject,
CreatingModelServingObjectCommon,
ModelServingSize,
} from '~/pages/modelServing/screens/types';
import { ServingRuntimeKind } from '~/k8sTypes';
Expand All @@ -18,10 +17,8 @@ import SimpleSelect from '~/components/SimpleSelect';
import ServingRuntimeSizeExpandedField from './ServingRuntimeSizeExpandedField';

type ServingRuntimeSizeSectionProps = {
data: CreatingServingRuntimeObject | CreatingInferenceServiceObject;
setData:
| UpdateObjectAtPropAndValue<CreatingServingRuntimeObject>
| UpdateObjectAtPropAndValue<CreatingInferenceServiceObject>;
data: CreatingModelServingObjectCommon;
setData: UpdateObjectAtPropAndValue<CreatingModelServingObjectCommon>;
sizes: ModelServingSize[];
servingRuntimeSelected?: ServingRuntimeKind;
acceleratorProfileState: AcceleratorProfileState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import {
import { ExclamationCircleIcon, MinusCircleIcon } from '@patternfly/react-icons';
import { UpdateObjectAtPropAndValue } from '~/pages/projects/types';
import {
CreatingInferenceServiceObject,
CreatingServingRuntimeObject,
CreatingModelServingObjectCommon,
ServingRuntimeToken,
} from '~/pages/modelServing/screens/types';
import { translateDisplayNameForK8s } from '~/concepts/k8s/utils';

type ServingRuntimeTokenInputProps = {
data: CreatingServingRuntimeObject | CreatingInferenceServiceObject;
setData:
| UpdateObjectAtPropAndValue<CreatingServingRuntimeObject>
| UpdateObjectAtPropAndValue<CreatingInferenceServiceObject>;
data: CreatingModelServingObjectCommon;
setData: UpdateObjectAtPropAndValue<CreatingModelServingObjectCommon>;
token: ServingRuntimeToken;
disabled?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import { Alert, Button, Checkbox, FormGroup, Stack, StackItem } from '@patternfl
import { PlusCircleIcon } from '@patternfly/react-icons';
import IndentSection from '~/pages/projects/components/IndentSection';
import { UpdateObjectAtPropAndValue } from '~/pages/projects/types';
import {
CreatingInferenceServiceObject,
CreatingServingRuntimeObject,
} from '~/pages/modelServing/screens/types';
import { CreatingModelServingObjectCommon } from '~/pages/modelServing/screens/types';
import ServingRuntimeTokenInput from './ServingRuntimeTokenInput';

type ServingRuntimeTokenSectionProps = {
data: CreatingServingRuntimeObject | CreatingInferenceServiceObject;
setData:
| UpdateObjectAtPropAndValue<CreatingServingRuntimeObject>
| UpdateObjectAtPropAndValue<CreatingInferenceServiceObject>;
data: CreatingModelServingObjectCommon;
setData: UpdateObjectAtPropAndValue<CreatingModelServingObjectCommon>;
allowCreate: boolean;
createNewToken: () => void;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ const ManageKServeModal: React.FC<ManageKServeModalProps> = ({
/>
<ServingRuntimeSizeSection
data={createDataInferenceService}
setData={setCreateDataInferenceService}
setData={(key, value) => setCreateDataInferenceService(key, value)}
sizes={sizes}
servingRuntimeSelected={servingRuntimeSelected}
acceleratorProfileState={acceleratorProfileState}
Expand All @@ -388,7 +388,7 @@ const ManageKServeModal: React.FC<ManageKServeModalProps> = ({
{isAuthorinoEnabled && (
<AuthServingRuntimeSection
data={createDataInferenceService}
setData={setCreateDataInferenceService}
setData={(key, value) => setCreateDataInferenceService(key, value)}
allowCreate={allowCreate}
publicRoute
/>
Expand Down
18 changes: 8 additions & 10 deletions frontend/src/pages/modelServing/screens/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,9 @@ export type SupportedModelFormatsInfo = {
priority?: number;
};

export type CreatingServingRuntimeObject = {
name: string;
export type CreatingServingRuntimeObject = CreatingModelServingObjectCommon & {
servingRuntimeTemplateName: string;
numReplicas: number;
modelSize: ModelServingSize;
externalRoute: boolean;
tokenAuth: boolean;
tokens: ServingRuntimeToken[];
imageName?: string;
supportedModelFormatsInfo?: SupportedModelFormatsInfo;
};
Expand All @@ -64,19 +59,22 @@ export type ModelServingSize = {
resources: ContainerResources;
};

export type CreatingInferenceServiceObject = {
name: string;
export type CreatingInferenceServiceObject = CreatingModelServingObjectCommon & {
project: string;
servingRuntimeName: string;
storage: InferenceServiceStorage;
modelSize: ModelServingSize;
format: InferenceServiceFormat;
maxReplicas: number;
minReplicas: number;
labels?: Record<string, string>;
};

export type CreatingModelServingObjectCommon = {
name: string;
modelSize: ModelServingSize;
externalRoute: boolean;
tokenAuth: boolean;
tokens: ServingRuntimeToken[];
labels?: Record<string, string>;
};

export enum InferenceServiceStorageType {
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/pages/projects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {
Volume,
VolumeMount,
} from '~/types';
import { ValueOf } from '~/typeHelpers';
import { AWSSecretKind } from '~/k8sTypes';
import { AcceleratorProfileState } from '~/utilities/useAcceleratorProfileState';
import { AcceleratorProfileSelectFieldState } from '~/pages/notebookController/screens/server/AcceleratorProfileSelectField';
import { K8sNameDescriptionFieldData } from '~/concepts/k8s/K8sNameDescriptionField/types';
import { AwsKeys } from './dataConnections/const';

export type UpdateObjectAtPropAndValue<T> = (propKey: keyof T, propValue: ValueOf<T>) => void;
export type UpdateObjectAtPropAndValue<T> = <K extends keyof T>(
propKey: K,
propValue: Partial<T>[K],
) => void;

export type NameDescType = {
name: string;
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utilities/useGenericObjectState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export type GenericObjectState<T> = [
resetDefault: () => void,
];

const useGenericObjectState = <T>(defaultData: T): GenericObjectState<T> => {
const useGenericObjectState = <T>(defaultData: T | (() => T)): GenericObjectState<T> => {
const [value, setValue] = React.useState<T>(defaultData);

const setPropValue = React.useCallback<UpdateObjectAtPropAndValue<T>>((propKey, propValue) => {
setValue((oldValue) => ({ ...oldValue, [propKey]: propValue }));
}, []);

const defaultDataRef = React.useRef(defaultData);
const defaultDataRef = React.useRef(value);
const resetToDefault = React.useCallback(() => {
setValue(defaultDataRef.current);
}, []);
Expand Down

0 comments on commit 6799579

Please sign in to comment.