import { FormattedMessage } from 'react-intl';
import { createSelector } from '@reduxjs/toolkit';
import { animated, useSpring } from '@react-spring/web';
import { me } from 'mastodon/initial_state';
import { useAppSelector } from 'mastodon/store';
import type { RootState } from 'mastodon/store';
import { HASHTAG_PATTERN_REGEX } from 'mastodon/utils/hashtags';
import { MENTION_PATTERN_REGEX } from 'mastodon/utils/mentions';
const selector = createSelector(
(state: RootState) => state.compose.get('privacy') as string,
(state: RootState) => !!state.accounts.getIn([me, 'locked']),
(state: RootState) => state.compose.get('text') as string,
(state: RootState) => state.compose.get('searchability') as string,
(state: RootState) => state.compose.get('limited_scope') as string,
(privacy, locked, text, searchability, limited_scope) => ({
needsLockWarning: privacy === 'private' && !locked,
hashtagWarning:
!['public', 'public_unlisted', 'login'].includes(privacy) &&
(privacy !== 'unlisted' || searchability !== 'public') &&
HASHTAG_PATTERN_REGEX.test(text),
directMessageWarning: privacy === 'direct',
searchabilityWarning: searchability === 'limited',
mentionWarning:
['mutual', 'circle', 'limited'].includes(privacy) &&
MENTION_PATTERN_REGEX.test(text),
limitedPostWarning:
['mutual', 'circle'].includes(privacy) && !limited_scope,
}),
);
export const Warning = () => {
const {
needsLockWarning,
hashtagWarning,
directMessageWarning,
searchabilityWarning,
mentionWarning,
limitedPostWarning,
} = useAppSelector(selector);
if (needsLockWarning) {
return (
),
}}
/>
);
}
if (hashtagWarning) {
return (
);
}
if (directMessageWarning) {
return (
{' '}
);
}
if (searchabilityWarning) {
return (
);
}
if (mentionWarning) {
return (
);
}
if (limitedPostWarning) {
return (
);
}
return null;
};
export const WarningMessage: React.FC = ({
children,
}) => {
const styles = useSpring({
from: {
opacity: 0,
transform: 'scale(0.85, 0.75)',
},
to: {
opacity: 1,
transform: 'scale(1, 1)',
},
});
return (
{children}
);
};