From d60d2c8c75b419f88f9c0c0dc514435ee70d55e9 Mon Sep 17 00:00:00 2001 From: William Grant Date: Tue, 16 May 2023 13:02:30 +1000 Subject: [PATCH] feat: quotes will show the authors name where possible cleaned up extra author props on quote and created a hook instead --- .../message/message-content/MessageQuote.tsx | 1 - .../message/message-content/quote/Quote.tsx | 18 ++--------- .../message-content/quote/QuoteAuthor.tsx | 31 +++++++++---------- ts/hooks/useParamSelector.ts | 11 ++++++- ts/state/ducks/conversations.ts | 7 ++--- ts/state/selectors/conversations.ts | 23 +++++++------- 6 files changed, 43 insertions(+), 48 deletions(-) diff --git a/ts/components/conversation/message/message-content/MessageQuote.tsx b/ts/components/conversation/message/message-content/MessageQuote.tsx index eaf3c8386..bf00f3aeb 100644 --- a/ts/components/conversation/message/message-content/MessageQuote.tsx +++ b/ts/components/conversation/message/message-content/MessageQuote.tsx @@ -82,7 +82,6 @@ export const MessageQuote = (props: Props) => { attachment={quote?.attachment} isIncoming={direction === 'incoming'} author={quote.author} - authorName={quote?.authorName} referencedMessageNotFound={quoteNotFound} isFromMe={Boolean(quote.isFromMe)} /> diff --git a/ts/components/conversation/message/message-content/quote/Quote.tsx b/ts/components/conversation/message/message-content/quote/Quote.tsx index e0d3e1ff9..c4d2cd62f 100644 --- a/ts/components/conversation/message/message-content/quote/Quote.tsx +++ b/ts/components/conversation/message/message-content/quote/Quote.tsx @@ -2,9 +2,6 @@ import React, { MouseEvent, useState } from 'react'; import * as MIME from '../../../../../types/MIME'; -import { useSelector } from 'react-redux'; - -import { isPublicGroupConversation } from '../../../../../state/selectors/conversations'; import { QuoteAuthor } from './QuoteAuthor'; import { QuoteText } from './QuoteText'; import { QuoteIconContainer } from './QuoteIconContainer'; @@ -49,12 +46,11 @@ const StyledQuoteTextContent = styled.div` export type QuoteProps = { attachment?: QuotedAttachmentType; author: string; - authorProfileName?: string; - authorName?: string; isFromMe: boolean; isIncoming: boolean; - text: string | null; referencedMessageNotFound: boolean; + text: string | null; + onClick?: (e: React.MouseEvent) => void; }; @@ -73,8 +69,6 @@ export interface QuotedAttachmentType { } export const Quote = (props: QuoteProps) => { - const isPublic = useSelector(isPublicGroupConversation); - const { isIncoming, attachment, text, referencedMessageNotFound, onClick } = props; const [imageBroken, setImageBroken] = useState(false); @@ -96,13 +90,7 @@ export const Quote = (props: QuoteProps) => { referencedMessageNotFound={referencedMessageNotFound} /> - + ` color: ${props => @@ -21,30 +24,26 @@ const StyledQuoteAuthor = styled.div<{ isIncoming: boolean }>` } `; -type QuoteAuthorProps = Pick & { - showPubkeyForAuthor: boolean; -}; +type QuoteAuthorProps = Pick; export const QuoteAuthor = (props: QuoteAuthorProps) => { - const { author, authorName, isFromMe, isIncoming, showPubkeyForAuthor } = props; - debugger; + const { author, isIncoming } = props; + + const isPublic = useSelector(isPublicGroupConversation); + const authorName = useQuoteAuthorName(author); - if (author === '' || authorName === '') { + if (!author || author === '' || !authorName) { return null; } return ( - {isFromMe ? ( - window.i18n('you') - ) : ( - - )} + ); }; diff --git a/ts/hooks/useParamSelector.ts b/ts/hooks/useParamSelector.ts index 2a5706af6..a3c1e6940 100644 --- a/ts/hooks/useParamSelector.ts +++ b/ts/hooks/useParamSelector.ts @@ -37,7 +37,7 @@ export function useConversationUsernameOrShorten(convoId?: string) { } /** - * Returns the name if that conversation. + * Returns the name of that conversation. * This is the group name, or the realName of a user for a private conversation with a recent nickname set */ export function useConversationRealName(convoId?: string) { @@ -183,3 +183,12 @@ export function useMessageReactsPropsById(messageId?: string) { return messageReactsProps; }); } + +export function useQuoteAuthorName(authorId?: string) { + const convoProps = useConversationPropsById(authorId); + return authorId === UserUtils.getOurPubKeyStrFromCache() + ? window.i18n('you') + : convoProps?.nickname || convoProps?.isPrivate + ? convoProps?.displayNameInProfile + : undefined; +} diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 6d72f3c3f..24b477265 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -165,14 +165,13 @@ export type PropsForAttachment = { }; export type PropsForQuote = { - text?: string; attachment?: QuotedAttachmentType; - isFromMe?: boolean; author: string; - authorName?: string; + convoId?: string; id?: string; // this is the quoted message timestamp + isFromMe?: boolean; referencedMessageNotFound?: boolean; - convoId?: string; + text?: string; }; export type PropsForMessageWithoutConvoProps = { diff --git a/ts/state/selectors/conversations.ts b/ts/state/selectors/conversations.ts index 9592f5cf5..5a47407ff 100644 --- a/ts/state/selectors/conversations.ts +++ b/ts/state/selectors/conversations.ts @@ -997,33 +997,39 @@ export const getMessageQuoteProps = createSelector( return undefined; } - const { id, author, authorName: _authorName } = msgProps.quote; + const { id, author } = msgProps.quote; if (!id || !author) { return undefined; } + let isFromMe = UserUtils.isUsFromCache(author) || false; + + const quoteNotFound = { + direction, + quote: { author, isFromMe, referencedMessageNotFound: true }, + }; + // NOTE: if the message is not found, we still want to render the quote if (!quotesProps || isEmpty(quotesProps)) { - return { direction, quote: { author, referencedMessageNotFound: true } }; + return quoteNotFound; } const sourceMessage = quotesProps[`${id}-${author}`]; if (!sourceMessage) { - return { direction, quote: { author, referencedMessageNotFound: true } }; + return quoteNotFound; } const sourceMsgProps = sourceMessage.propsForMessage; if (!sourceMsgProps || sourceMsgProps.isDeleted) { - return { direction, quote: { author, referencedMessageNotFound: true } }; + return quoteNotFound; } const convo = getConversationController().get(sourceMsgProps.convoId); if (!convo) { - return { direction, quote: { author, referencedMessageNotFound: true } }; + return quoteNotFound; } const attachment = sourceMsgProps.attachments && sourceMsgProps.attachments[0]; - let isFromMe = convo ? UserUtils.isUsFromCache(author) : false; if (convo.isPublic() && PubKey.hasBlindedPrefix(sourceMsgProps.sender)) { const room = OpenGroupData.getV2OpenGroupRoom(sourceMsgProps.convoId); @@ -1038,16 +1044,11 @@ export const getMessageQuoteProps = createSelector( } } - const authorName = isFromMe - ? window.i18n('you') - : convo.getNicknameOrRealUsernameOrPlaceholder(); - const quote: PropsForQuote = { text: sourceMsgProps.text, attachment: attachment ? processQuoteAttachment(attachment) : undefined, isFromMe, author: sourceMsgProps.sender, - authorName, id: sourceMsgProps.id, referencedMessageNotFound: false, convoId: convo.id,