import type React from 'react'; import { useEffect, useState } from 'react'; import { Dialog, IconViewList, Toast, useUIKit, } from '@tencentcloud/uikit-base-component-react'; import cs from 'classnames'; import { Avatar, useLoginState } from 'tuikit-atomicx-react/chat'; import classes from './ProfileEditButton.module.scss'; export interface ProfileEditButtonProps { label?: string; iconSize?: number; disabled?: boolean; className?: string; style?: React.CSSProperties; showLabel?: boolean; } const avatarUrlPattern = /^https?:\/\/\S+$/i; function ProfileEditButton(props: ProfileEditButtonProps): React.ReactElement { const { label, iconSize = 20, disabled = false, className, style, showLabel = false, } = props; const { t } = useUIKit(); const { loginUserInfo, setSelfInfo } = useLoginState(); const [isDialogOpen, setIsDialogOpen] = useState(false); const [isSaving, setIsSaving] = useState(false); const [userName, setUserName] = useState(''); const [avatarUrl, setAvatarUrl] = useState(''); useEffect(() => { if (!isDialogOpen) { return; } setUserName(loginUserInfo?.userName || ''); setAvatarUrl(loginUserInfo?.avatarUrl || ''); }, [isDialogOpen, loginUserInfo?.avatarUrl, loginUserInfo?.userName]); const openDialog = () => { setUserName(loginUserInfo?.userName || ''); setAvatarUrl(loginUserInfo?.avatarUrl || ''); setIsDialogOpen(true); }; const closeDialog = () => { if (isSaving) { return; } setIsDialogOpen(false); }; const handleSaveProfile = async () => { const trimmedUserName = userName.trim(); const trimmedAvatarUrl = avatarUrl.trim(); if (!trimmedUserName) { Toast.warning({ message: t('TUIProfile.Nickname required'), }); return; } if (trimmedAvatarUrl && !avatarUrlPattern.test(trimmedAvatarUrl)) { Toast.warning({ message: t('TUIProfile.Avatar URL invalid'), }); return; } setIsSaving(true); try { await setSelfInfo({ userName: trimmedUserName, avatarUrl: trimmedAvatarUrl, }); Toast.success({ message: t('TUIProfile.Profile updated successfully'), }); setIsDialogOpen(false); } catch (error) { Toast.error({ message: `${t('TUIProfile.Profile update failed')}${error instanceof Error ? `: ${error.message}` : ''}`, }); } finally { setIsSaving(false); } }; return ( <>
{userName.trim() || loginUserInfo?.userName || loginUserInfo?.userId}
{loginUserInfo?.userId}
{t('TUIProfile.Avatar URL hint')}
); } export { ProfileEditButton };