import {PlusIcon, XMarkIcon} from '@heroicons/react/24/solid' import clsx from 'clsx' import {ProfileWithoutUser} from 'common/profiles/profile' import { getSocialEntries, getSocialLinkValues, isMultiValueSite, PLATFORM_LABELS, type Site, SITE_ORDER, Socials, } from 'common/socials' import {removeNullOrUndefinedProps} from 'common/util/object' import {Fragment, useState} from 'react' import {Button, IconButton} from 'web/components/buttons/button' import {Col} from 'web/components/layout/col' import {Input} from 'web/components/widgets/input' import {PlatformSelect} from 'web/components/widgets/platform-select' import {useT} from 'web/lib/locale' import {SocialIcon} from './user/social' interface SocialLinksSectionProps { profile: ProfileWithoutUser setProfile: (key: K, value: ProfileWithoutUser[K]) => void } export function SocialLinksSection({profile, setProfile}: SocialLinksSectionProps) { const t = useT() const [newLinkPlatform, setNewLinkPlatform] = useState('site') const [newLinkValue, setNewLinkValue] = useState('') const setLinks = (links: Socials) => { setProfile('links', removeNullOrUndefinedProps(links)) } const updateUserLink = (platform: string, value: string | null, index = 0) => { const links = {...((profile.links as Socials) ?? {})} const currentValue = links[platform] if (Array.isArray(currentValue)) { const nextValues = [...currentValue] if (value == null) { nextValues.splice(index, 1) } else { nextValues[index] = value } setLinks({...links, [platform]: nextValues.length > 0 ? nextValues : null}) return } setLinks({...links, [platform]: value}) } const addNewLink = () => { if (newLinkPlatform && newLinkValue) { const platform = newLinkPlatform.toLowerCase().trim() const value = newLinkValue.trim() const links = {...((profile.links as Socials) ?? {})} if (isMultiValueSite(platform) && links[platform] != null) { setLinks({ ...links, [platform]: [...getSocialLinkValues(links[platform]).filter(Boolean), value], }) } else { updateUserLink(platform, value) } setNewLinkPlatform('site') setNewLinkValue('') } } return (
{getSocialEntries((profile.links ?? {}) as Socials).map(({platform, value, index}) => (
{PLATFORM_LABELS[platform as Site] ?? platform}
) => updateUserLink(platform, e.target.value, index) } className="col-span-2 sm:col-span-1" /> updateUserLink(platform, null, index)}>
{t('common.remove', 'Remove')}
))} {/* Spacer */}
) => setNewLinkValue(e.target.value)} // disable password managers autoComplete="off" data-1p-ignore data-lpignore="true" data-bwignore="true" data-protonpass-ignore="true" className="w-full" />
) }