From 3ef2fa7680ff4b7836cd995726f971e69567591f Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 4 Dec 2023 14:39:15 +0100 Subject: [PATCH] New Chat Step: Generate default chat name (#4033) --- src/components/left/newChat/NewChatStep2.tsx | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/components/left/newChat/NewChatStep2.tsx b/src/components/left/newChat/NewChatStep2.tsx index dc23c5411..aa9f24143 100644 --- a/src/components/left/newChat/NewChatStep2.tsx +++ b/src/components/left/newChat/NewChatStep2.tsx @@ -3,10 +3,11 @@ import React, { memo, useCallback, useEffect, useState, } from '../../../lib/teact/teact'; -import { getActions, withGlobal } from '../../../global'; +import { getActions, getGlobal, withGlobal } from '../../../global'; import { ChatCreationProgress } from '../../../types'; +import { getUserFirstOrLastName } from '../../../global/helpers'; import { selectTabState } from '../../../global/selectors'; import useHistoryBack from '../../../hooks/useHistoryBack'; @@ -33,6 +34,8 @@ type StateProps = { maxGroupSize?: number; }; +const MAX_MEMBERS_FOR_GENERATE_CHAT_NAME = 4; + const NewChatStep2: FC = ({ isChannel, isActive, @@ -65,6 +68,25 @@ const NewChatStep2: FC = ({ const isLoading = creationProgress === ChatCreationProgress.InProgress; + useEffect(() => { + if (isChannel) { + return; + } + if (!memberIds.length || memberIds.length > MAX_MEMBERS_FOR_GENERATE_CHAT_NAME) { + setTitle(''); + return; + } + const global = getGlobal(); + const usersById = global.users.byId; + const memberFirstNames = [global.currentUserId!, ...memberIds] + .map((userId) => getUserFirstOrLastName(usersById[userId])) + .filter(Boolean); + const generatedChatName = memberFirstNames.slice(0, -1).join(', ') + + lang('CreateGroup.PeersTitleLastDelimeter') + + memberFirstNames[memberFirstNames.length - 1]; + setTitle(generatedChatName); + }, [isChannel, memberIds, lang]); + const handleTitleChange = useCallback((e: React.ChangeEvent) => { const { value } = e.currentTarget; const newValue = value.replace(/^\s+/, '');