Skip to content

Commit

Permalink
✨ feat: 角色设置同步
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed May 14, 2024
1 parent c0b225b commit fadef78
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 128 deletions.
3 changes: 1 addition & 2 deletions src/app/role/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Flexbox } from 'react-layout-kit';

import TopBanner from '@/components/TopBanner';
import RoleDisplay from '@/features/AgentViewer/RoleDisplay/index';
import RoleInfo from '@/features/RoleInfo';
import RoleEdit from '@/panels/RolePanel/RoleEdit';

import SideBar from './SideBar';
Expand All @@ -27,7 +26,7 @@ const Role = () => {
</Flexbox>
</Flexbox>
</Flexbox>
<RoleInfo />
{/*<RoleInfo />*/}
</Flexbox>
);
};
Expand Down
20 changes: 7 additions & 13 deletions src/panels/RolePanel/RoleEdit/Info/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Form, Input } from 'antd';
import { createStyles } from 'antd-style';
import classNames from 'classnames';
import { debounce, isEqual } from 'lodash-es';
import React, { useEffect } from 'react';
import { debounce } from 'lodash-es';
import React from 'react';

import { agentListSelectors, useAgentStore } from '@/store/agent';
import { useAgentStore } from '@/store/agent';

import { useSyncSettings } from '../useSyncSetting';

const FormItem = Form.Item;

Expand Down Expand Up @@ -39,17 +41,13 @@ const Info = (props: InfoProps) => {
const { style, className } = props;
const { styles } = useStyles();
const [form] = Form.useForm();
const currentAgent = useAgentStore((s) => agentListSelectors.currentAgentItem(s), isEqual);
const updateAgentConfig = useAgentStore((s) => s.updateAgentConfig);

useEffect(() => {
form.setFieldsValue(currentAgent);
}, [currentAgent, form]);
useSyncSettings(form);

return (
<Form
form={form}
initialValues={currentAgent}
onValuesChange={debounce(updateAgentConfig, 100)}
layout="horizontal"
requiredMark={false}
Expand Down Expand Up @@ -82,11 +80,7 @@ const Info = (props: InfoProps) => {
placeholder="请输入角色与你打招呼时的用语"
/>
</FormItem>
<FormItem
label={'说明'}
name={['meta', 'readme']}
rules={[{ message: '请输入角色说明', required: true }]}
>
<FormItem label={'说明'} name={['meta', 'readme']}>
<Input.TextArea
autoSize={{ maxRows: 15, minRows: 15 }}
placeholder="请输入角色说明"
Expand Down
4 changes: 4 additions & 0 deletions src/panels/RolePanel/RoleEdit/SystemRole/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import React from 'react';

import { agentListSelectors, useAgentStore } from '@/store/agent';

import { useSyncSettings } from '../useSyncSetting';

const FormItem = Form.Item;

interface InfoProps {
Expand Down Expand Up @@ -45,6 +47,8 @@ const Info = (props: InfoProps) => {
const currentAgent = useAgentStore((s) => agentListSelectors.currentAgentItem(s), isEqual);
const updateAgentConfig = useAgentStore((s) => s.updateAgentConfig);

useSyncSettings(form);

return (
<Form
form={form}
Expand Down
7 changes: 3 additions & 4 deletions src/panels/RolePanel/RoleEdit/Tachie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Form } from 'antd';
import { createStyles } from 'antd-style';
import classNames from 'classnames';
import { debounce, isEqual } from 'lodash-es';
import React, { useEffect } from 'react';
import React from 'react';

import HolographicCard from '@/components/HolographicCard';
import { useSyncSettings } from '@/panels/RolePanel/RoleEdit/useSyncSetting';
import { agentListSelectors, useAgentStore } from '@/store/agent';

const FormItem = Form.Item;
Expand Down Expand Up @@ -44,9 +45,7 @@ const Info = (props: InfoProps) => {
const currentAgent = useAgentStore((s) => agentListSelectors.currentAgentItem(s), isEqual);
const updateAgentConfig = useAgentStore((s) => s.updateAgentConfig);

useEffect(() => {
form.setFieldsValue(currentAgent);
}, [currentAgent, form]);
useSyncSettings(form);

return (
<Form
Expand Down
53 changes: 27 additions & 26 deletions src/panels/RolePanel/RoleEdit/Voice/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useRequest } from 'ahooks';
import { Button, Divider, Form, Input, Select, Slider, message } from 'antd';
import { createStyles } from 'antd-style';
import classNames from 'classnames';
import { isEqual } from 'lodash-es';
import React, { useEffect, useRef, useState } from 'react';
import React, { useRef, useState } from 'react';

import { useSyncSettings } from '@/panels/RolePanel/RoleEdit/useSyncSetting';
import { speechApi, voiceListApi } from '@/services/tts';
import { agentListSelectors, useAgentStore } from '@/store/agent';
import { useAgentStore } from '@/store/agent';
import { Voice } from '@/types/tts';

const FormItem = Form.Item;
Expand Down Expand Up @@ -81,13 +81,10 @@ const Config = (props: ConfigProps) => {
const ref = useRef<HTMLAudioElement>(null);
const [form] = Form.useForm();
const [voices, setVoices] = useState<Voice[]>([]);
const currentAgent = useAgentStore((s) => agentListSelectors.currentAgentItem(s), isEqual);
const updateAgentConfig = useAgentStore((s) => s.updateAgentConfig);
const [audioUrl, setAudioUrl] = useState<string | undefined>(undefined);

useEffect(() => {
form.setFieldsValue(currentAgent?.tts);
}, [currentAgent, form]);
useSyncSettings(form);

const { loading, run: speek } = useRequest(speechApi, {
manual: true,
Expand All @@ -112,7 +109,7 @@ const Config = (props: ConfigProps) => {

const { loading: voiceLoading, run: getVoiceList } = useRequest(
() => {
const engine = form.getFieldValue('engine');
const engine = form.getFieldValue(['tts', 'engine']);
return voiceListApi(engine);
},
{
Expand All @@ -124,12 +121,13 @@ const Config = (props: ConfigProps) => {

const getExtraNode = () => {
const samples =
suportedLocales.find((item) => item.value === form.getFieldValue('locale'))?.samples || [];
suportedLocales.find((item) => item.value === form.getFieldValue(['tts', 'locale']))
?.samples || [];
const nodes: React.ReactNode[] = [];

samples.forEach((item, index) => {
nodes.push(
<a href="#" onClick={() => form.setFieldValue('message', item)}>
<a href="#" onClick={() => form.setFieldValue(['tts', 'message'], item)}>
{item}
</a>,
);
Expand All @@ -143,24 +141,23 @@ const Config = (props: ConfigProps) => {
return (
<Form
form={form}
initialValues={currentAgent?.tts}
layout="horizontal"
onFinish={() => {
form.validateFields().then((values) => {
updateAgentConfig({ tts: values });
updateAgentConfig(values);
message.success('保存成功');
});
}}
onValuesChange={(changedValues) => {
if (changedValues.engine) {
form.setFieldsValue({ voice: undefined });
if (changedValues.tts.engine) {
form.setFieldsValue({ tts: { voice: undefined } });
getVoiceList();
}
if (changedValues.locale) {
if (changedValues.tts.locale) {
const sample = suportedLocales.find((item) => item.value === changedValues.locale)
?.samples[1];

form.setFieldsValue({ voice: undefined, message: sample });
form.setFieldsValue({ tts: { voice: undefined, message: sample } });
}
}}
preserve={false}
Expand All @@ -169,9 +166,13 @@ const Config = (props: ConfigProps) => {
<div className={classNames(className, styles.container)} style={style}>
<div className={styles.form}>
<div className={styles.message}>
<FormItem dependencies={['locale']} noStyle>
<FormItem dependencies={[['tts', 'locale']]} noStyle>
{() => (
<FormItem extra={getExtraNode()} name="message" style={{ marginBottom: 0 }}>
<FormItem
extra={getExtraNode()}
name={['tts', 'message']}
style={{ marginBottom: 0 }}
>
<Input.TextArea
autoSize={{ maxRows: 28, minRows: 28 }}
maxLength={800}
Expand All @@ -182,7 +183,7 @@ const Config = (props: ConfigProps) => {
</FormItem>
</div>
<div className={styles.config}>
<FormItem label={'引擎'} name="engine">
<FormItem label={'引擎'} name={['tts', 'engine']}>
<Select
options={[
{
Expand All @@ -196,22 +197,22 @@ const Config = (props: ConfigProps) => {
]}
/>
</FormItem>
<FormItem label={'语言'} name="locale">
<FormItem label={'语言'} name={['tts', 'locale']}>
<Select options={suportedLocales} />
</FormItem>
<FormItem dependencies={['locale']} noStyle>
<FormItem dependencies={[['tts', 'locale']]} noStyle>
{() => (
<FormItem
label={'语音'}
name="voice"
name={['tts', 'voice']}
rules={[{ message: '请选择语音', required: true }]}
>
<Select
defaultActiveFirstOption
disabled={voiceLoading}
loading={voiceLoading}
options={voices
.filter((voice) => voice.locale === form.getFieldValue('locale'))
.filter((voice) => voice.locale === form.getFieldValue(['tts', 'locale']))
.map((item) => ({
label: `${item.DisplayName}-${item.LocalName}`,
value: item.ShortName,
Expand All @@ -220,10 +221,10 @@ const Config = (props: ConfigProps) => {
</FormItem>
)}
</FormItem>
<FormItem label={'语速'} name="speed">
<FormItem label={'语速'} name={['tts', 'speed']}>
<Slider max={3} min={0} step={0.01} />
</FormItem>
<FormItem label={'音调'} name="pitch">
<FormItem label={'音调'} name={['tts', 'pitch']}>
<Slider max={2} min={0} step={0.01} />
</FormItem>
<FormFooter>
Expand All @@ -250,7 +251,7 @@ const Config = (props: ConfigProps) => {
loading={loading}
onClick={() => {
const values = form.getFieldsValue();
speek(values);
speek({ ...values.tts });
}}
>
试听
Expand Down
26 changes: 26 additions & 0 deletions src/panels/RolePanel/RoleEdit/useSyncSetting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FormInstance } from 'antd/es/form/hooks/useForm';
import { isEqual } from 'lodash-es';
import { useLayoutEffect } from 'react';

import { agentListSelectors, useAgentStore } from '@/store/agent';

export const useSyncSettings = (form: FormInstance) => {
useLayoutEffect(() => {
const currentAgent = agentListSelectors.currentAgentItem(useAgentStore.getState());
console.log('currentAgent', currentAgent);
form.setFieldsValue(currentAgent);

// sync with later updated settings
const unsubscribe = useAgentStore.subscribe(
(s) => agentListSelectors.currentAgentItem(s),
(agent) => {
form.setFieldsValue(agent);
},
{ equalityFn: isEqual },
);

return () => {
unsubscribe();
};
}, []);
};
Loading

0 comments on commit fadef78

Please sign in to comment.