import type { FC } from 'react'; import { useEffect, useState } from 'react'; import { FormattedDate, FormattedMessage } from 'react-intl'; import { dismissAnnouncement } from '@/mastodon/actions/announcements'; import type { ApiAnnouncementJSON } from '@/mastodon/api_types/announcements'; import { AnimateEmojiProvider } from '@/mastodon/components/emoji/context'; import { EmojiHTML } from '@/mastodon/components/emoji/html'; import { useAppDispatch } from '@/mastodon/store'; import { ReactionsBar } from './reactions'; export interface IAnnouncement extends ApiAnnouncementJSON { contentHtml: string; } interface AnnouncementProps { announcement: IAnnouncement; active?: boolean; } export const Announcement: FC = ({ announcement, active, }) => { const { read, id } = announcement; // Dismiss announcement when it becomes active. const dispatch = useAppDispatch(); useEffect(() => { if (active && !read) { dispatch(dismissAnnouncement(id)); } }, [active, id, dispatch, read]); // But visually show the announcement as read only when it goes out of view. const [isVisuallyRead, setIsVisuallyRead] = useState(read); const [previousActive, setPreviousActive] = useState(active); if (active !== previousActive) { setPreviousActive(active); // This marks the announcement as read in the UI only after it // went from active to inactive. if (!active && isVisuallyRead !== read) { setIsVisuallyRead(read); } } return ( {' ยท '} {!isVisuallyRead && } ); }; const Timestamp: FC> = ({ announcement, }) => { const startsAt = announcement.starts_at && new Date(announcement.starts_at); const endsAt = announcement.ends_at && new Date(announcement.ends_at); const now = new Date(); const hasTimeRange = startsAt && endsAt; const skipTime = announcement.all_day; if (hasTimeRange) { const skipYear = startsAt.getFullYear() === endsAt.getFullYear() && endsAt.getFullYear() === now.getFullYear(); const skipEndDate = startsAt.getDate() === endsAt.getDate() && startsAt.getMonth() === endsAt.getMonth() && startsAt.getFullYear() === endsAt.getFullYear(); return ( <> {' '} -{' '} ); } const publishedAt = new Date(announcement.published_at); return ( ); };