/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useMemo } from 'react';
import { Select, Tooltip } from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';
import { optionKey, optionValue } from './constraints';
import { optionLabel, paramLabel } from './labels';
import './creation-params.css';
/**
* 下拉选择(schema type = 'select'),也用于运镜预设:
* options 可为原始值,或 { value, label, description } 对象(description 作为说明文字显示)。
* 被约束禁用的选项置灰,并以说明行展示原因(下拉内不依赖 Tooltip,键盘用户同样可见)。
*
* props: { param, value, onChange(value), optionDisabledReasons?, disabledReason? }
*/
const ParamSelect = ({
param,
value,
onChange,
optionDisabledReasons = {},
disabledReason,
}) => {
const { t } = useTranslation();
const options = useMemo(() => param?.options || [], [param]);
const optionList = useMemo(
() =>
options.map((opt) => {
const reason = optionDisabledReasons[optionKey(opt)] || '';
const desc =
opt && typeof opt === 'object' && opt.description
? t(opt.description)
: '';
const text = optionLabel(t, param, opt);
return {
value: optionValue(opt),
disabled: Boolean(reason),
showTick: true,
label: (
{text}
{reason || desc ? (
{reason || desc}
) : null}
),
};
}),
[options, optionDisabledReasons, param, t],
);
// 选项可能是数字,取值可能是字符串:映射到选项的规范值,保证 Select 能命中
const hit = options.find((o) => optionKey(o) === optionKey(value));
const current = hit !== undefined ? optionValue(hit) : undefined;
const sel = (