import { useState, useRef, useEffect } from 'react'; import { Table, Button, Modal, message } from 'antd'; import { invoke } from '@tauri-apps/api'; import useChatModel from '@/hooks/useChatModel'; import useColumns from '@/hooks/useColumns'; import useData from '@/hooks/useData'; import { chatModelPath } from '@/utils'; import { modelColumns } from './config'; import LanguageModelForm from './Form'; import './index.scss'; export default function LanguageModel() { const [isVisible, setVisible] = useState(false); const [modelPath, setChatModelPath] = useState(''); const { modelData, modelSet } = useChatModel(); const { opData, opAdd, opRemove, opReplace, opSafeKey } = useData(modelData); const { columns, ...opInfo } = useColumns(modelColumns()); const formRef = useRef(null); useEffect(() => { if (!opInfo.opType) return; if (['edit', 'new'].includes(opInfo.opType)) { setVisible(true); } if (['delete'].includes(opInfo.opType)) { const data = opRemove(opInfo?.opRecord?.[opSafeKey]); modelSet(data); opInfo.resetRecord(); } }, [opInfo.opType, formRef]); const hide = () => { setVisible(false); opInfo.resetRecord(); }; const handleOk = () => { formRef.current?.form?.validateFields() .then((vals: Record) => { if (modelData.map((i: any) => i.cmd).includes(vals.cmd) && opInfo?.opRecord?.cmd !== vals.cmd) { message.warning(`"cmd: /${vals.cmd}" already exists, please change the "${vals.cmd}" name and resubmit.`); return; } let data = []; switch (opInfo.opType) { case 'new': data = opAdd(vals); break; case 'edit': data = opReplace(opInfo?.opRecord?.[opSafeKey], vals); break; default: break; } modelSet(data) hide(); }) }; const handleOpenFile = async () => { const path = await chatModelPath(); setChatModelPath(path); invoke('open_file', { path }); }; const modalTitle = `${({ new: 'Create', edit: 'Edit' })[opInfo.opType]} Language Model`; return (
PATH: {modelPath}
Total {total} items, }} /> ) }