diff --git a/lib/selectors/chat-selectors.js b/lib/selectors/chat-selectors.js --- a/lib/selectors/chat-selectors.js +++ b/lib/selectors/chat-selectors.js @@ -605,17 +605,44 @@ return shouldSourceBeAdded ? messageInfos[sourceMessageID] : null; }); + // Get the latest edit message to show updated message content + // in a pending sidebar + const pendingSidebarEditMessageInfo = useSelector(state => { + const sourceMessageID = threadInfo?.sourceMessageID; + if (!pendingSidebarSourceMessageInfo || !threadInfo) { + return null; + } + const messageInfos = messageInfoSelector(state); + const containingThread = + state.messageStore.threads[threadInfo.containingThreadID]; + + const threadMessageInfos = (containingThread?.messageIDs ?? []) + .map((messageID: string) => messageInfos[messageID]) + .filter(Boolean) + .filter(message => message.targetMessageID === sourceMessageID) + .filter(message => message.type === messageTypes.EDIT_MESSAGE); + + const result = threadMessageInfos[0]; + return result ? result : null; + }); + invariant( !pendingSidebarSourceMessageInfo || pendingSidebarSourceMessageInfo.type !== messageTypes.SIDEBAR_SOURCE, 'sidebars can not be created from sidebar_source message', ); - const additionalMessages = React.useMemo( - () => - pendingSidebarSourceMessageInfo ? [pendingSidebarSourceMessageInfo] : [], - [pendingSidebarSourceMessageInfo], - ); + const additionalMessages = React.useMemo(() => { + if (!pendingSidebarSourceMessageInfo) { + return []; + } + const result = [pendingSidebarSourceMessageInfo]; + if (pendingSidebarEditMessageInfo) { + result.push(pendingSidebarEditMessageInfo); + } + return result; + }, [pendingSidebarSourceMessageInfo, pendingSidebarEditMessageInfo]); + const boundMessageListData = useSelector( messageListData(threadInfo?.id, additionalMessages), );