/*
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, { useCallback, useEffect, useRef, useState } from 'react';
import { Popover, SideSheet } from '@douyinfe/semi-ui';
import { ChevronDown, Lock } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { useIsMobile } from '../../../hooks/common/useIsMobile';
import ModelPickerPanel from './ModelPickerPanel';
import { displayNameOf, loadRecent, pushRecent } from './modelPickerUtils';
import { sheetHeight } from '../viewport';
import './model-picker.css';
const getStorage = () => {
try {
return window.localStorage;
} catch (e) {
return null;
}
};
/**
* 模型选择器(WP B-4):触发 chip + 桌面弹层 / 移动端(< md)底部抽屉。
*
* props:
* kind 'image' | 'video'
* models schema.models[]
* value 当前模型名
* onChange(model) 选中可调用的模型(等级锁定的模型不会回调,改为显示升级入口)
* params 当前参数取值(用于显示对应档位单价)
* mode 当前模式(用于判定不兼容模型)
* loading / error / onRetry schema 加载状态
* disabled
* compare 对比生成(WP C-2,仅图像):{ active, names, blockOf(model), toggleModel(model),
* setEnabled(on) }。开启后卡片变为多选(2~4 个),点击勾选而不关闭弹层
*/
const ModelPicker = ({
kind = 'video',
models = [],
value = '',
onChange,
params = {},
mode = '',
loading = false,
error = null,
onRetry,
disabled = false,
className = '',
compare = null,
}) => {
const { t } = useTranslation();
const isMobile = useIsMobile();
const [open, setOpen] = useState(false);
const [recent, setRecent] = useState(() => loadRecent(getStorage(), kind));
const triggerRef = useRef(null);
useEffect(() => {
setRecent(loadRecent(getStorage(), kind));
}, [kind]);
const selected = models.find((m) => m.model === value) || null;
const close = useCallback(() => {
setOpen(false);
// 关闭后焦点回到触发按钮
requestAnimationFrame(() => triggerRef.current?.focus?.());
}, []);
const comparing = Boolean(compare?.active);
const handleSelect = useCallback(
(model) => {
if (!model || model.level_locked) return;
if (comparing) {
compare.toggleModel(model);
return;
}
setRecent((prev) => pushRecent(getStorage(), kind, model.model, prev));
if (onChange) onChange(model);
close();
},
[kind, onChange, close, comparing, compare],
);
const panel = (
);
const label = comparing
? t('对比 {{n}} 个模型', { n: compare.names.length })
: selected
? displayNameOf(selected)
: loading
? t('模型列表加载中')
: t('选择模型');
const trigger = (
);
if (isMobile) {
return (
<>
{trigger}
{open ? panel : null}
>
);
}
return (
setOpen(false)}
onEscKeyDown={close}
content={open ? panel : null}
contentClassName='mp-popover'
>
{trigger}
);
};
export default ModelPicker;