mirror of https://github.com/msgbyte/tailchat
refactor: 调整创建群面板的代码,拆分部分逻辑
parent
25beff5526
commit
0b83fa0456
@ -0,0 +1,57 @@
|
|||||||
|
import { findPluginPanelInfoByName } from '@/utils/plugin-helper';
|
||||||
|
import React, { useState } from 'react';
|
||||||
|
import {
|
||||||
|
GroupPanelType,
|
||||||
|
t,
|
||||||
|
useAsyncRequest,
|
||||||
|
createGroupPanel,
|
||||||
|
createFastFormSchema,
|
||||||
|
fieldSchema,
|
||||||
|
showToasts,
|
||||||
|
} from 'tailchat-shared';
|
||||||
|
import { ModalWrapper } from '../../Modal';
|
||||||
|
import { WebFastForm } from '../../WebFastForm';
|
||||||
|
import { buildDataFromValues } from './helper';
|
||||||
|
import type { GroupPanelValues } from './types';
|
||||||
|
import { useGroupPanelFields } from './useGroupPanelFields';
|
||||||
|
|
||||||
|
const schema = createFastFormSchema({
|
||||||
|
name: fieldSchema
|
||||||
|
.string()
|
||||||
|
.required(t('面板名不能为空'))
|
||||||
|
.max(20, t('面板名过长')),
|
||||||
|
type: fieldSchema.string().required(t('面板类型不能为空')),
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建群组面板
|
||||||
|
*/
|
||||||
|
export const ModalCreateGroupPanel: React.FC<{
|
||||||
|
groupId: string;
|
||||||
|
onCreateSuccess: () => void;
|
||||||
|
}> = React.memo((props) => {
|
||||||
|
const [currentValues, setValues] = useState<Partial<GroupPanelValues>>({});
|
||||||
|
|
||||||
|
const [, handleSubmit] = useAsyncRequest(
|
||||||
|
async (values: GroupPanelValues) => {
|
||||||
|
await createGroupPanel(props.groupId, buildDataFromValues(values));
|
||||||
|
showToasts(t('创建成功'), 'success');
|
||||||
|
props.onCreateSuccess();
|
||||||
|
},
|
||||||
|
[props.groupId, props.onCreateSuccess]
|
||||||
|
);
|
||||||
|
|
||||||
|
const fields = useGroupPanelFields(props.groupId, currentValues);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ModalWrapper title={t('创建群组面板')} style={{ maxWidth: 440 }}>
|
||||||
|
<WebFastForm
|
||||||
|
schema={schema}
|
||||||
|
fields={fields}
|
||||||
|
onChange={setValues}
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
/>
|
||||||
|
</ModalWrapper>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
ModalCreateGroupPanel.displayName = 'ModalCreateGroupPanel';
|
@ -0,0 +1,34 @@
|
|||||||
|
import { findPluginPanelInfoByName } from '@/utils/plugin-helper';
|
||||||
|
import { GroupPanelType } from 'tailchat-shared';
|
||||||
|
import type { GroupPanelValues } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据表单数据生成需要提交的内容
|
||||||
|
*/
|
||||||
|
export function buildDataFromValues(values: GroupPanelValues) {
|
||||||
|
const { name, type, ...meta } = values;
|
||||||
|
let panelType: number;
|
||||||
|
let provider: string | undefined = undefined;
|
||||||
|
let pluginPanelName: string | undefined = undefined;
|
||||||
|
|
||||||
|
if (typeof type === 'string') {
|
||||||
|
// 创建一个来自插件的面板
|
||||||
|
const panelName = type;
|
||||||
|
panelType = GroupPanelType.PLUGIN;
|
||||||
|
const pluginPanelInfo = findPluginPanelInfoByName(panelName);
|
||||||
|
if (pluginPanelInfo) {
|
||||||
|
provider = pluginPanelInfo.provider;
|
||||||
|
pluginPanelName = pluginPanelInfo.name;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panelType = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
type: panelType,
|
||||||
|
provider,
|
||||||
|
pluginPanelName,
|
||||||
|
meta,
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
import type { GroupPanelType } from 'tailchat-shared';
|
||||||
|
|
||||||
|
export interface GroupPanelValues {
|
||||||
|
name: string;
|
||||||
|
type: string | GroupPanelType;
|
||||||
|
[key: string]: unknown;
|
||||||
|
}
|
Loading…
Reference in New Issue