From 4632cb49641724579eda0cf30d2ce36307085e65 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 5 Dec 2024 22:21:14 -0300 Subject: [PATCH] refactor: create PureStatusReplyMentions component and use it in PureStatus component --- src/components/pure-status-reply-mentions.tsx | 113 ++++++++++++++++++ src/components/pure-status.tsx | 4 +- 2 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 src/components/pure-status-reply-mentions.tsx diff --git a/src/components/pure-status-reply-mentions.tsx b/src/components/pure-status-reply-mentions.tsx new file mode 100644 index 000000000..49a63b6df --- /dev/null +++ b/src/components/pure-status-reply-mentions.tsx @@ -0,0 +1,113 @@ +import { FormattedList, FormattedMessage } from 'react-intl'; +import { Link } from 'react-router-dom'; + +import { openModal } from 'soapbox/actions/modals.ts'; +import HoverRefWrapper from 'soapbox/components/hover-ref-wrapper.tsx'; +import HoverStatusWrapper from 'soapbox/components/hover-status-wrapper.tsx'; +import { Entities, EntityTypes } from 'soapbox/entity-store/entities.ts'; +import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts'; +import { shortenNostr } from 'soapbox/utils/nostr.ts'; + + +interface IPureStatusReplyMentions { + status: EntityTypes[Entities.STATUSES]; + hoverable?: boolean; +} + +const PureStatusReplyMentions: React.FC = ({ status, hoverable = true }) => { + const dispatch = useAppDispatch(); + + const handleOpenMentionsModal: React.MouseEventHandler = (e) => { + e.stopPropagation(); + + const account = status.account; + + dispatch(openModal('MENTIONS', { + username: account.acct, + statusId: status.id, + })); + }; + + if (!status.in_reply_to_id) { + return null; + } + + const to = status.mentions; + + // The post is a reply, but it has no mentions. + // Rare, but it can happen. + if (to.length === 0) { + return ( +
+ +
+ ); + } + + // The typical case with a reply-to and a list of mentions. + const accounts = to.slice(0, 2).map(account => { + const link = ( + e.stopPropagation()} + > {/* eslint-disable-line formatjs/no-literal-string-in-jsx */} + @{shortenNostr(account.username)} + + ); + + if (hoverable) { + return ( + + {link} + + ); + } else { + return link; + } + }); + + if (to.length > 2) { + accounts.push( + + + , + ); + } + + return ( +
+ , + // @ts-ignore wtf? + hover: (children: React.ReactNode) => { + if (hoverable) { + return ( + + + {children} + + + ); + } else { + return children; + } + }, + }} + /> +
+ ); +}; + +export default PureStatusReplyMentions; diff --git a/src/components/pure-status.tsx b/src/components/pure-status.tsx index 9aa081836..68e000795 100644 --- a/src/components/pure-status.tsx +++ b/src/components/pure-status.tsx @@ -8,6 +8,7 @@ import { Link, useHistory } from 'react-router-dom'; import { openModal } from 'soapbox/actions/modals.ts'; import { unfilterStatus } from 'soapbox/actions/statuses.ts'; +import PureStatusReplyMentions from 'soapbox/components/pure-status-reply-mentions.tsx'; import TranslateButton from 'soapbox/components/translate-button.tsx'; import { Card } from 'soapbox/components/ui/card.tsx'; import Icon from 'soapbox/components/ui/icon.tsx'; @@ -33,7 +34,6 @@ import EventPreview from './event-preview.tsx'; import StatusActionBar from './status-action-bar.tsx'; import StatusContent from './status-content.tsx'; import StatusMedia from './status-media.tsx'; -import StatusReplyMentions from './status-reply-mentions.tsx'; import SensitiveContentOverlay from './statuses/sensitive-content-overlay.tsx'; import StatusInfo from './statuses/status-info.tsx'; import Tombstone from './tombstone.tsx'; @@ -451,7 +451,7 @@ const PureStatus: React.FC = (props) => { />
- {/* fix later */} +