-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
index.tsx
130 lines (119 loc) · 4 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Form, type FormItemProps, Icon, type ItemGroup, Tooltip } from '@lobehub/ui';
import { Button } from 'antd';
import isEqual from 'fast-deep-equal';
import { isString } from 'lodash-es';
import { UserCircle, Wand2 } from 'lucide-react';
import dynamic from 'next/dynamic';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { FORM_STYLE } from '@/const/layoutTokens';
import { useGlobalStore } from '@/store/global';
import { settingsSelectors } from '@/store/global/selectors';
import { useStore } from '../store';
import { SessionLoadingState } from '../store/initialState';
import AutoGenerateInput from './AutoGenerateInput';
import AutoGenerateSelect from './AutoGenerateSelect';
import BackgroundSwatches from './BackgroundSwatches';
const EmojiPicker = dynamic(() => import('@lobehub/ui/es/EmojiPicker'), { ssr: false });
const AgentMeta = memo(() => {
const { t } = useTranslation('setting');
const [hasSystemRole, updateMeta, autocompleteMeta, autocompleteAllMeta] = useStore((s) => [
!!s.config.systemRole,
s.setAgentMeta,
s.autocompleteMeta,
s.autocompleteAllMeta,
]);
const locale = useGlobalStore(settingsSelectors.currentLanguage);
const loading = useStore((s) => s.autocompleteLoading);
const meta = useStore((s) => s.meta, isEqual);
const basic = [
{
Render: AutoGenerateInput,
key: 'title',
label: t('settingAgent.name.title'),
onChange: (e: any) => updateMeta({ title: e.target.value }),
placeholder: t('settingAgent.name.placeholder'),
},
{
Render: AutoGenerateInput,
key: 'description',
label: t('settingAgent.description.title'),
onChange: (e: any) => updateMeta({ description: e.target.value }),
placeholder: t('settingAgent.description.placeholder'),
},
{
Render: AutoGenerateSelect,
key: 'tags',
label: t('settingAgent.tag.title'),
onChange: (e: any) => updateMeta({ tags: isString(e) ? e.split(',') : e }),
placeholder: t('settingAgent.tag.placeholder'),
},
];
const autocompleteItems: FormItemProps[] = basic.map((item) => {
const AutoGenerate = item.Render;
return {
children: (
<AutoGenerate
loading={loading[item.key as keyof SessionLoadingState]}
onChange={item.onChange}
onGenerate={() => {
autocompleteMeta(item.key as keyof typeof meta);
}}
placeholder={item.placeholder}
value={meta[item.key as keyof typeof meta]}
/>
),
label: item.label,
};
});
const metaData: ItemGroup = useMemo(
() => ({
children: [
{
children: (
<EmojiPicker
backgroundColor={meta.backgroundColor}
locale={locale}
onChange={(avatar) => updateMeta({ avatar })}
value={meta.avatar}
/>
),
label: t('settingAgent.avatar.title'),
minWidth: undefined,
},
{
children: (
<BackgroundSwatches
backgroundColor={meta.backgroundColor}
onChange={(backgroundColor) => updateMeta({ backgroundColor })}
/>
),
label: t('settingAgent.backgroundColor.title'),
minWidth: undefined,
},
...autocompleteItems,
],
extra: (
<Tooltip title={t('autoGenerateTooltip', { ns: 'common' })}>
<Button
disabled={!hasSystemRole}
icon={<Icon icon={Wand2} />}
loading={Object.values(loading).some((i) => !!i)}
onClick={(e: any) => {
e.stopPropagation();
autocompleteAllMeta(true);
}}
size={'small'}
>
{t('autoGenerate', { ns: 'common' })}
</Button>
</Tooltip>
),
icon: UserCircle,
title: t('settingAgent.title'),
}),
[autocompleteItems, meta],
);
return <Form items={[metaData]} {...FORM_STYLE} />;
});
export default AgentMeta;