cherry-studio/src/renderer/src/pages/settings/ProviderSettings/AddModelPopup.tsx
Asurada 4327e60d00 feat: Update model label format to include provider information (#2793)
* feat: Update model label format to include provider information

* feat: Improve AddModelPopup layout and button styling
2025-03-04 15:36:07 +08:00

157 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TopView } from '@renderer/components/TopView'
import { useProvider } from '@renderer/hooks/useProvider'
import { Model, Provider } from '@renderer/types'
import { getDefaultGroupName } from '@renderer/utils'
import { Button, Flex, Form, FormProps, Input, Modal } from 'antd'
import { find } from 'lodash'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
interface ShowParams {
title: string
provider: Provider
}
interface Props extends ShowParams {
resolve: (data: any) => void
}
type FieldType = {
provider: string
id: string
name?: string
group?: string
}
const PopupContainer: React.FC<Props> = ({ title, provider, resolve }) => {
const [open, setOpen] = useState(true)
const [form] = Form.useForm()
const { addModel, models } = useProvider(provider.id)
const { t } = useTranslation()
const onOk = () => {
setOpen(false)
}
const onCancel = () => {
setOpen(false)
}
const onClose = () => {
resolve({})
}
const onAddModel = (values: FieldType) => {
const id = values.id.trim()
if (find(models, { id })) {
window.message.error(t('error.model.exists'))
return
}
const model: Model = {
id,
provider: provider.id,
name: values.name ? values.name : id.toUpperCase(),
group: getDefaultGroupName(values.group || id)
}
addModel(model)
return true
}
const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
const id = values.id.trim().replaceAll('', ',')
if (id.includes(',')) {
const ids = id.split(',')
ids.forEach((id) => onAddModel({ id, name: id } as FieldType))
resolve({})
return
}
if (onAddModel(values)) {
resolve({})
}
}
return (
<Modal
title={title}
open={open}
onOk={onOk}
onCancel={onCancel}
maskClosable={false}
afterClose={onClose}
footer={null}
centered>
<Form
form={form}
labelCol={{ flex: '110px' }}
labelAlign="left"
colon={false}
style={{ marginTop: 25 }}
onFinish={onFinish}>
<Form.Item
name="id"
label={t('settings.models.add.model_id')}
tooltip={t('settings.models.add.model_id.tooltip')}
rules={[{ required: true }]}>
<Input
placeholder={t('settings.models.add.model_id.placeholder')}
spellCheck={false}
maxLength={200}
onChange={(e) => {
form.setFieldValue('name', e.target.value)
form.setFieldValue('group', getDefaultGroupName(e.target.value))
}}
/>
</Form.Item>
<Form.Item
name="name"
label={t('settings.models.add.model_name')}
tooltip={t('settings.models.add.model_name.placeholder')}>
<Input placeholder={t('settings.models.add.model_name.placeholder')} spellCheck={false} />
</Form.Item>
<Form.Item
name="group"
label={t('settings.models.add.group_name')}
tooltip={t('settings.models.add.group_name.tooltip')}>
<Input placeholder={t('settings.models.add.group_name.placeholder')} spellCheck={false} />
</Form.Item>
<Form.Item style={{ marginBottom: 15, textAlign: 'center' }}>
<Flex justify="center" align="center" style={{ position: 'relative' }}>
<div>
<Button type="primary" htmlType="submit" size="middle">
{t('settings.models.add.add_model')}
</Button>
</div>
</Flex>
</Form.Item>
</Form>
</Modal>
)
}
export default class AddModelPopup {
static topviewId = 0
static hide() {
TopView.hide('AddModelPopup')
}
static show(props: ShowParams) {
return new Promise<any>((resolve) => {
TopView.show(
<PopupContainer
{...props}
resolve={(v) => {
resolve(v)
this.hide()
}}
/>,
'AddModelPopup'
)
})
}
}