Skip to content

Commit

Permalink
update useGenericObjectState types
Browse files Browse the repository at this point in the history
  • Loading branch information
christianvogt committed Oct 3, 2024
1 parent 7ff95e0 commit bea9b86
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 92 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={setData}
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={setData}
isFirstVersion={false}
latestVersion={latestVersion}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ import { ModelVersion } from '~/concepts/modelRegistry/types';
import { ModelLocationType, RegistrationCommonFormData } from './useRegisterModelData';
import { ConnectionModal } from './ConnectionModal';

type RegistrationCommonFormSectionsProps = {
formData: RegistrationCommonFormData;
setData: UpdateObjectAtPropAndValue<RegistrationCommonFormData>;
type RegistrationCommonFormSectionsProps<D extends RegistrationCommonFormData> = {
formData: D;
setData: UpdateObjectAtPropAndValue<D>;
isFirstVersion: boolean;
latestVersion?: ModelVersion;
};

const RegistrationCommonFormSections: React.FC<RegistrationCommonFormSectionsProps> = ({
const RegistrationCommonFormSections = <D extends RegistrationCommonFormData>({
formData,
setData,
isFirstVersion,
latestVersion,
}) => {
}: RegistrationCommonFormSectionsProps<D>): React.ReactNode => {
const [isAutofillModalOpen, setAutofillModalOpen] = React.useState(false);

const connectionDataMap: Record<string, keyof RegistrationCommonFormData> = {
const connectionDataMap: Record<
string,
keyof Pick<
RegistrationCommonFormData,
'modelLocationEndpoint' | 'modelLocationBucket' | 'modelLocationRegion'
>
> = {
AWS_S3_ENDPOINT: 'modelLocationEndpoint',
AWS_S3_BUCKET: 'modelLocationBucket',
AWS_DEFAULT_REGION: 'modelLocationRegion',
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 @@ -11,28 +11,23 @@ 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>;
type AuthServingRuntimeSectionProps<D extends CreatingModelServingObjectCommon> = {
data: D;
setData: UpdateObjectAtPropAndValue<D>;
allowCreate: boolean;
publicRoute?: boolean;
};

const AuthServingRuntimeSection: React.FC<AuthServingRuntimeSectionProps> = ({
const AuthServingRuntimeSection = <D extends CreatingModelServingObjectCommon>({
data,
setData,
allowCreate,
publicRoute,
}) => {
}: AuthServingRuntimeSectionProps<D>): React.ReactNode => {
const createNewToken = React.useCallback(() => {
const name = 'default-name';
const duplicated = data.tokens.filter((token) => token.name === name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ 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>;
type ServingRuntimeSizeExpandedFieldProps<D extends CreatingModelServingObjectCommon> = {
data: D;
setData: UpdateObjectAtPropAndValue<D>;
};

type ResourceKeys = keyof ContainerResources;

const ServingRuntimeSizeExpandedField: React.FC<ServingRuntimeSizeExpandedFieldProps> = ({
const ServingRuntimeSizeExpandedField = <D extends CreatingModelServingObjectCommon>({
data,
setData,
}) => {
}: ServingRuntimeSizeExpandedFieldProps<D>): React.ReactNode => {
const handleChange = (
type: ContainerResourceAttributes.CPU | ContainerResourceAttributes.MEMORY,
variant: ResourceKeys,
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 @@ -17,11 +16,9 @@ import { AcceleratorProfileState } from '~/utilities/useAcceleratorProfileState'
import SimpleSelect from '~/components/SimpleSelect';
import ServingRuntimeSizeExpandedField from './ServingRuntimeSizeExpandedField';

type ServingRuntimeSizeSectionProps = {
data: CreatingServingRuntimeObject | CreatingInferenceServiceObject;
setData:
| UpdateObjectAtPropAndValue<CreatingServingRuntimeObject>
| UpdateObjectAtPropAndValue<CreatingInferenceServiceObject>;
type ServingRuntimeSizeSectionProps<D extends CreatingModelServingObjectCommon> = {
data: D;
setData: UpdateObjectAtPropAndValue<D>;
sizes: ModelServingSize[];
servingRuntimeSelected?: ServingRuntimeKind;
acceleratorProfileState: AcceleratorProfileState;
Expand All @@ -30,7 +27,7 @@ type ServingRuntimeSizeSectionProps = {
infoContent?: string;
};

const ServingRuntimeSizeSection: React.FC<ServingRuntimeSizeSectionProps> = ({
const ServingRuntimeSizeSection = <D extends CreatingModelServingObjectCommon>({
data,
setData,
sizes,
Expand All @@ -39,7 +36,7 @@ const ServingRuntimeSizeSection: React.FC<ServingRuntimeSizeSectionProps> = ({
selectedAcceleratorProfile,
setSelectedAcceleratorProfile,
infoContent,
}) => {
}: ServingRuntimeSizeSectionProps<D>): React.ReactNode => {
const [supportedAcceleratorProfiles, setSupportedAcceleratorProfiles] = React.useState<
string[] | undefined
>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ 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>;
type ServingRuntimeTokenInputProps<D extends CreatingModelServingObjectCommon> = {
data: D;
setData: UpdateObjectAtPropAndValue<D>;
token: ServingRuntimeToken;
disabled?: boolean;
};

const ServingRuntimeTokenInput: React.FC<ServingRuntimeTokenInputProps> = ({
const ServingRuntimeTokenInput = <D extends CreatingModelServingObjectCommon>({
data,
setData,
token,
disabled,
}) => {
}: ServingRuntimeTokenInputProps<D>): React.ReactNode => {
const checkDuplicates = (name: string): boolean => {
const duplicates = data.tokens.filter(
(currentToken) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ 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>;
type ServingRuntimeTokenSectionProps<D extends CreatingModelServingObjectCommon> = {
data: D;
setData: UpdateObjectAtPropAndValue<D>;
allowCreate: boolean;
createNewToken: () => void;
};

const ServingRuntimeTokenSection: React.FC<ServingRuntimeTokenSectionProps> = ({
const ServingRuntimeTokenSection = <D extends CreatingModelServingObjectCommon>({
data,
setData,
allowCreate,
createNewToken,
}) => (
}: ServingRuntimeTokenSectionProps<D>): React.ReactNode => (
<FormGroup
label="Token authentication"
data-testid="auth-section"
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
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const ManageStorageModal: React.FC<AddStorageModalProps> = ({ existingData, isOp
<StackItem>
<CreateNewStorageSection
data={createData}
setData={(key, value) => setCreateData(key, value)}
setData={setCreateData}
currentSize={existingData?.status?.capacity?.storage}
autoFocusName
disableStorageClassSelect={!!existingData}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import NameDescriptionField from '~/concepts/k8s/NameDescriptionField';
import { SupportedArea, useIsAreaAvailable } from '~/concepts/areas';
import StorageClassSelect from './StorageClassSelect';

type CreateNewStorageSectionProps = {
data: CreatingStorageObject;
setData: UpdateObjectAtPropAndValue<CreatingStorageObject>;
type CreateNewStorageSectionProps<D extends CreatingStorageObject> = {
data: D;
setData: UpdateObjectAtPropAndValue<D>;
currentSize?: string;
autoFocusName?: boolean;
menuAppendTo?: HTMLElement;
disableStorageClassSelect?: boolean;
};

const CreateNewStorageSection: React.FC<CreateNewStorageSectionProps> = ({
const CreateNewStorageSection = <D extends CreatingStorageObject>({
data,
setData,
currentSize,
menuAppendTo,
autoFocusName,
disableStorageClassSelect,
}) => {
}: CreateNewStorageSectionProps<D>): React.ReactNode => {
const isStorageClassesAvailable = useIsAreaAvailable(SupportedArea.STORAGE_CLASSES).status;

return (
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: T[K],
) => void;

export type NameDescType = {
name: string;
Expand Down
Loading

0 comments on commit bea9b86

Please sign in to comment.