Fix: correctly dismisses announcement when viewed (#36750)
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import type { FC } from 'react';
|
import type { FC } from 'react';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { FormattedDate, FormattedMessage } from 'react-intl';
|
import { FormattedDate, FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
import { dismissAnnouncement } from '@/mastodon/actions/announcements';
|
||||||
import type { ApiAnnouncementJSON } from '@/mastodon/api_types/announcements';
|
import type { ApiAnnouncementJSON } from '@/mastodon/api_types/announcements';
|
||||||
import { AnimateEmojiProvider } from '@/mastodon/components/emoji/context';
|
import { AnimateEmojiProvider } from '@/mastodon/components/emoji/context';
|
||||||
import { EmojiHTML } from '@/mastodon/components/emoji/html';
|
import { EmojiHTML } from '@/mastodon/components/emoji/html';
|
||||||
|
import { useAppDispatch } from '@/mastodon/store';
|
||||||
|
|
||||||
import { ReactionsBar } from './reactions';
|
import { ReactionsBar } from './reactions';
|
||||||
|
|
||||||
@@ -22,13 +24,23 @@ export const Announcement: FC<AnnouncementProps> = ({
|
|||||||
announcement,
|
announcement,
|
||||||
selected,
|
selected,
|
||||||
}) => {
|
}) => {
|
||||||
const [unread, setUnread] = useState(!announcement.read);
|
const { read, id } = announcement;
|
||||||
|
|
||||||
|
// Dismiss announcement when it becomes active.
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only update `unread` marker once the announcement is out of view
|
if (selected && !read) {
|
||||||
if (!selected && unread !== !announcement.read) {
|
dispatch(dismissAnnouncement(id));
|
||||||
setUnread(!announcement.read);
|
|
||||||
}
|
}
|
||||||
}, [announcement.read, selected, unread]);
|
}, [selected, id, dispatch, read]);
|
||||||
|
|
||||||
|
// But visually show the announcement as read only when it goes out of view.
|
||||||
|
const [unread, setUnread] = useState(!read);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!selected && unread !== !read) {
|
||||||
|
setUnread(!read);
|
||||||
|
}
|
||||||
|
}, [selected, unread, read]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AnimateEmojiProvider className='announcements__item'>
|
<AnimateEmojiProvider className='announcements__item'>
|
||||||
|
|||||||
Reference in New Issue
Block a user