import { Check, Close, Edit } from '@mui/icons-material' import clsx from 'clsx' import { useCallback, useState } from 'react' import toast from 'react-hot-toast' import { useTranslation } from 'react-i18next' import { ChainId, LoadingData, PfpkProfileUpdateFunction, UnifiedProfile, } from '@dao-dao/types' import { processError } from '@dao-dao/utils' import { Button } from '../buttons' import { ChainLogo } from '../chain/ChainLogo' import { IconButton } from '../icon_buttons' import { TextInput } from '../inputs' import { Loader } from '../logo' import { Tooltip, TooltipInfoIcon } from '../tooltip' export type ProfileNameDisplayAndEditorProps = { profile: LoadingData /** * Override the name to display when not editing. */ nameOverride?: string compact?: boolean updateProfile?: PfpkProfileUpdateFunction className?: string header?: boolean editingClassName?: string hideNoNameTooltip?: boolean } export const ProfileNameDisplayAndEditor = ({ compact, profile, nameOverride, updateProfile, className, header, editingClassName, hideNoNameTooltip, }: ProfileNameDisplayAndEditorProps) => { const { t } = useTranslation() const canEdit = !!updateProfile && !profile.loading && !!profile.data.uuid // If set, will show edit input. const [editingName, setEditingName] = useState() const [savingName, setSavingName] = useState(false) const doUpdateName = useCallback(async () => { if (editingName === undefined || !canEdit) { return } setSavingName(true) try { // Empty names unset. await updateProfile({ name: editingName.trim() || null }) // Stop editing on success. setEditingName(undefined) } catch (err) { console.error(err) toast.error( processError(err, { forceCapture: false, }) ) } finally { setSavingName(false) } }, [canEdit, editingName, updateProfile]) const noNameSet = !profile.loading && profile.data.name === null && !nameOverride const nameClassName = clsx('title-text', header && '!text-2xl !font-bold') // Height should match text line-height. const editingContainerClassName = header ? 'h-8 !min-w-72' : 'h-5' return (
{canEdit && editingName !== undefined ? (
setEditingName((event.target as HTMLInputElement).value) } onKeyDown={(event) => event.key === 'Escape' ? setEditingName(undefined) : event.key === 'Enter' ? doUpdateName() : undefined } size={1} value={editingName} />
{savingName ? ( ) : ( )} setEditingName(undefined)} size="xs" variant="ghost" />
) : ( )}
) }