Files
monkey-ai-web/src/components/settings/personal/sections/AvatarSection.jsx
T

171 lines
6.1 KiB
React

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React from 'react';
import { Input as AntInput, Segmented } from 'antd';
import { Button, Input } from '@/components/ui';
import { Check } from 'lucide-react';
import {
CARTOON_AVATAR_PRESETS,
USER_AVATAR_TYPES,
getCartoonAvatarDataUrl,
getInitialAvatarDataUrl,
} from '../../../../helpers';
import SectionHeader from '../components/SectionHeader';
const AVATAR_TYPE_OPTIONS = [
{ value: USER_AVATAR_TYPES.PRESET, label: '卡通形象' },
{ value: USER_AVATAR_TYPES.INITIAL, label: '姓名首字' },
{ value: USER_AVATAR_TYPES.CUSTOM, label: '图片链接' },
];
const AvatarSection = ({
t,
userState,
avatarSettings,
handleAvatarSettingChange,
saveAvatarSettings,
savingAvatarSettings,
}) => {
const username =
userState?.user?.display_name || userState?.user?.username || 'User';
const previewUrl =
avatarSettings.avatarType === USER_AVATAR_TYPES.CUSTOM &&
avatarSettings.customAvatarUrl
? avatarSettings.customAvatarUrl
: avatarSettings.avatarType === USER_AVATAR_TYPES.INITIAL
? getInitialAvatarDataUrl(username, 128)
: getCartoonAvatarDataUrl(avatarSettings.avatarPreset, 128);
const customAvatarInvalid =
avatarSettings.avatarType === USER_AVATAR_TYPES.CUSTOM &&
!avatarSettings.customAvatarUrl?.trim();
// 与 getInitialAvatarDataUrl 相同的首字取法,仅用于展示,不进入保存载荷
const initialText = username ? username[0].toUpperCase() : '';
return (
<section className='ui-avatar-section'>
<SectionHeader
title={t('头像设置')}
description={t('选择你在平台内展示的头像样式')}
extra={
<Button
theme='solid'
loading={savingAvatarSettings}
disabled={customAvatarInvalid}
onClick={saveAvatarSettings}
>
{t('保存头像')}
</Button>
}
/>
<div className='ui-avatar-layout'>
<div className='ui-avatar-preview'>
<span className='ui-avatar-preview-frame'>
<img src={previewUrl} alt={t('头像预览')} />
</span>
<strong>{username}</strong>
<span className='ui-avatar-preview-live'>{t('实时预览')}</span>
<p>{t('头像将同步显示在用户入口和体验中心对话中')}</p>
</div>
<div className='ui-avatar-editor'>
<div className='ui-avatar-field-label'>{t('头像类型')}</div>
<Segmented
block
value={avatarSettings.avatarType}
onChange={(value) => handleAvatarSettingChange('avatarType', value)}
options={AVATAR_TYPE_OPTIONS.map((option) => ({
value: option.value,
label: t(option.label),
}))}
/>
{avatarSettings.avatarType === USER_AVATAR_TYPES.PRESET && (
<div className='ui-avatar-field'>
<div className='ui-avatar-field-label ui-avatar-field-label-muted'>
{t('选择卡通形象')}
</div>
<div className='ui-avatar-grid'>
{CARTOON_AVATAR_PRESETS.map((preset) => {
const selected = avatarSettings.avatarPreset === preset.id;
return (
<button
key={preset.id}
type='button'
aria-pressed={selected}
onClick={() => {
handleAvatarSettingChange(
'avatarType',
USER_AVATAR_TYPES.PRESET,
);
handleAvatarSettingChange('avatarPreset', preset.id);
}}
className={`ui-avatar-option ${selected ? 'selected' : ''}`}
>
<img
src={getCartoonAvatarDataUrl(preset.id, 96)}
alt={preset.label}
/>
<span>{t(preset.label)}</span>
{selected && (
<Check size={14} className='ui-avatar-check' />
)}
</button>
);
})}
</div>
</div>
)}
{avatarSettings.avatarType === USER_AVATAR_TYPES.INITIAL && (
<div className='ui-avatar-field'>
<div className='ui-avatar-field-label'>{t('显示文字')}</div>
<AntInput
readOnly
value={initialText}
className='ui-avatar-text-input'
aria-label={t('显示文字')}
/>
<p className='ui-avatar-help'>
{t('根据用户名自动生成简洁文字头像')}
</p>
</div>
)}
{avatarSettings.avatarType === USER_AVATAR_TYPES.CUSTOM && (
<div className='ui-avatar-field'>
<div className='ui-avatar-field-label'>{t('图片链接')}</div>
<Input
className='ui-avatar-text-input'
value={avatarSettings.customAvatarUrl}
onChange={(value) =>
handleAvatarSettingChange('customAvatarUrl', value)
}
placeholder='https://example.com/avatar.png'
showClear
/>
</div>
)}
</div>
</div>
</section>
);
};
export default AvatarSection;