2023-11-30 06:45:54 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-call,
|
|
|
|
@typescript-eslint/no-unsafe-return,
|
|
|
|
@typescript-eslint/no-unsafe-assignment,
|
|
|
|
@typescript-eslint/no-unsafe-member-access
|
|
|
|
-- the settings store is not yet typed */
|
2023-07-08 05:12:20 -04:00
|
|
|
import type { PropsWithChildren } from 'react';
|
2023-11-30 06:45:54 -05:00
|
|
|
import { useCallback, useState, useEffect } from 'react';
|
2023-07-08 05:12:20 -04:00
|
|
|
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
2024-01-12 15:16:48 -05:00
|
|
|
import CloseIcon from 'mastodon/../material-icons/400-24px/close.svg?react';
|
2023-11-30 06:45:54 -05:00
|
|
|
import { changeSetting } from 'mastodon/actions/settings';
|
2023-07-08 05:12:20 -04:00
|
|
|
import { bannerSettings } from 'mastodon/settings';
|
2023-11-30 06:45:54 -05:00
|
|
|
import { useAppSelector, useAppDispatch } from 'mastodon/store';
|
2023-07-08 05:12:20 -04:00
|
|
|
|
|
|
|
import { IconButton } from './icon_button';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
dismiss: { id: 'dismissable_banner.dismiss', defaultMessage: 'Dismiss' },
|
|
|
|
});
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DismissableBanner: React.FC<PropsWithChildren<Props>> = ({
|
|
|
|
id,
|
|
|
|
children,
|
|
|
|
}) => {
|
2023-11-30 06:45:54 -05:00
|
|
|
const dismissed = useAppSelector((state) =>
|
|
|
|
state.settings.getIn(['dismissed_banners', id], false),
|
|
|
|
);
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
|
|
|
const [visible, setVisible] = useState(!bannerSettings.get(id) && !dismissed);
|
2023-07-08 05:12:20 -04:00
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const handleDismiss = useCallback(() => {
|
|
|
|
setVisible(false);
|
|
|
|
bannerSettings.set(id, true);
|
2023-11-30 06:45:54 -05:00
|
|
|
dispatch(changeSetting(['dismissed_banners', id], true));
|
|
|
|
}, [id, dispatch]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!visible && !dismissed) {
|
|
|
|
dispatch(changeSetting(['dismissed_banners', id], true));
|
|
|
|
}
|
|
|
|
}, [id, dispatch, visible, dismissed]);
|
2023-07-08 05:12:20 -04:00
|
|
|
|
|
|
|
if (!visible) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='dismissable-banner'>
|
|
|
|
<div className='dismissable-banner__action'>
|
|
|
|
<IconButton
|
|
|
|
icon='times'
|
2023-10-24 13:45:08 -04:00
|
|
|
iconComponent={CloseIcon}
|
2023-07-08 05:12:20 -04:00
|
|
|
title={intl.formatMessage(messages.dismiss)}
|
|
|
|
onClick={handleDismiss}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-09-15 13:53:28 -04:00
|
|
|
|
|
|
|
<div className='dismissable-banner__message'>{children}</div>
|
2023-07-08 05:12:20 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|