import React, { FC, memo, useCallback, useEffect, } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import { GlobalState } from '../../global/types'; import { ApiChat, ApiCountryCode, ApiUser } from '../../api/types'; import { selectChat, selectNotifyExceptions, selectNotifySettings, selectUser, } from '../../global/selectors'; import { getChatDescription, getChatLink, getHasAdminRight, isChatChannel, isUserId, isUserRightBanned, selectIsChatMuted, } from '../../global/helpers'; import renderText from './helpers/renderText'; import { copyTextToClipboard } from '../../util/clipboard'; import { formatPhoneNumberWithCode } from '../../util/phoneNumber'; import useLang from '../../hooks/useLang'; import ListItem from '../ui/ListItem'; import Switcher from '../ui/Switcher'; type OwnProps = { chatOrUserId: string; forceShowSelf?: boolean; }; type StateProps = { user?: ApiUser; chat?: ApiChat; canInviteUsers?: boolean; isMuted?: boolean; phoneCodeList: ApiCountryCode[]; } & Pick; const ChatExtra: FC = ({ lastSyncTime, user, chat, forceShowSelf, canInviteUsers, isMuted, phoneCodeList, }) => { const { loadFullUser, showNotification, updateChatMutedState, } = getActions(); const { id: userId, fullInfo, username, phoneNumber, isSelf, } = user || {}; const { id: chatId } = chat || {}; const lang = useLang(); useEffect(() => { if (lastSyncTime && userId) { loadFullUser({ userId }); } }, [loadFullUser, userId, lastSyncTime]); const handleNotificationChange = useCallback(() => { updateChatMutedState({ chatId, isMuted: !isMuted }); }, [chatId, isMuted, updateChatMutedState]); if (!chat || chat.isRestricted || (isSelf && !forceShowSelf)) { return undefined; } function copy(text: string, entity: string) { copyTextToClipboard(text); showNotification({ message: `${entity} was copied` }); } const formattedNumber = phoneNumber && formatPhoneNumberWithCode(phoneCodeList, phoneNumber); const link = getChatLink(chat); const description = (fullInfo?.bio) || getChatDescription(chat); return (
{formattedNumber && Boolean(formattedNumber.length) && ( // eslint-disable-next-line react/jsx-no-bind copy(formattedNumber, lang('Phone'))}> {formattedNumber} {lang('Phone')} )} {username && ( copy(`@${username}`, lang('Username'))} > {renderText(username)} {lang('Username')} )} {description && Boolean(description.length) && ( {renderText(description, ['br', 'links', 'emoji'])} {lang(userId ? 'UserBio' : 'Info')} )} {(canInviteUsers || !username) && link && ( copy(link, lang('SetUrlPlaceholder'))} >
{link}
{lang('SetUrlPlaceholder')}
)} {!forceShowSelf && ( {lang('Notifications')} )}
); }; export default memo(withGlobal( (global, { chatOrUserId }): StateProps => { const { lastSyncTime, countryList: { phoneCodes: phoneCodeList } } = global; const chat = chatOrUserId ? selectChat(global, chatOrUserId) : undefined; const user = isUserId(chatOrUserId) ? selectUser(global, chatOrUserId) : undefined; const isMuted = chat && selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)); const canInviteUsers = chat && !user && ( (!isChatChannel(chat) && !isUserRightBanned(chat, 'inviteUsers')) || getHasAdminRight(chat, 'inviteUsers') ); return { lastSyncTime, phoneCodeList, chat, user, canInviteUsers, isMuted, }; }, )(ChatExtra));