2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
175 lines
4.6 KiB
React
175 lines
4.6 KiB
React
import {
|
|
Lock as ActionLock,
|
|
ChevronDown as ActionChevronDown,
|
|
} from '@/components/common/icons/ActionIcons';
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { Popover, SideSheet } from '@/components/ui';
|
|
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 = (
|
|
<ModelPickerPanel
|
|
kind={kind}
|
|
models={models}
|
|
value={value}
|
|
params={params}
|
|
mode={mode}
|
|
recent={recent}
|
|
loading={loading}
|
|
error={error}
|
|
onRetry={onRetry}
|
|
onSelect={handleSelect}
|
|
onClose={close}
|
|
compact={isMobile}
|
|
compare={compare}
|
|
/>
|
|
);
|
|
|
|
const label = comparing
|
|
? t('对比 {{n}} 个模型', { n: compare.names.length })
|
|
: selected
|
|
? displayNameOf(selected)
|
|
: loading
|
|
? t('模型列表加载中')
|
|
: t('选择模型');
|
|
|
|
const trigger = (
|
|
<button
|
|
ref={triggerRef}
|
|
type='button'
|
|
className={[
|
|
'app-action-button',
|
|
`mp-trigger${comparing ? ' is-compare' : ''} ${className}`,
|
|
]
|
|
.filter(Boolean)
|
|
.join(' ')}
|
|
aria-haspopup='listbox'
|
|
aria-expanded={open}
|
|
aria-label={t('选择模型:{{name}}', { name: label })}
|
|
disabled={disabled}
|
|
onClick={() => setOpen((v) => !v)}
|
|
>
|
|
{selected?.level_locked ? <ActionLock size={13} aria-hidden /> : null}
|
|
<span className='mp-trigger-name'>{label}</span>
|
|
<ActionChevronDown size={14} aria-hidden className='mp-trigger-caret' />
|
|
</button>
|
|
);
|
|
|
|
if (isMobile) {
|
|
return (
|
|
<>
|
|
{trigger}
|
|
<SideSheet
|
|
visible={open}
|
|
placement='bottom'
|
|
height={sheetHeight(80)}
|
|
title={t('选择模型')}
|
|
onCancel={close}
|
|
bodyStyle={{
|
|
padding: 0,
|
|
paddingBottom: 'env(safe-area-inset-bottom, 0px)',
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
}}
|
|
className='mp-sheet'
|
|
closeOnEsc
|
|
>
|
|
{open ? panel : null}
|
|
</SideSheet>
|
|
</>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Popover
|
|
open={open}
|
|
trigger='click'
|
|
position='topLeft'
|
|
showArrow={false}
|
|
onOpenChange={(next) => {
|
|
if (!next) setOpen(false);
|
|
}}
|
|
content={open ? panel : null}
|
|
classNames={{ body: 'mp-popover' }}
|
|
>
|
|
{trigger}
|
|
</Popover>
|
|
);
|
|
};
|
|
|
|
export default ModelPicker;
|