2023-05-23 11:15:17 -04:00
import PropTypes from 'prop-types' ;
2023-05-23 04:52:27 -04:00
import { PureComponent } from 'react' ;
2023-05-23 11:15:17 -04:00
2023-06-25 23:26:41 -04:00
import { FormattedMessage , defineMessages , injectIntl } from 'react-intl' ;
2023-05-23 11:15:17 -04:00
import { Link , withRouter } from 'react-router-dom' ;
2022-10-04 14:13:23 -04:00
import { connect } from 'react-redux' ;
2023-05-23 11:15:17 -04:00
2024-01-12 05:31:24 -05:00
import SearchIcon from '@material-symbols/svg-600/outlined/search.svg?react' ;
2023-10-24 13:45:08 -04:00
2022-12-06 18:25:40 -05:00
import { openModal } from 'mastodon/actions/modal' ;
2023-06-22 06:10:49 -04:00
import { fetchServer } from 'mastodon/actions/server' ;
2023-05-23 11:15:17 -04:00
import { Avatar } from 'mastodon/components/avatar' ;
2023-06-25 23:26:41 -04:00
import { Icon } from 'mastodon/components/icon' ;
2023-05-23 11:15:17 -04:00
import { WordmarkLogo , SymbolLogo } from 'mastodon/components/logo' ;
2023-08-03 10:43:15 -04:00
import { registrationsOpen , me , sso _redirect } from 'mastodon/initial_state' ;
2022-10-04 14:13:23 -04:00
const Account = connect ( state => ( {
account : state . getIn ( [ 'accounts' , me ] ) ,
} ) ) ( ( { account } ) => (
2022-11-13 15:10:20 -05:00
< Link to = { ` /@ ${ account . get ( 'acct' ) } ` } title = { account . get ( 'acct' ) } >
2022-10-04 14:13:23 -04:00
< Avatar account = { account } size = { 35 } / >
2022-11-13 15:10:20 -05:00
< / Link >
2022-10-04 14:13:23 -04:00
) ) ;
2023-06-25 23:26:41 -04:00
const messages = defineMessages ( {
search : { id : 'navigation_bar.search' , defaultMessage : 'Search' } ,
} ) ;
2023-05-23 09:17:09 -04:00
const mapStateToProps = ( state ) => ( {
2023-05-24 03:49:26 -04:00
signupUrl : state . getIn ( [ 'server' , 'server' , 'registrations' , 'url' ] , null ) || '/auth/sign_up' ,
2023-05-23 09:17:09 -04:00
} ) ;
2022-12-06 18:25:40 -05:00
const mapDispatchToProps = ( dispatch ) => ( {
openClosedRegistrationsModal ( ) {
2023-05-25 09:42:37 -04:00
dispatch ( openModal ( { modalType : 'CLOSED_REGISTRATIONS' } ) ) ;
2022-12-06 18:25:40 -05:00
} ,
2023-06-22 06:10:49 -04:00
dispatchServer ( ) {
dispatch ( fetchServer ( ) ) ;
}
2022-12-06 18:25:40 -05:00
} ) ;
2023-05-23 04:52:27 -04:00
class Header extends PureComponent {
2022-10-04 14:13:23 -04:00
static contextTypes = {
identity : PropTypes . object ,
} ;
2022-10-23 09:58:24 -04:00
static propTypes = {
2022-12-06 18:25:40 -05:00
openClosedRegistrationsModal : PropTypes . func ,
2022-10-23 09:58:24 -04:00
location : PropTypes . object ,
2023-05-23 09:17:09 -04:00
signupUrl : PropTypes . string . isRequired ,
2023-06-25 23:26:41 -04:00
dispatchServer : PropTypes . func ,
intl : PropTypes . object . isRequired ,
2022-10-23 09:58:24 -04:00
} ;
2023-06-22 06:10:49 -04:00
componentDidMount ( ) {
const { dispatchServer } = this . props ;
dispatchServer ( ) ;
}
2022-10-04 14:13:23 -04:00
render ( ) {
const { signedIn } = this . context . identity ;
2023-06-25 23:26:41 -04:00
const { location , openClosedRegistrationsModal , signupUrl , intl } = this . props ;
2022-10-04 14:13:23 -04:00
let content ;
if ( signedIn ) {
2022-10-23 09:58:24 -04:00
content = (
< >
2023-10-24 13:45:08 -04:00
{ location . pathname !== '/search' && < Link to = '/search' className = 'button button-secondary' aria - label = { intl . formatMessage ( messages . search ) } > < Icon id = 'search' icon = { SearchIcon } / > < / Link > }
2023-06-25 23:26:41 -04:00
{ location . pathname !== '/publish' && < Link to = '/publish' className = 'button button-secondary' > < FormattedMessage id = 'compose_form.publish_form' defaultMessage = 'New post' / > < / Link > }
2022-10-23 09:58:24 -04:00
< Account / >
< / >
) ;
2022-10-04 14:13:23 -04:00
} else {
2022-12-06 18:25:40 -05:00
2023-08-03 10:43:15 -04:00
if ( sso _redirect ) {
content = (
2023-10-09 07:38:29 -04:00
< a href = { sso _redirect } data - method = 'post' className = 'button button--block button-tertiary' > < FormattedMessage id = 'sign_in_banner.sso_redirect' defaultMessage = 'Login or Register' / > < / a >
) ;
2022-12-06 18:25:40 -05:00
} else {
2023-08-03 10:43:15 -04:00
let signupButton ;
if ( registrationsOpen ) {
signupButton = (
< a href = { signupUrl } className = 'button' >
< FormattedMessage id = 'sign_in_banner.create_account' defaultMessage = 'Create account' / >
< / a >
) ;
} else {
signupButton = (
< button className = 'button' onClick = { openClosedRegistrationsModal } >
< FormattedMessage id = 'sign_in_banner.create_account' defaultMessage = 'Create account' / >
< / button >
) ;
}
content = (
< >
{ signupButton }
< a href = '/auth/sign_in' className = 'button button-tertiary' > < FormattedMessage id = 'sign_in_banner.sign_in' defaultMessage = 'Login' / > < / a >
< / >
2022-12-06 18:25:40 -05:00
) ;
}
2022-10-04 14:13:23 -04:00
}
return (
< div className = 'ui__header' >
2023-04-28 04:00:33 -04:00
< Link to = '/' className = 'ui__header__logo' >
< WordmarkLogo / >
< SymbolLogo / >
< / Link >
2022-10-04 14:13:23 -04:00
< div className = 'ui__header__links' >
{ content }
< / div >
< / div >
) ;
}
}
2023-03-23 22:17:53 -04:00
2023-06-25 23:26:41 -04:00
export default injectIntl ( withRouter ( connect ( mapStateToProps , mapDispatchToProps ) ( Header ) ) ) ;