'use client' import type { MouseEventHandler } from 'react' import { useCallback, useRef, useState } from 'react' import { RiCloseLine } from '@remixicon/react' import { useTranslation } from 'react-i18next' import cn from '@/utils/classnames' import Button from '@/app/components/base/button' import Input from '@/app/components/base/input' import Textarea from '@/app/components/base/textarea' import Modal from '@/app/components/base/modal' import type { DataSet } from '@/models/datasets' import { updateDatasetSetting } from '@/service/datasets' import { noop } from 'lodash-es' import AppIcon from '../../base/app-icon' import type { AppIconSelection } from '../../base/app-icon-picker' import AppIconPicker from '../../base/app-icon-picker' import Toast from '../../base/toast' type RenameDatasetModalProps = { show: boolean dataset: DataSet onSuccess?: () => void onClose: () => void } const RenameDatasetModal = ({ show, dataset, onSuccess, onClose }: RenameDatasetModalProps) => { const { t } = useTranslation() const [loading, setLoading] = useState(false) const [name, setName] = useState(dataset.name) const [description, setDescription] = useState(dataset.description) const [externalKnowledgeId] = useState(dataset.external_knowledge_info.external_knowledge_id) const [externalKnowledgeApiId] = useState(dataset.external_knowledge_info.external_knowledge_api_id) const [appIcon, setAppIcon] = useState( dataset.icon_info?.icon_type === 'image' ? { type: 'image' as const, url: dataset.icon_info?.icon_url || '', fileId: dataset.icon_info?.icon || '' } : { type: 'emoji' as const, icon: dataset.icon_info?.icon || '', background: dataset.icon_info?.icon_background || '' }, ) const [showAppIconPicker, setShowAppIconPicker] = useState(false) const previousAppIcon = useRef( dataset.icon_info?.icon_type === 'image' ? { type: 'image' as const, url: dataset.icon_info?.icon_url || '', fileId: dataset.icon_info?.icon || '' } : { type: 'emoji' as const, icon: dataset.icon_info?.icon || '', background: dataset.icon_info?.icon_background || '' }, ) const handleOpenAppIconPicker = useCallback(() => { setShowAppIconPicker(true) previousAppIcon.current = appIcon }, [appIcon]) const handleSelectAppIcon = useCallback((icon: AppIconSelection) => { setAppIcon(icon) setShowAppIconPicker(false) }, []) const handleCloseAppIconPicker = useCallback(() => { setAppIcon(previousAppIcon.current) setShowAppIconPicker(false) }, []) const onConfirm: MouseEventHandler = useCallback(async () => { if (!name.trim()) { Toast.notify({ type: 'error', message: t('datasetSettings.form.nameError') }) return } try { setLoading(true) const body: Partial & { external_knowledge_id?: string; external_knowledge_api_id?: string } = { name, description, icon_info: { icon: appIcon.type === 'image' ? appIcon.fileId : appIcon.icon, icon_type: appIcon.type, icon_background: appIcon.type === 'image' ? undefined : appIcon.background, icon_url: appIcon.type === 'image' ? appIcon.url : undefined, }, } if (externalKnowledgeId && externalKnowledgeApiId) { body.external_knowledge_id = externalKnowledgeId body.external_knowledge_api_id = externalKnowledgeApiId } await updateDatasetSetting({ datasetId: dataset.id, body, }) Toast.notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) if (onSuccess) onSuccess() onClose() } catch { Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) } finally { setLoading(false) } }, [appIcon, description, dataset.id, externalKnowledgeApiId, externalKnowledgeId, name, onClose, onSuccess, t]) return (
{t('datasetSettings.title')}
{t('datasetSettings.form.name')}
setName(e.target.value)} className='h-9 grow' placeholder={t('datasetSettings.form.namePlaceholder') || ''} />
{t('datasetSettings.form.desc')}