Hide media by default in notification requests (#29572)

pull/29577/head
Claire 2024-03-13 17:47:48 +01:00 committed by GitHub
parent 71e5f0f48c
commit a32a126cac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 29 deletions

View File

@ -22,6 +22,7 @@ import Card from '../features/status/components/card';
// to use the progress bar to show download progress // to use the progress bar to show download progress
import Bundle from '../features/ui/components/bundle'; import Bundle from '../features/ui/components/bundle';
import { MediaGallery, Video, Audio } from '../features/ui/util/async-components'; import { MediaGallery, Video, Audio } from '../features/ui/util/async-components';
import { SensitiveMediaContext } from '../features/ui/util/sensitive_media_context';
import { displayMedia } from '../initial_state'; import { displayMedia } from '../initial_state';
import { Avatar } from './avatar'; import { Avatar } from './avatar';
@ -78,6 +79,8 @@ const messages = defineMessages({
class Status extends ImmutablePureComponent { class Status extends ImmutablePureComponent {
static contextType = SensitiveMediaContext;
static propTypes = { static propTypes = {
status: ImmutablePropTypes.map, status: ImmutablePropTypes.map,
account: ImmutablePropTypes.record, account: ImmutablePropTypes.record,
@ -133,19 +136,21 @@ class Status extends ImmutablePureComponent {
]; ];
state = { state = {
showMedia: defaultMediaVisibility(this.props.status), showMedia: defaultMediaVisibility(this.props.status) && !(this.context?.hideMediaByDefault),
statusId: undefined,
forceFilter: undefined, forceFilter: undefined,
}; };
static getDerivedStateFromProps(nextProps, prevState) { componentDidUpdate (prevProps) {
if (nextProps.status && nextProps.status.get('id') !== prevState.statusId) { // This will potentially cause a wasteful redraw, but in most cases `Status` components are used
return { // with a `key` directly depending on their `id`, preventing re-use of the component across
showMedia: defaultMediaVisibility(nextProps.status), // different IDs.
statusId: nextProps.status.get('id'), // But just in case this does change, reset the state on status change.
};
} else { if (this.props.status?.get('id') !== prevProps.status?.get('id')) {
return null; this.setState({
showMedia: defaultMediaVisibility(this.props.status) && !(this.context?.hideMediaByDefault),
forceFilter: undefined,
});
} }
} }

View File

@ -15,6 +15,7 @@ import Column from 'mastodon/components/column';
import ColumnHeader from 'mastodon/components/column_header'; import ColumnHeader from 'mastodon/components/column_header';
import { IconButton } from 'mastodon/components/icon_button'; import { IconButton } from 'mastodon/components/icon_button';
import ScrollableList from 'mastodon/components/scrollable_list'; import ScrollableList from 'mastodon/components/scrollable_list';
import { SensitiveMediaContextProvider } from 'mastodon/features/ui/util/sensitive_media_context';
import NotificationContainer from './containers/notification_container'; import NotificationContainer from './containers/notification_container';
@ -106,6 +107,7 @@ export const NotificationRequest = ({ multiColumn, params: { id } }) => {
)} )}
/> />
<SensitiveMediaContextProvider hideMediaByDefault>
<ScrollableList <ScrollableList
scrollKey={`notification_requests/${id}`} scrollKey={`notification_requests/${id}`}
trackScroll={!multiColumn} trackScroll={!multiColumn}
@ -125,6 +127,7 @@ export const NotificationRequest = ({ multiColumn, params: { id } }) => {
/> />
))} ))}
</ScrollableList> </ScrollableList>
</SensitiveMediaContextProvider>
<Helmet> <Helmet>
<title>{columnTitle}</title> <title>{columnTitle}</title>

View File

@ -0,0 +1,28 @@
import { createContext, useContext, useMemo } from 'react';
export const SensitiveMediaContext = createContext<{
hideMediaByDefault: boolean;
}>({
hideMediaByDefault: false,
});
export function useSensitiveMediaContext() {
return useContext(SensitiveMediaContext);
}
type ContextValue = React.ContextType<typeof SensitiveMediaContext>;
export const SensitiveMediaContextProvider: React.FC<
React.PropsWithChildren<{ hideMediaByDefault: boolean }>
> = ({ hideMediaByDefault, children }) => {
const contextValue = useMemo<ContextValue>(
() => ({ hideMediaByDefault }),
[hideMediaByDefault],
);
return (
<SensitiveMediaContext.Provider value={contextValue}>
{children}
</SensitiveMediaContext.Provider>
);
};