Files
monkey-ai-web/src/components/settings/personal/sections/AvatarSection.jsx
T
2026-10-15 16:33:46 +08:00

235 lines
9.4 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 { Button, Input, Radio, RadioGroup, Tag } from '@douyinfe/semi-ui';
import { Image, Palette, Sparkles, Type, Wand2 } from 'lucide-react';
import {
CARTOON_AVATAR_PRESETS,
USER_AVATAR_TYPES,
getCartoonAvatarDataUrl,
getInitialAvatarDataUrl,
} from '../../../../helpers';
const AVATAR_TYPE_OPTIONS = [
{
value: USER_AVATAR_TYPES.PRESET,
label: '卡通形象',
desc: '从系统内置卡通头像中选择,适合默认展示',
icon: Palette,
},
{
value: USER_AVATAR_TYPES.INITIAL,
label: '姓名首字',
desc: '根据用户名自动生成简洁文字头像',
icon: Type,
},
{
value: USER_AVATAR_TYPES.CUSTOM,
label: '图片链接',
desc: '填写公网图片 URL,使用自己的头像',
icon: Image,
},
];
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();
return (
<div className='space-y-6'>
<div className='flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 rounded-3xl border border-white/70 dark:border-white/10 bg-white/50 dark:bg-white/[0.045] backdrop-blur-xl px-5 py-4'>
<div className='flex items-center gap-3'>
<div className='w-11 h-11 rounded-2xl bg-gradient-to-br from-rose-500 to-orange-400 flex items-center justify-center shadow-lg shadow-rose-500/20'>
<Wand2 className='w-5 h-5 text-white' />
</div>
<div>
<h2 className='text-xl font-bold text-gray-900 dark:text-white'>
{t('头像设置')}
</h2>
<p className='text-sm text-gray-500 dark:text-gray-400'>
{t('选择你在平台内展示的头像样式')}
</p>
</div>
</div>
<Button
theme='solid'
loading={savingAvatarSettings}
disabled={customAvatarInvalid}
onClick={saveAvatarSettings}
className='!bg-gradient-to-r !from-rose-500 !to-orange-400 !border-0 !rounded-xl !shadow-lg !shadow-rose-500/20'
>
{t('保存头像')}
</Button>
</div>
<div className='grid grid-cols-1 xl:grid-cols-[280px_1fr] gap-5'>
<div className='relative overflow-hidden rounded-[28px] border border-white/70 dark:border-white/10 bg-white/[0.54] dark:bg-white/[0.045] p-5 shadow-[0_20px_55px_rgba(244,63,94,0.10)]'>
<div className='absolute inset-0 bg-[radial-gradient(circle_at_25%_20%,rgba(251,113,133,0.22),transparent_32%),radial-gradient(circle_at_90%_85%,rgba(251,146,60,0.16),transparent_34%)] pointer-events-none' />
<div className='relative flex flex-col items-center text-center'>
<div className='relative mb-4'>
<div className='absolute inset-0 rounded-[32px] bg-gradient-to-br from-rose-500 to-orange-400 blur-lg opacity-30' />
<img
src={previewUrl}
alt={t('头像预览')}
className='relative w-28 h-28 rounded-[32px] object-cover ring-4 ring-white/90 dark:ring-slate-950 shadow-xl'
/>
<span className='absolute -bottom-1 -right-1 w-6 h-6 rounded-full bg-emerald-500 border-4 border-white dark:border-slate-950' />
</div>
<div className='text-base font-semibold text-gray-900 dark:text-white'>
{username}
</div>
<div className='mt-2 text-xs text-gray-500 dark:text-gray-400 leading-relaxed'>
{t('该头像会同步展示在右上角用户入口和体验中心文本对话中。')}
</div>
<Tag
color='pink'
shape='circle'
className='mt-4 !bg-white/70 dark:!bg-white/[0.08]'
>
{t('实时预览')}
</Tag>
</div>
</div>
<div className='rounded-[28px] border border-white/70 dark:border-white/10 bg-white/50 dark:bg-white/[0.045] backdrop-blur-xl p-5'>
<div className='mb-5'>
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3'>
{t('头像类型')}
</label>
<RadioGroup
type='button'
value={avatarSettings.avatarType}
onChange={(e) =>
handleAvatarSettingChange('avatarType', e.target.value)
}
className='!flex !flex-wrap !gap-2'
>
{AVATAR_TYPE_OPTIONS.map((option) => {
const Icon = option.icon;
return (
<Radio key={option.value} value={option.value}>
<span className='inline-flex items-center gap-1.5'>
<Icon size={14} />
{t(option.label)}
</span>
</Radio>
);
})}
</RadioGroup>
<p className='mt-2 text-xs text-gray-500 dark:text-gray-400'>
{t(
AVATAR_TYPE_OPTIONS.find(
(item) => item.value === avatarSettings.avatarType,
)?.desc || '',
)}
</p>
</div>
{avatarSettings.avatarType === USER_AVATAR_TYPES.PRESET && (
<div>
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3'>
{t('选择卡通形象')}
</label>
<div className='grid grid-cols-2 sm:grid-cols-4 gap-3'>
{CARTOON_AVATAR_PRESETS.map((preset) => {
const selected = avatarSettings.avatarPreset === preset.id;
return (
<button
key={preset.id}
type='button'
onClick={() => {
handleAvatarSettingChange(
'avatarType',
USER_AVATAR_TYPES.PRESET,
);
handleAvatarSettingChange('avatarPreset', preset.id);
}}
className={`group relative rounded-2xl border p-3 transition-all duration-200 ${
selected
? 'border-rose-300 bg-white/[0.82] dark:bg-white/[0.08] shadow-lg shadow-rose-500/10'
: 'border-white/70 dark:border-white/10 bg-white/[0.46] dark:bg-white/[0.035] hover:border-rose-200 dark:hover:border-rose-400/25'
}`}
>
<img
src={getCartoonAvatarDataUrl(preset.id, 96)}
alt={preset.label}
className='mx-auto w-14 h-14 rounded-2xl object-cover transition-transform group-hover:scale-105'
/>
<div className='mt-2 text-xs font-medium text-gray-700 dark:text-gray-200 truncate'>
{t(preset.label)}
</div>
{selected && (
<Sparkles
size={16}
className='absolute top-2 right-2 text-rose-500'
/>
)}
</button>
);
})}
</div>
</div>
)}
{avatarSettings.avatarType === USER_AVATAR_TYPES.CUSTOM && (
<div className='rounded-2xl border border-sky-100 dark:border-sky-400/20 bg-sky-50/[0.55] dark:bg-sky-400/[0.06] p-4'>
<Input
value={avatarSettings.customAvatarUrl}
onChange={(value) =>
handleAvatarSettingChange('customAvatarUrl', value)
}
placeholder={t(
'请输入图片 URL,例如 https://example.com/avatar.png',
)}
prefix={<Image size={15} className='text-sky-500' />}
showClear
/>
<p className='mt-2 text-xs text-gray-500 dark:text-gray-400'>
{t('支持公网图片链接。若图片加载失败,会回退到默认卡通头像。')}
</p>
</div>
)}
</div>
</div>
</div>
);
};
export default AvatarSection;