/** @jsx createElement */ import { createElement, Component, Fragment } from 'preact'; import { useSelector } from 'react-redux'; import { FormattedMessage, defineMessages, IntlShape, useIntl } from 'react-intl'; import b from 'bem-react-helper'; import { User, AuthProvider, Sorting, Theme, PostInfo } from '@app/common/types'; import { IS_STORAGE_AVAILABLE, IS_THIRD_PARTY } from '@app/common/constants'; import { requestDeletion } from '@app/utils/email'; import postMessage from '@app/utils/postMessage'; import { getHandleClickProps } from '@app/common/accessibility'; import { StoreState } from '@app/store'; import { ProviderState } from '@app/store/provider/reducers'; import { Dropdown, DropdownItem } from '@app/components/dropdown'; import { Button } from '@app/components/button'; import Auth from '@app/components/auth'; import useTheme from '@app/hooks/useTheme'; import { StaticStore } from '@app/common/static_store'; export interface OwnProps { user: User | null; hiddenUsers: StoreState['hiddenUsers']; isCommentsDisabled: boolean; postInfo: PostInfo; onSortChange(s: Sorting): Promise; onSignIn(p: AuthProvider): Promise; onSignOut(): Promise; onCommentsChangeReadOnlyMode(readOnly: boolean): Promise; onBlockedUsersShow(): void; onBlockedUsersHide(): void; } export interface Props extends OwnProps { intl: IntlShape; theme: Theme; providers: AuthProvider['name'][]; provider: ProviderState; sort: Sorting; } interface State { isBlockedVisible: boolean; anonymousUsernameInputValue: string; sortSelectFocused: boolean; } export class AuthPanel extends Component { state = { isBlockedVisible: false, anonymousUsernameInputValue: 'anon', sortSelectFocused: false, }; onSortChange = (e: Event) => { const { value } = e.target as HTMLOptionElement; this.props.onSortChange(value as Sorting); }; onSortFocus = () => { this.setState({ sortSelectFocused: true }); }; onSortBlur = (e: Event) => { this.setState({ sortSelectFocused: false }); this.onSortChange(e); }; toggleBlockedVisibility = () => { if (!this.state.isBlockedVisible) { if (this.props.onBlockedUsersShow) this.props.onBlockedUsersShow(); } else if (this.props.onBlockedUsersHide) this.props.onBlockedUsersHide(); this.setState({ isBlockedVisible: !this.state.isBlockedVisible }); }; toggleCommentsAvailability = () => { this.props.onCommentsChangeReadOnlyMode(!this.props.isCommentsDisabled); }; toggleUserInfoVisibility = () => { const { user } = this.props; if (window.parent && user) { postMessage({ isUserInfoShown: true, user }); } }; renderAuthorized = (user: User) => { const { onSignOut, theme } = this.props; const isUserAnonymous = user && user.id.substr(0, 10) === 'anonymous_'; return ( {' '}
{user.id}
{!isUserAnonymous && ( )}
{' '}
); }; renderThirdPartyWarning = () => { if (IS_STORAGE_AVAILABLE || !IS_THIRD_PARTY) return null; return ( ); }; renderCookiesWarning = () => { if (IS_STORAGE_AVAILABLE || IS_THIRD_PARTY) return null; return (
); }; renderSettingsLabel = () => { return ( ); }; renderReadOnlySwitch = () => { const { isCommentsDisabled } = this.props; return ( ); }; renderSort = () => { const { sort } = this.props; const { sortSelectFocused } = this.state; const sortArray = getSortArray(sort, this.props.intl); return ( {' '} {sortArray.find(x => 'selected' in x && x.selected!)!.label} ); }; render({ user, postInfo, theme }: Props, { isBlockedVisible }: State) { const { read_only } = postInfo; const isAdmin = user && user.admin; const isSettingsLabelVisible = Object.keys(this.props.hiddenUsers).length > 0 || isAdmin || isBlockedVisible; return (
{user ? this.renderAuthorized(user) : read_only && }
{this.renderThirdPartyWarning()} {this.renderCookiesWarning()}
{isSettingsLabelVisible && this.renderSettingsLabel()} {isSettingsLabelVisible && ' • '} {isAdmin && this.renderReadOnlySwitch()} {isAdmin && ' • '} {!isAdmin && read_only && ( )} {this.renderSort()}
); } } const sortMessages = defineMessages({ best: { id: 'commentsSort.best', defaultMessage: 'Best', }, worst: { id: 'commentsSort.worst', defaultMessage: 'Worst', }, newest: { id: 'commentsSort.newest', defaultMessage: 'Newest', }, oldest: { id: 'commentsSort.oldest', defaultMessage: 'Oldest', }, recentlyUpdated: { id: 'commentsSort.recently-updated', defaultMessage: 'Recently updated', }, leastRecentlyUpdated: { id: 'commentsSort.least-recently-updated', defaultMessage: 'Least recently updated', }, mostControversial: { id: 'commentsSort.most-controversial', defaultMessage: 'Most controversial', }, leastControversial: { id: 'commentsSort.least-controversial', defaultMessage: 'Least controversial', }, }); function getSortArray(currentSort: Sorting, intl: IntlShape) { const sortArray: { value: Sorting; label: string; selected?: boolean; }[] = [ { value: '-score', label: intl.formatMessage(sortMessages.best), }, { value: '+score', label: intl.formatMessage(sortMessages.worst), }, { value: '-time', label: intl.formatMessage(sortMessages.newest), }, { value: '+time', label: intl.formatMessage(sortMessages.oldest), }, { value: '-active', label: intl.formatMessage(sortMessages.recentlyUpdated), }, { value: '+active', label: intl.formatMessage(sortMessages.leastRecentlyUpdated), }, { value: '-controversy', label: intl.formatMessage(sortMessages.mostControversial), }, { value: '+controversy', label: intl.formatMessage(sortMessages.leastControversial), }, ]; return sortArray.map(sort => { if (sort.value === currentSort) { sort.selected = true; } return sort; }); } export default function AuthPanelConnected(props: OwnProps) { const intl = useIntl(); const theme = useTheme(); const provider = useSelector(state => state.provider); const sort = useSelector(state => state.comments.sort); return ( ); }