Status media attachments refactor (#39601)

This commit is contained in:
Echo authored and GitHub committed 2026-06-25 16:34:11 +00:00
1 parent e4e19ba264
commit 3f98bee57f
11 files changed
+494 -88

No files matched your search

@@ -6,6 +6,7 @@ import classNames from 'classnames';
import { useSpring, animated, config } from '@react-spring/web';
import type { DeployPictureInPictureCallback } from '@/mastodon/actions/picture_in_picture';
import DownloadIcon from '@/material-icons/400-24px/download.svg?react';
import Forward5Icon from '@/material-icons/400-24px/forward_5-fill.svg?react';
import PauseIcon from '@/material-icons/400-24px/pause-fill.svg?react';
@@ -68,19 +69,7 @@ export const Audio: React.FC<{
startPlaying?: boolean;
startVolume?: number;
startMuted?: boolean;
deployPictureInPicture?: (
type: string,
mediaProps: {
src: string;
muted: boolean;
volume: number;
currentTime: number;
poster?: string;
backgroundColor: string;
foregroundColor: string;
accentColor: string;
},
) => void;
deployPictureInPicture?: DeployPictureInPictureCallback;
matchedFilters?: string[];
}> = ({
src,
@@ -1,9 +1,11 @@
import { useCallback, useId, useState } from 'react';
import { useCallback, useId, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import { isMap } from 'immutable';
import punycode from 'punycode/';
import DescriptionIcon from '@/material-icons/400-24px/description-fill.svg?react';
@@ -14,7 +16,7 @@ import { Icon } from 'mastodon/components/icon';
import { MoreFromAuthor } from 'mastodon/components/more_from_author';
import { RelativeTimestamp } from 'mastodon/components/relative_timestamp';
import { displayMedia, useBlurhash } from 'mastodon/initial_state';
import type { Card as CardType } from 'mastodon/models/status';
import type { CardShape, Card as CardType } from 'mastodon/models/status';
const IDNA_PREFIX = 'xn--';
@@ -66,27 +68,26 @@ const handleIframeUrl = (html: string, url: string, providerName: string) => {
const hideAllMedia = displayMedia === 'hide_all';
interface CardProps {
card: CardType | null;
card: CardType | CardShape | null;
sensitive?: boolean;
}
const CardVideo: React.FC<Pick<CardProps, 'card'>> = ({ card }) => (
const CardVideo: React.FC<{ card: CardShape }> = ({ card }) => (
<div
className='status-card__image status-card-video'
dangerouslySetInnerHTML={{
__html: card
? handleIframeUrl(
card.get('html'),
card.get('url'),
card.get('provider_name'),
)
: '',
__html: handleIframeUrl(card.html, card.url, card.provider_name),
}}
style={{ aspectRatio: '16 / 9' }}
/>
);
const Card: React.FC<CardProps> = ({ card, sensitive }) => {
const Card: React.FC<CardProps> = ({ card: rawCard, sensitive }) => {
const card: CardShape | null = useMemo(
() => (isMap(rawCard) ? (rawCard.toJS() as CardShape) : rawCard),
[rawCard],
);
const [previewLoaded, setPreviewLoaded] = useState(false);
const [embedded, setEmbedded] = useState(false);
const [revealed, setRevealed] = useState(!sensitive && !hideAllMedia);
@@ -116,52 +117,43 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
}
const provider =
card.get('provider_name').length === 0
? decodeIDNA(getHostname(card.get('url')))
: card.get('provider_name');
const interactive = card.get('type') === 'video';
const language = card.get('language') ?? '';
const hasImage = (card.get('image')?.length ?? 0) > 0;
const largeImage =
(hasImage && card.get('width') > card.get('height')) || interactive;
const showAuthor = !!card.getIn(['authors', 0, 'accountId']);
card.provider_name.length === 0
? decodeIDNA(getHostname(card.url))
: card.provider_name;
const interactive = card.type === 'video';
const language = card.language ?? '';
const hasImage = (card.image?.length ?? 0) > 0;
const largeImage = (hasImage && card.width > card.height) || interactive;
const author = card.authors.at(0)?.accountId;
const description = (
<div className='status-card__content' dir='auto'>
<span className='status-card__host'>
<span lang={language}>{provider}</span>
{card.get('published_at') && (
{card.published_at && (
<>
{' '}
·{' '}
<RelativeTimestamp
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
timestamp={card.get('published_at')!}
/>
· <RelativeTimestamp timestamp={card.published_at} />
</>
)}
</span>
<strong
className='status-card__title'
title={card.get('title')}
lang={language}
>
{card.get('title')}
<strong className='status-card__title' title={card.title} lang={language}>
{card.title}
</strong>
{!showAuthor &&
(card.get('author_name').length > 0 ? (
{!author &&
(card.author_name.length > 0 ? (
<span className='status-card__author'>
<FormattedMessage
id='link_preview.author'
defaultMessage='By {name}'
values={{ name: <strong>{card.get('author_name')}</strong> }}
values={{ name: <strong>{card.author_name}</strong> }}
/>
</span>
) : (
<span className='status-card__description' lang={language}>
{card.get('description')}
{card.description}
</span>
))}
</div>
@@ -172,7 +164,7 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
aspectRatio: '1',
};
if (largeImage && card.get('type') === 'video') {
if (largeImage && card.type === 'video') {
thumbnailStyle.aspectRatio = `16 / 9`;
} else if (largeImage) {
thumbnailStyle.aspectRatio = '1.91 / 1';
@@ -185,15 +177,15 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
className={classNames('status-card__image-preview', {
'status-card__image-preview--hidden': revealed && previewLoaded,
})}
hash={card.get('blurhash')}
hash={card.blurhash}
dummy={!useBlurhash}
/>
);
const thumbnailDescription = card.get('image_description');
const thumbnailDescription = card.image_description;
const thumbnail = (
<img
src={card.get('image') ?? undefined}
src={card.image ?? undefined}
alt={thumbnailDescription}
title={thumbnailDescription}
lang={language}
@@ -251,7 +243,7 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
<Icon id='play' icon={PlayArrowIcon} />
</button>
<a
href={card.get('url')}
href={card.url}
onClick={handleExternalLinkClick}
target='_blank'
rel='noopener'
@@ -271,7 +263,7 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
<div className={classNames('status-card', { expanded: largeImage })}>
{embed}
<a
href={card.get('url')}
href={card.url}
target='_blank'
rel='noopener'
onClick={revealed ? undefined : handleReveal}
@@ -281,7 +273,7 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
</a>
</div>
);
} else if (card.get('image')) {
} else if (card.image) {
embed = (
<div className='status-card__image'>
{canvas}
@@ -300,10 +292,10 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
return (
<>
<a
href={card.get('url')}
href={card.url}
className={classNames('status-card', {
expanded: largeImage,
bottomless: showAuthor,
bottomless: !!author,
})}
target='_blank'
rel='noopener'
@@ -312,11 +304,7 @@ const Card: React.FC<CardProps> = ({ card, sensitive }) => {
{description}
</a>
{showAuthor && (
<MoreFromAuthor
accountId={card.getIn(['authors', 0, 'accountId']) as string}
/>
)}
{author && <MoreFromAuthor accountId={author} />}
</>
);
};
@@ -7,6 +7,7 @@ import classNames from 'classnames';
import { useSpring, animated, config } from '@react-spring/web';
import { throttle } from 'lodash';
import type { DeployPictureInPictureCallback } from '@/mastodon/actions/picture_in_picture';
import Forward5Icon from '@/material-icons/400-24px/forward_5-fill.svg?react';
import FullscreenIcon from '@/material-icons/400-24px/fullscreen.svg?react';
import FullscreenExitIcon from '@/material-icons/400-24px/fullscreen_exit.svg?react';
@@ -174,15 +175,7 @@ export const Video: React.FC<{
alwaysVisible?: boolean;
visible?: boolean;
onToggleVisibility?: () => void;
deployPictureInPicture?: (
type: string,
mediaProps: {
src: string;
muted: boolean;
volume: number;
currentTime: number;
},
) => void;
deployPictureInPicture?: DeployPictureInPictureCallback;
blurhash?: string;
startPlaying?: boolean;
startTime?: number;
@@ -241,7 +234,7 @@ export const Video: React.FC<{
(c: HTMLVideoElement | null) => {
if (videoRef.current && !videoRef.current.paused && c === null) {
deployPictureInPicture?.('video', {
src: src,
src,
currentTime: videoRef.current.currentTime,
muted: videoRef.current.muted,
volume: videoRef.current.volume,