fix: revert backend selection
This commit is contained in:
+1
-1
@@ -51,7 +51,7 @@ export interface BackGroupOption {
|
|||||||
label: string;
|
label: string;
|
||||||
value: string;
|
value: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
children: BackendOption[];
|
options: BackendOption[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const backendOptionsAtom = atom<BackGroupOption[]>([]);
|
export const backendOptionsAtom = atom<BackGroupOption[]>([]);
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ interface FormContextProps {
|
|||||||
label: string;
|
label: string;
|
||||||
value: string;
|
value: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
children: BackendOption[];
|
options: BackendOption[];
|
||||||
}[];
|
}[];
|
||||||
flatBackendOptions: BackendOption[];
|
flatBackendOptions: BackendOption[];
|
||||||
initialValues?: FormData; // for editing model
|
initialValues?: FormData; // for editing model
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
import SealCascader from '@/components/seal-form/seal-cascader';
|
|
||||||
import SealInput from '@/components/seal-form/seal-input';
|
|
||||||
import SealSelect from '@/components/seal-form/seal-select';
|
import SealSelect from '@/components/seal-form/seal-select';
|
||||||
import TooltipList from '@/components/tooltip-list';
|
import TooltipList from '@/components/tooltip-list';
|
||||||
import useAppUtils from '@/hooks/use-app-utils';
|
import useAppUtils from '@/hooks/use-app-utils';
|
||||||
import { BackendSourceValueMap } from '@/pages/backends/config';
|
|
||||||
import { CaretDownOutlined, InfoCircleOutlined } from '@ant-design/icons';
|
import { CaretDownOutlined, InfoCircleOutlined } from '@ant-design/icons';
|
||||||
import { useIntl, useNavigate } from '@umijs/max';
|
import { useIntl, useNavigate } from '@umijs/max';
|
||||||
import { Form, Select } from 'antd';
|
import { Form, Select } from 'antd';
|
||||||
import React, { useEffect, useMemo } from 'react';
|
import React, { useMemo } from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import { backendTipsList } from '../config';
|
import { backendTipsList } from '../config';
|
||||||
import { backendOptionsMap } from '../config/backend-parameters';
|
import { backendOptionsMap } from '../config/backend-parameters';
|
||||||
@@ -31,13 +28,8 @@ const BackendFields: React.FC = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { getRuleMessage } = useAppUtils();
|
const { getRuleMessage } = useAppUtils();
|
||||||
const form = Form.useFormInstance();
|
const form = Form.useFormInstance();
|
||||||
const {
|
const { action, onValuesChange, backendOptions, onBackendChange } =
|
||||||
action,
|
useFormContext();
|
||||||
onValuesChange,
|
|
||||||
backendOptions,
|
|
||||||
flatBackendOptions,
|
|
||||||
onBackendChange
|
|
||||||
} = useFormContext();
|
|
||||||
const backend = Form.useWatch('backend', form);
|
const backend = Form.useWatch('backend', form);
|
||||||
const [showDeprecated, setShowDeprecated] = React.useState<boolean>(false);
|
const [showDeprecated, setShowDeprecated] = React.useState<boolean>(false);
|
||||||
const [selectedBackend, setSelectedBackend] =
|
const [selectedBackend, setSelectedBackend] =
|
||||||
@@ -51,22 +43,6 @@ const BackendFields: React.FC = () => {
|
|||||||
onValuesChange?.({}, form.getFieldsValue());
|
onValuesChange?.({}, form.getFieldsValue());
|
||||||
};
|
};
|
||||||
|
|
||||||
const backendHelperText = useMemo(() => {
|
|
||||||
const selected = flatBackendOptions?.find((item) => item.value === backend);
|
|
||||||
if (
|
|
||||||
selected &&
|
|
||||||
!selected.enabled &&
|
|
||||||
selected.backend_source === BackendSourceValueMap.COMMUNITY
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
<span style={{ color: 'var(--ant-color-error)' }}>
|
|
||||||
{intl.formatMessage({ id: 'models.form.backend.helperText' })}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}, [backend, flatBackendOptions, intl]);
|
|
||||||
|
|
||||||
const backendVersions = useMemo((): {
|
const backendVersions = useMemo((): {
|
||||||
builtIn: any[];
|
builtIn: any[];
|
||||||
custom: any[];
|
custom: any[];
|
||||||
@@ -137,30 +113,16 @@ const BackendFields: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleOnBackendChange = (value: any[], option: any[]) => {
|
const handleOnBackendChange = (value: any, option: any) => {
|
||||||
const selectedBackend = value?.[1];
|
console.log('Selected backend value:', value, option);
|
||||||
const selectedOption = option?.[1] || {};
|
|
||||||
|
|
||||||
form.setFieldsValue({
|
form.setFieldsValue({
|
||||||
backend: selectedBackend
|
backend: value
|
||||||
});
|
});
|
||||||
form.setFieldValue('env', {
|
form.setFieldValue('env', {
|
||||||
...(selectedOption.default_env || {})
|
...(option.default_env || {})
|
||||||
});
|
});
|
||||||
onBackendChange?.(selectedBackend, selectedOption);
|
onBackendChange?.(value, option);
|
||||||
setSelectedBackend(selectedOption);
|
setSelectedBackend(option);
|
||||||
};
|
|
||||||
|
|
||||||
const displayRender = (labels: any[], selectedOptions?: any[]) => {
|
|
||||||
const groupTitle = selectedOptions?.[0]?.title;
|
|
||||||
if (!groupTitle) {
|
|
||||||
return <span>{labels?.[0]}</span>;
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<span className="flex-center">
|
|
||||||
{intl.formatMessage({ id: groupTitle })} / {labels?.[1]}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderDeprecatedVersionOptions = (values: any[]) => {
|
const renderDeprecatedVersionOptions = (values: any[]) => {
|
||||||
@@ -192,73 +154,35 @@ const BackendFields: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const BackendNode = (props: any) => {
|
const optionRender = (option: any) => {
|
||||||
const { data: backend } = props;
|
return option.data.title;
|
||||||
return backend.isLeaf ? (
|
|
||||||
<span className="flex-center">
|
|
||||||
{backend.label}
|
|
||||||
{backend.backend_source === BackendSourceValueMap.COMMUNITY &&
|
|
||||||
!backend.enabled && (
|
|
||||||
<span className="text-tertiary m-l-4">
|
|
||||||
[{intl.formatMessage({ id: 'common.status.disabled' })}]
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</span>
|
|
||||||
) : (
|
|
||||||
<span>{intl.formatMessage({ id: backend.title })}</span>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
const labelRender = (option: any) => {
|
||||||
if (backend) {
|
return option.title;
|
||||||
const selected = flatBackendOptions?.find(
|
};
|
||||||
(item) => item.value === backend
|
|
||||||
);
|
|
||||||
if (selected) {
|
|
||||||
form.setFieldValue('backend_selection', [
|
|
||||||
selected.backend_source,
|
|
||||||
backend
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
// TODO: init env variables from selected backend, when action is CREATE
|
|
||||||
}
|
|
||||||
}, [backend, flatBackendOptions]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form.Item name="backend" hidden>
|
|
||||||
<SealInput.Input></SealInput.Input>
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item
|
<Form.Item
|
||||||
name="backend_selection"
|
name="backend"
|
||||||
rules={[
|
rules={[
|
||||||
{
|
{
|
||||||
required: true,
|
required: true,
|
||||||
message: getRuleMessage('select', 'models.form.backend')
|
message: getRuleMessage('select', 'models.form.backend')
|
||||||
}
|
}
|
||||||
]}
|
]}
|
||||||
help={backendHelperText}
|
|
||||||
>
|
>
|
||||||
<SealCascader
|
<SealSelect
|
||||||
required
|
required
|
||||||
showSearch
|
showSearch
|
||||||
allowClear={false}
|
|
||||||
changeOnSelect={false}
|
|
||||||
expandTrigger="hover"
|
|
||||||
multiple={false}
|
|
||||||
classNames={{
|
|
||||||
popup: {
|
|
||||||
root: 'cascader-popup-wrapper gpu-selector'
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
maxTagCount={1}
|
|
||||||
label={intl.formatMessage({ id: 'models.form.backend' })}
|
|
||||||
options={backendOptions}
|
|
||||||
getPopupContainer={(triggerNode) => triggerNode.parentNode}
|
|
||||||
optionNode={BackendNode}
|
|
||||||
displayRender={displayRender}
|
|
||||||
onChange={handleOnBackendChange}
|
onChange={handleOnBackendChange}
|
||||||
></SealCascader>
|
label={intl.formatMessage({ id: 'models.form.backend' })}
|
||||||
|
description={<TooltipList list={backendTipsList}></TooltipList>}
|
||||||
|
options={backendOptions}
|
||||||
|
optionRender={optionRender}
|
||||||
|
labelRender={labelRender}
|
||||||
|
></SealSelect>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
{backendOptionsMap.custom !== backend && (
|
{backendOptionsMap.custom !== backend && (
|
||||||
<Form.Item name="backend_version">
|
<Form.Item name="backend_version">
|
||||||
|
|||||||
@@ -16,33 +16,9 @@ interface BackendGroup {
|
|||||||
value: string;
|
value: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
isLeaf?: boolean;
|
isLeaf?: boolean;
|
||||||
children: BackendOption[];
|
options: BackendOption[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupByBackendSource = (list: BackendOption[]): BackendGroup[] => {
|
|
||||||
const map = list.reduce<Record<string, BackendOption[]>>((acc, item) => {
|
|
||||||
const key = item.backend_source;
|
|
||||||
|
|
||||||
if (!acc[key]) {
|
|
||||||
acc[key] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
acc[key].push(item);
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
return Object.entries(map).map(([backend_source, backends]) => ({
|
|
||||||
label: backend_source,
|
|
||||||
value: backend_source,
|
|
||||||
title:
|
|
||||||
backend_source === BackendSourceValueMap.CUSTOM
|
|
||||||
? BackendSourceLabelMap[BackendSourceValueMap.USER_DEFINED]
|
|
||||||
: BackendSourceLabelMap[backend_source],
|
|
||||||
isLeaf: false,
|
|
||||||
children: backends
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function useQueryBackends() {
|
export default function useQueryBackends() {
|
||||||
const [backendOptions, setBackendOptions] = useAtom(backendOptionsAtom);
|
const [backendOptions, setBackendOptions] = useAtom(backendOptionsAtom);
|
||||||
const [flatBackendOptions, setFlatBackendOptions] = useState<BackendOption[]>(
|
const [flatBackendOptions, setFlatBackendOptions] = useState<BackendOption[]>(
|
||||||
@@ -50,6 +26,32 @@ export default function useQueryBackends() {
|
|||||||
);
|
);
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
|
const groupByBackendSource = (list: BackendOption[]): BackendGroup[] => {
|
||||||
|
const map = list.reduce<Record<string, BackendOption[]>>((acc, item) => {
|
||||||
|
const key = item.backend_source;
|
||||||
|
|
||||||
|
if (!acc[key]) {
|
||||||
|
acc[key] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
acc[key].push(item);
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return Object.entries(map).map(([backend_source, backends]) => {
|
||||||
|
const title =
|
||||||
|
backend_source === BackendSourceValueMap.CUSTOM
|
||||||
|
? BackendSourceLabelMap[BackendSourceValueMap.USER_DEFINED]
|
||||||
|
: BackendSourceLabelMap[backend_source];
|
||||||
|
return {
|
||||||
|
value: backend_source,
|
||||||
|
label: title ? intl.formatMessage({ id: title }) : backend_source,
|
||||||
|
isLeaf: false,
|
||||||
|
options: backends
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const getBackendOptions = async (params?: { cluster_id: number }) => {
|
const getBackendOptions = async (params?: { cluster_id: number }) => {
|
||||||
try {
|
try {
|
||||||
const res = await queryBackendList(params);
|
const res = await queryBackendList(params);
|
||||||
|
|||||||
Reference in New Issue
Block a user