2023-11-30 22:45:54 +11: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 19:12:20 +10:00
|
|
|
import type { PropsWithChildren } from 'react';
|
2023-11-30 22:45:54 +11:00
|
|
|
import { useCallback, useState, useEffect } from 'react';
|
2023-07-08 19:12:20 +10:00
|
|
|
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
2023-11-30 22:45:54 +11:00
|
|
|
import { changeSetting } from 'mastodon/actions/settings';
|
2023-07-08 19:12:20 +10:00
|
|
|
import { bannerSettings } from 'mastodon/settings';
|
2023-11-30 22:45:54 +11:00
|
|
|
import { useAppSelector, useAppDispatch } from 'mastodon/store';
|
2023-07-08 19:12:20 +10: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 22:45:54 +11: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 19:12:20 +10:00
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const handleDismiss = useCallback(() => {
|
|
|
|
setVisible(false);
|
|
|
|
bannerSettings.set(id, true);
|
2023-11-30 22:45:54 +11: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 19:12:20 +10:00
|
|
|
|
|
|
|
if (!visible) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='dismissable-banner'>
|
|
|
|
<div className='dismissable-banner__action'>
|
|
|
|
<IconButton
|
|
|
|
icon='times'
|
|
|
|
title={intl.formatMessage(messages.dismiss)}
|
|
|
|
onClick={handleDismiss}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-09-16 03:53:28 +10:00
|
|
|
|
|
|
|
<div className='dismissable-banner__message'>{children}</div>
|
2023-07-08 19:12:20 +10:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|