diff --git a/app/javascript/flavours/glitch/components/status/attachments.tsx b/app/javascript/flavours/glitch/components/status/attachments.tsx new file mode 100644 index 0000000000..238e2a4ce2 --- /dev/null +++ b/app/javascript/flavours/glitch/components/status/attachments.tsx @@ -0,0 +1,280 @@ +import { lazy, Suspense, useCallback, useState } from 'react'; + +import { openModal } from '@/flavours/glitch/actions/modal'; +import type { DeployPictureInPictureCallback } from '@/flavours/glitch/actions/picture_in_picture'; +import { deployPictureInPicture } from '@/flavours/glitch/actions/picture_in_picture'; +import { CollectionPreviewCard } from '@/flavours/glitch/features/collections/components/collection_preview_card'; +import Card from '@/flavours/glitch/features/status/components/card'; +import { displayMedia } from '@/flavours/glitch/initial_state'; +import type { + MediaAttachment, + MediaAttachmentShape, +} from '@/flavours/glitch/models/status'; +import { isMediaAttachmentOfType } from '@/flavours/glitch/models/status'; +import { + selectExpandedStatus, + selectMediaMatchFilters, + selectPictureInPicture, +} from '@/flavours/glitch/selectors/statuses'; +import { useAppDispatch, useAppSelector } from '@/flavours/glitch/store'; +import { compareUrls } from '@/flavours/glitch/utils/compare_urls'; + +import { PictureInPicturePlaceholder } from '../picture_in_picture_placeholder'; + +export const StatusAttachments: React.FC<{ + statusId: string; + contextType?: string; +}> = ({ statusId, contextType }) => { + // Selectors + const status = useAppSelector((state) => + selectExpandedStatus(state, statusId), + ); + + if (!status) { + return null; + } + + const attachment = status.media_attachments[0]; + if (attachment) { + return ( + + ); + } + + // Don't display the card or collection if this is a quote. + if (status.quote) { + return null; + } + + const card = status.card; + const collection = card?.url + ? status.tagged_collections.find(({ url }) => compareUrls(url, card.url)) + : status.tagged_collections[0]; + if (card && !collection) { + return ( + + ); + } + + if (collection) { + return ; + } + + return null; +}; + +type TMediaGallery = React.ComponentClass< + { + media: Immutable.List; + height: number; + onOpenMedia: (index: number) => void; + onToggleVisibility?: () => void; + sensitive?: boolean; + lang?: string; + standalone?: boolean; + visible?: boolean; + autoplay?: boolean; + matchedFilters?: (string | null | undefined)[]; + cacheWidth?: () => void; + defaultWidth?: number; + }, + { visible: boolean; width?: number } +>; + +const MediaGallery = lazy( + () => import('@/flavours/glitch/components/media_gallery'), +); +const Audio = lazy(() => import('@/flavours/glitch/features/audio')); +const Video = lazy(() => import('@/flavours/glitch/features/video')); + +const MediaAttachments: React.FC<{ + statusId: string; + accountId: string; + contextType?: string; + sensitive: boolean; + language: string; + attachment: MediaAttachmentShape; + restAttachments: MediaAttachmentShape[]; + defaultPosterUrl: string; +}> = ({ + statusId, + accountId, + contextType, + sensitive, + language, + attachment, + defaultPosterUrl, +}) => { + const description = + attachment.translation?.description ?? attachment.description; + + const immutableAttachments = useAppSelector( + (state) => + state.statuses.getIn( + statusId, + 'media_attachments', + ) as Immutable.List, + ); + const mediaFilters = useAppSelector((state) => + selectMediaMatchFilters(state, { statusId, contextType }), + ); + const pictureInPicture = useAppSelector((state) => + selectPictureInPicture(state, statusId), + ); + + const [showMedia, setShowMedia] = useState( + () => + mediaFilters.length === 0 && + ((displayMedia !== 'hide_all' && !sensitive) || + displayMedia === 'show_all'), + ); + + const dispatch = useAppDispatch(); + const handleToggleMediaVisibility = useCallback(() => { + setShowMedia((prev) => !prev); + }, []); + const handleOpenMedia = useCallback( + (index: number) => { + dispatch( + openModal({ + modalType: 'MEDIA', + modalProps: { statusId, media: attachment, index, lang: language }, + }), + ); + }, + [attachment, dispatch, language, statusId], + ); + const handleOpenVideo = useCallback( + (options: { + startTime: number; + autoPlay: boolean; + defaultVolume: number; + }) => { + dispatch( + openModal({ + modalType: 'VIDEO', + modalProps: { + statusId, + options, + media: attachment, + lang: language, + }, + }), + ); + }, + [attachment, dispatch, language, statusId], + ); + const handleDeployPictureInPicture: DeployPictureInPictureCallback = + useCallback( + (type, props) => { + if (!accountId || !pictureInPicture.available) { + return; + } + void dispatch( + deployPictureInPicture({ + statusId, + accountId, + playerType: type, + props, + }), + ); + }, + [dispatch, pictureInPicture.available, accountId, statusId], + ); + + let aspectRatio = '3 / 2'; + if ( + isMediaAttachmentOfType(attachment, 'image') || + isMediaAttachmentOfType(attachment, 'video') || + isMediaAttachmentOfType(attachment, 'gifv') + ) { + aspectRatio = `${attachment.meta.original.width} / ${attachment.meta.original.height}`; + } else if (isMediaAttachmentOfType(attachment, 'audio')) { + aspectRatio = '16 / 9'; + } + + if (pictureInPicture.inUse) { + return ; + } + + if (isMediaAttachmentOfType(attachment, 'audio')) { + const { colors, original } = attachment.meta; + return ( + } + > + + ); + } + + if (isMediaAttachmentOfType(attachment, 'video')) { + const { original } = attachment.meta; + return ( + } + > + + ); + } + + return ( + } + > + + + ); +};