import classNames from 'clsx'; import { List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable'; import debounce from 'lodash/debounce'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { HotKeys } from 'react-hotkeys'; import { defineMessages, useIntl } from 'react-intl'; import { Redirect, useHistory } from 'react-router-dom'; import { createSelector } from 'reselect'; import { replyCompose, mentionCompose, } from 'soapbox/actions/compose'; import { favourite, unfavourite, reblog, unreblog, } from 'soapbox/actions/interactions'; import { openModal } from 'soapbox/actions/modals'; import { getSettings } from 'soapbox/actions/settings'; import { hideStatus, revealStatus, fetchStatusWithContext, fetchNext, } from 'soapbox/actions/statuses'; import MissingIndicator from 'soapbox/components/missing_indicator'; import PullToRefresh from 'soapbox/components/pull-to-refresh'; import ScrollableList from 'soapbox/components/scrollable_list'; import StatusActionBar from 'soapbox/components/status-action-bar'; import ModerationOverlay from 'soapbox/components/statuses/moderation-overlay'; import SubNavigation from 'soapbox/components/sub_navigation'; import Tombstone from 'soapbox/components/tombstone'; import { Column, Stack } from 'soapbox/components/ui'; import PlaceholderStatus from 'soapbox/features/placeholder/components/placeholder_status'; import PendingStatus from 'soapbox/features/ui/components/pending_status'; import { useAppDispatch, useAppSelector, useSettings } from 'soapbox/hooks'; import { makeGetStatus } from 'soapbox/selectors'; import { defaultMediaVisibility, textForScreenReader } from 'soapbox/utils/status'; import DetailedStatus from './components/detailed-status'; import ThreadLoginCta from './components/thread-login-cta'; import ThreadStatus from './components/thread-status'; import type { VirtuosoHandle } from 'react-virtuoso'; import type { RootState } from 'soapbox/store'; import type { Account as AccountEntity, Attachment as AttachmentEntity, Status as StatusEntity, } from 'soapbox/types/entities'; const messages = defineMessages({ title: { id: 'status.title', defaultMessage: '@{username}\'s Post' }, titleDirect: { id: 'status.title_direct', defaultMessage: 'Direct message' }, deleteConfirm: { id: 'confirmations.delete.confirm', defaultMessage: 'Delete' }, deleteHeading: { id: 'confirmations.delete.heading', defaultMessage: 'Delete post' }, deleteMessage: { id: 'confirmations.delete.message', defaultMessage: 'Are you sure you want to delete this post?' }, redraftConfirm: { id: 'confirmations.redraft.confirm', defaultMessage: 'Delete & redraft' }, redraftHeading: { id: 'confirmations.redraft.heading', defaultMessage: 'Delete & redraft' }, redraftMessage: { id: 'confirmations.redraft.message', defaultMessage: 'Are you sure you want to delete this post and re-draft it? Favorites and reposts will be lost, and replies to the original post will be orphaned.' }, blockConfirm: { id: 'confirmations.block.confirm', defaultMessage: 'Block' }, revealAll: { id: 'status.show_more_all', defaultMessage: 'Show more for all' }, hideAll: { id: 'status.show_less_all', defaultMessage: 'Show less for all' }, detailedStatus: { id: 'status.detailed_status', defaultMessage: 'Detailed conversation view' }, replyConfirm: { id: 'confirmations.reply.confirm', defaultMessage: 'Reply' }, replyMessage: { id: 'confirmations.reply.message', defaultMessage: 'Replying now will overwrite the message you are currently composing. Are you sure you want to proceed?' }, blockAndReport: { id: 'confirmations.block.block_and_report', defaultMessage: 'Block & Report' }, }); const getAncestorsIds = createSelector([ (_: RootState, statusId: string | undefined) => statusId, (state: RootState) => state.contexts.inReplyTos, ], (statusId, inReplyTos) => { let ancestorsIds = ImmutableOrderedSet(); let id: string | undefined = statusId; while (id && !ancestorsIds.includes(id)) { ancestorsIds = ImmutableOrderedSet([id]).union(ancestorsIds); id = inReplyTos.get(id); } return ancestorsIds; }); const getDescendantsIds = createSelector([ (_: RootState, statusId: string) => statusId, (state: RootState) => state.contexts.replies, ], (statusId, contextReplies) => { let descendantsIds = ImmutableOrderedSet(); const ids = [statusId]; while (ids.length > 0) { const id = ids.shift(); if (!id) break; const replies = contextReplies.get(id); if (descendantsIds.includes(id)) { break; } if (statusId !== id) { descendantsIds = descendantsIds.union([id]); } if (replies) { replies.reverse().forEach((reply: string) => { ids.unshift(reply); }); } } return descendantsIds; }); type DisplayMedia = 'default' | 'hide_all' | 'show_all'; type RouteParams = { statusId: string }; interface IThread { params: RouteParams, onOpenMedia: (media: ImmutableList, index: number) => void, onOpenVideo: (video: AttachmentEntity, time: number) => void, } const Thread: React.FC = (props) => { const intl = useIntl(); const history = useHistory(); const dispatch = useAppDispatch(); const settings = useSettings(); const getStatus = useCallback(makeGetStatus(), []); const me = useAppSelector(state => state.me); const status = useAppSelector(state => getStatus(state, { id: props.params.statusId })); const displayMedia = settings.get('displayMedia') as DisplayMedia; const inReview = status?.visibility === 'self'; const { ancestorsIds, descendantsIds } = useAppSelector(state => { let ancestorsIds = ImmutableOrderedSet(); let descendantsIds = ImmutableOrderedSet(); if (status) { const statusId = status.id; ancestorsIds = getAncestorsIds(state, state.contexts.inReplyTos.get(statusId)); descendantsIds = getDescendantsIds(state, statusId); ancestorsIds = ancestorsIds.delete(statusId).subtract(descendantsIds); descendantsIds = descendantsIds.delete(statusId).subtract(ancestorsIds); } return { status, ancestorsIds, descendantsIds, }; }); const [showMedia, setShowMedia] = useState(defaultMediaVisibility(status, displayMedia)); const [isLoaded, setIsLoaded] = useState(!!status); const [next, setNext] = useState(); const node = useRef(null); const statusRef = useRef(null); const scroller = useRef(null); /** Fetch the status (and context) from the API. */ const fetchData = async() => { const { params } = props; const { statusId } = params; const { next } = await dispatch(fetchStatusWithContext(statusId)); setNext(next); }; // Load data. useEffect(() => { fetchData().then(() => { setIsLoaded(true); }).catch(error => { setIsLoaded(true); }); }, [props.params.statusId]); const handleToggleMediaVisibility = () => { setShowMedia(!showMedia); }; const handleHotkeyReact = () => { if (statusRef.current) { const firstEmoji: HTMLButtonElement | null = statusRef.current.querySelector('.emoji-react-selector .emoji-react-selector__emoji'); firstEmoji?.focus(); } }; const handleFavouriteClick = (status: StatusEntity) => { if (status.favourited) { dispatch(unfavourite(status)); } else { dispatch(favourite(status)); } }; const handleReplyClick = (status: StatusEntity) => { dispatch(replyCompose(status)); }; const handleModalReblog = (status: StatusEntity) => { dispatch(reblog(status)); }; const handleReblogClick = (status: StatusEntity, e?: React.MouseEvent) => { dispatch((_, getState) => { const boostModal = getSettings(getState()).get('boostModal'); if (status.reblogged) { dispatch(unreblog(status)); } else { if ((e && e.shiftKey) || !boostModal) { handleModalReblog(status); } else { dispatch(openModal('BOOST', { status, onReblog: handleModalReblog })); } } }); }; const handleMentionClick = (account: AccountEntity) => { dispatch(mentionCompose(account)); }; const handleOpenMedia = (media: ImmutableList, index: number) => { dispatch(openModal('MEDIA', { media, index })); }; const handleOpenVideo = (media: ImmutableList, time: number) => { dispatch(openModal('VIDEO', { media, time })); }; const handleHotkeyOpenMedia = (e?: KeyboardEvent) => { const { onOpenMedia, onOpenVideo } = props; const firstAttachment = status?.media_attachments.get(0); e?.preventDefault(); if (status && firstAttachment) { if (firstAttachment.type === 'video') { onOpenVideo(firstAttachment, 0); } else { onOpenMedia(status.media_attachments, 0); } } }; const handleToggleHidden = (status: StatusEntity) => { if (status.hidden) { dispatch(revealStatus(status.id)); } else { dispatch(hideStatus(status.id)); } }; const handleHotkeyMoveUp = () => { handleMoveUp(status!.id); }; const handleHotkeyMoveDown = () => { handleMoveDown(status!.id); }; const handleHotkeyReply = (e?: KeyboardEvent) => { e?.preventDefault(); handleReplyClick(status!); }; const handleHotkeyFavourite = () => { handleFavouriteClick(status!); }; const handleHotkeyBoost = () => { handleReblogClick(status!); }; const handleHotkeyMention = (e?: KeyboardEvent) => { e?.preventDefault(); const { account } = status!; if (!account || typeof account !== 'object') return; handleMentionClick(account); }; const handleHotkeyOpenProfile = () => { history.push(`/@${status!.getIn(['account', 'acct'])}`); }; const handleHotkeyToggleHidden = () => { handleToggleHidden(status!); }; const handleHotkeyToggleSensitive = () => { handleToggleMediaVisibility(); }; const handleMoveUp = (id: string) => { if (id === status?.id) { _selectChild(ancestorsIds.size - 1); } else { let index = ImmutableList(ancestorsIds).indexOf(id); if (index === -1) { index = ImmutableList(descendantsIds).indexOf(id); _selectChild(ancestorsIds.size + index); } else { _selectChild(index - 1); } } }; const handleMoveDown = (id: string) => { if (id === status?.id) { _selectChild(ancestorsIds.size + 1); } else { let index = ImmutableList(ancestorsIds).indexOf(id); if (index === -1) { index = ImmutableList(descendantsIds).indexOf(id); _selectChild(ancestorsIds.size + index + 2); } else { _selectChild(index + 1); } } }; const _selectChild = (index: number) => { scroller.current?.scrollIntoView({ index, behavior: 'smooth', done: () => { const element = document.querySelector(`#thread [data-index="${index}"] .focusable`); if (element) { element.focus(); } }, }); }; const renderTombstone = (id: string) => { return (
); }; const renderStatus = (id: string) => { return ( ); }; const renderPendingStatus = (id: string) => { const idempotencyKey = id.replace(/^末pending-/, ''); return ( ); }; const renderChildren = (list: ImmutableOrderedSet) => { return list.map(id => { if (id.endsWith('-tombstone')) { return renderTombstone(id); } else if (id.startsWith('末pending-')) { return renderPendingStatus(id); } else { return renderStatus(id); } }); }; // Reset media visibility if status changes. useEffect(() => { setShowMedia(defaultMediaVisibility(status, displayMedia)); }, [status?.id]); // Scroll focused status into view when thread updates. useEffect(() => { scroller.current?.scrollToIndex({ index: ancestorsIds.size, offset: -80, }); setImmediate(() => statusRef.current?.querySelector('.detailed-status')?.focus()); }, [props.params.statusId, status?.id, ancestorsIds.size, isLoaded]); const handleRefresh = () => { return fetchData(); }; const handleLoadMore = useCallback(debounce(() => { if (next && status) { dispatch(fetchNext(status.id, next)).then(({ next }) => { setNext(next); }).catch(() => {}); } }, 300, { leading: true }), [next, status]); const handleOpenCompareHistoryModal = (status: StatusEntity) => { dispatch(openModal('COMPARE_HISTORY', { statusId: status.id, })); }; const hasAncestors = ancestorsIds.size > 0; const hasDescendants = descendantsIds.size > 0; if (status?.event) { return ( ); } if (!status && isLoaded) { return ( ); } else if (!status) { return ( ); } type HotkeyHandlers = { [key: string]: (keyEvent?: KeyboardEvent) => void }; const handlers: HotkeyHandlers = { moveUp: handleHotkeyMoveUp, moveDown: handleHotkeyMoveDown, reply: handleHotkeyReply, favourite: handleHotkeyFavourite, boost: handleHotkeyBoost, mention: handleHotkeyMention, openProfile: handleHotkeyOpenProfile, toggleHidden: handleHotkeyToggleHidden, toggleSensitive: handleHotkeyToggleSensitive, openMedia: handleHotkeyOpenMedia, react: handleHotkeyReact, }; const username = String(status.getIn(['account', 'acct'])); const titleMessage = status.visibility === 'direct' ? messages.titleDirect : messages.title; const focusedStatus = (
{inReview ? ( ) : null}
{hasDescendants && (
)}
); const children: JSX.Element[] = []; if (hasAncestors) { children.push(...renderChildren(ancestorsIds).toArray()); } children.push(focusedStatus); if (hasDescendants) { children.push(...renderChildren(descendantsIds).toArray()); } return (
} initialTopMostItemIndex={ancestorsIds.size} > {children}
{!me && }
); }; export default Thread;