Remove user's own name from the autocomplete suggestions (#1258)

* remove the username from list

* fix updateAuthorList returns
This commit is contained in:
Meisam 2021-07-26 07:26:27 +02:00 committed by GitHub
parent 45af1f5135
commit 10456b0a01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -140,6 +140,7 @@ export default class Chat extends Component {
// fetch chat history // fetch chat history
getChatHistory(accessToken) { getChatHistory(accessToken) {
const { username } = this.props;
fetch(URL_CHAT_HISTORY + `?accessToken=${accessToken}`) fetch(URL_CHAT_HISTORY + `?accessToken=${accessToken}`)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
@ -149,7 +150,8 @@ export default class Chat extends Component {
}) })
.then((data) => { .then((data) => {
// extra user names // extra user names
const chatUserNames = extraUserNamesFromMessageHistory(data); const allChatUserNames = extraUserNamesFromMessageHistory(data);
const chatUserNames = allChatUserNames.filter(name => name != username);
this.setState({ this.setState({
messages: data.concat(this.state.messages), messages: data.concat(this.state.messages),
chatUserNames, chatUserNames,
@ -179,7 +181,7 @@ export default class Chat extends Component {
visible: messageVisible, visible: messageVisible,
} = message; } = message;
const { messages: curMessages } = this.state; const { messages: curMessages } = this.state;
const { messagesOnly } = this.props; const { username, messagesOnly } = this.props;
const existingIndex = curMessages.findIndex( const existingIndex = curMessages.findIndex(
(item) => item.id === messageId (item) => item.id === messageId
@ -221,8 +223,9 @@ export default class Chat extends Component {
const newState = { const newState = {
messages: [...curMessages, message], messages: [...curMessages, message],
}; };
const updatedChatUserNames = this.updateAuthorList(message); const updatedAllChatUserNames = this.updateAuthorList(message);
if (updatedChatUserNames.length) { if (updatedAllChatUserNames.length) {
const updatedChatUserNames = updatedAllChatUserNames.filter(name => name != username);
newState.chatUserNames = [...updatedChatUserNames]; newState.chatUserNames = [...updatedChatUserNames];
} }
this.setState(newState); this.setState(newState);
@ -260,17 +263,19 @@ export default class Chat extends Component {
updateAuthorList(message) { updateAuthorList(message) {
const { type } = message; const { type } = message;
const nameList = this.state.chatUserNames; let nameList = this.state.chatUserNames;
if ( if (
type === SOCKET_MESSAGE_TYPES.CHAT && type === SOCKET_MESSAGE_TYPES.CHAT &&
!nameList.includes(message.user.displayName) !nameList.includes(message.user.displayName)
) { ) {
return nameList.push(message.user.displayName); nameList.push(message.user.displayName)
return nameList;
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) { } else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
const { oldName, newName } = message; const { oldName, newName } = message;
const oldNameIndex = nameList.indexOf(oldName); const oldNameIndex = nameList.indexOf(oldName);
return nameList.splice(oldNameIndex, 1, newName); nameList.splice(oldNameIndex, 1, newName);
return nameList;
} }
return []; return [];
} }