import React, { useCallback, useEffect, useRef } from 'react'; import { Grid, Box, Card, Flex, Button, FormControl, Input, Textarea } from '@chakra-ui/react'; import type { ModelType } from '@/types/model'; import { useForm } from 'react-hook-form'; import { useToast } from '@/hooks/useToast'; import { putModelById } from '@/api/model'; import { useScreen } from '@/hooks/useScreen'; import { useGlobalStore } from '@/store/global'; const ModelEditForm = ({ model }: { model?: ModelType }) => { const isInit = useRef(false); const { register, handleSubmit, reset, formState: { errors } } = useForm(); const { setLoading } = useGlobalStore(); const { toast } = useToast(); const { media } = useScreen(); const onclickSave = useCallback( async (data: ModelType) => { setLoading(true); try { await putModelById(data._id, { name: data.name, systemPrompt: data.systemPrompt, service: data.service, security: data.security }); toast({ title: '更新成功', status: 'success' }); } catch (err) { console.error(err); toast({ title: err as string, status: 'success' }); } setLoading(false); }, [setLoading, toast] ); const submitError = useCallback(() => { // deep search message const deepSearch = (obj: any): string => { if (!obj) return '提交表单错误'; if (!!obj.message) { return obj.message; } return deepSearch(Object.values(obj)[0]); }; toast({ title: deepSearch(errors), status: 'error', duration: 4000, isClosable: true }); }, [errors, toast]); /* model 只会改变一次 */ useEffect(() => { if (model && !isInit.current) { reset(model); isInit.current = true; } }, [model, reset]); return ( 修改模型信息 展示名称: 对话模型: {model?.service.modelName}