column_header.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import PropTypes from 'prop-types';
  2. import { PureComponent, useCallback } from 'react';
  3. import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';
  4. import classNames from 'classnames';
  5. import { withRouter } from 'react-router-dom';
  6. import AddIcon from '@/material-icons/400-24px/add.svg?react';
  7. import ArrowBackIcon from '@/material-icons/400-24px/arrow_back.svg?react';
  8. import ChevronLeftIcon from '@/material-icons/400-24px/chevron_left.svg?react';
  9. import ChevronRightIcon from '@/material-icons/400-24px/chevron_right.svg?react';
  10. import CloseIcon from '@/material-icons/400-24px/close.svg?react';
  11. import TuneIcon from '@/material-icons/400-24px/tune.svg?react';
  12. import { Icon } from 'mastodon/components/icon';
  13. import { ButtonInTabsBar, useColumnsContext } from 'mastodon/features/ui/util/columns_context';
  14. import { WithRouterPropTypes } from 'mastodon/utils/react_router';
  15. import { useAppHistory } from './router';
  16. const messages = defineMessages({
  17. show: { id: 'column_header.show_settings', defaultMessage: 'Show settings' },
  18. hide: { id: 'column_header.hide_settings', defaultMessage: 'Hide settings' },
  19. moveLeft: { id: 'column_header.moveLeft_settings', defaultMessage: 'Move column to the left' },
  20. moveRight: { id: 'column_header.moveRight_settings', defaultMessage: 'Move column to the right' },
  21. });
  22. const BackButton = ({ pinned, show }) => {
  23. const history = useAppHistory();
  24. const { multiColumn } = useColumnsContext();
  25. const handleBackClick = useCallback(() => {
  26. if (history.location?.state?.fromMastodon) {
  27. history.goBack();
  28. } else {
  29. history.push('/');
  30. }
  31. }, [history]);
  32. const showButton = history && !pinned && ((multiColumn && history.location?.state?.fromMastodon) || show);
  33. if(!showButton) return null;
  34. return (<button onClick={handleBackClick} className='column-header__back-button'>
  35. <Icon id='chevron-left' icon={ArrowBackIcon} className='column-back-button__icon' />
  36. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  37. </button>);
  38. };
  39. BackButton.propTypes = {
  40. pinned: PropTypes.bool,
  41. show: PropTypes.bool,
  42. };
  43. class ColumnHeader extends PureComponent {
  44. static contextTypes = {
  45. identity: PropTypes.object,
  46. };
  47. static propTypes = {
  48. intl: PropTypes.object.isRequired,
  49. title: PropTypes.node,
  50. icon: PropTypes.string,
  51. iconComponent: PropTypes.func,
  52. active: PropTypes.bool,
  53. multiColumn: PropTypes.bool,
  54. extraButton: PropTypes.node,
  55. showBackButton: PropTypes.bool,
  56. children: PropTypes.node,
  57. pinned: PropTypes.bool,
  58. placeholder: PropTypes.bool,
  59. onPin: PropTypes.func,
  60. onMove: PropTypes.func,
  61. onClick: PropTypes.func,
  62. appendContent: PropTypes.node,
  63. collapseIssues: PropTypes.bool,
  64. ...WithRouterPropTypes,
  65. };
  66. state = {
  67. collapsed: true,
  68. animating: false,
  69. };
  70. handleToggleClick = (e) => {
  71. e.stopPropagation();
  72. this.setState({ collapsed: !this.state.collapsed, animating: true });
  73. };
  74. handleTitleClick = () => {
  75. this.props.onClick?.();
  76. };
  77. handleMoveLeft = () => {
  78. this.props.onMove(-1);
  79. };
  80. handleMoveRight = () => {
  81. this.props.onMove(1);
  82. };
  83. handleTransitionEnd = () => {
  84. this.setState({ animating: false });
  85. };
  86. handlePin = () => {
  87. if (!this.props.pinned) {
  88. this.props.history.replace('/');
  89. }
  90. this.props.onPin();
  91. };
  92. render () {
  93. const { title, icon, iconComponent, active, children, pinned, multiColumn, extraButton, showBackButton, intl: { formatMessage }, placeholder, appendContent, collapseIssues } = this.props;
  94. const { collapsed, animating } = this.state;
  95. const wrapperClassName = classNames('column-header__wrapper', {
  96. 'active': active,
  97. });
  98. const buttonClassName = classNames('column-header', {
  99. 'active': active,
  100. });
  101. const collapsibleClassName = classNames('column-header__collapsible', {
  102. 'collapsed': collapsed,
  103. 'animating': animating,
  104. });
  105. const collapsibleButtonClassName = classNames('column-header__button', {
  106. 'active': !collapsed,
  107. });
  108. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  109. if (children) {
  110. extraContent = (
  111. <div key='extra-content' className='column-header__collapsible__extra'>
  112. {children}
  113. </div>
  114. );
  115. }
  116. if (multiColumn && pinned) {
  117. pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={this.handlePin}><Icon id='times' icon={CloseIcon} /> <FormattedMessage id='column_header.unpin' defaultMessage='Unpin' /></button>;
  118. moveButtons = (
  119. <div key='move-buttons' className='column-header__setting-arrows'>
  120. <button title={formatMessage(messages.moveLeft)} aria-label={formatMessage(messages.moveLeft)} className='icon-button column-header__setting-btn' onClick={this.handleMoveLeft}><Icon id='chevron-left' icon={ChevronLeftIcon} /></button>
  121. <button title={formatMessage(messages.moveRight)} aria-label={formatMessage(messages.moveRight)} className='icon-button column-header__setting-btn' onClick={this.handleMoveRight}><Icon id='chevron-right' icon={ChevronRightIcon} /></button>
  122. </div>
  123. );
  124. } else if (multiColumn && this.props.onPin) {
  125. pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={this.handlePin}><Icon id='plus' icon={AddIcon} /> <FormattedMessage id='column_header.pin' defaultMessage='Pin' /></button>;
  126. }
  127. backButton = <BackButton pinned={pinned} show={showBackButton} />;
  128. const collapsedContent = [
  129. extraContent,
  130. ];
  131. if (multiColumn) {
  132. collapsedContent.push(pinButton);
  133. collapsedContent.push(moveButtons);
  134. }
  135. if (this.context.identity.signedIn && (children || (multiColumn && this.props.onPin))) {
  136. collapseButton = (
  137. <button
  138. className={collapsibleButtonClassName}
  139. title={formatMessage(collapsed ? messages.show : messages.hide)}
  140. aria-label={formatMessage(collapsed ? messages.show : messages.hide)}
  141. onClick={this.handleToggleClick}
  142. >
  143. <i className='icon-with-badge'>
  144. <Icon id='sliders' icon={TuneIcon} />
  145. {collapseIssues && <i className='icon-with-badge__issue-badge' />}
  146. </i>
  147. </button>
  148. );
  149. }
  150. const hasTitle = (icon || iconComponent) && title;
  151. const component = (
  152. <div className={wrapperClassName}>
  153. <h1 className={buttonClassName}>
  154. {hasTitle && (
  155. <button onClick={this.handleTitleClick}>
  156. <Icon id={icon} icon={iconComponent} className='column-header__icon' />
  157. {title}
  158. </button>
  159. )}
  160. {!hasTitle && backButton}
  161. <div className='column-header__buttons'>
  162. {hasTitle && backButton}
  163. {extraButton}
  164. {collapseButton}
  165. </div>
  166. </h1>
  167. <div className={collapsibleClassName} tabIndex={collapsed ? -1 : null} onTransitionEnd={this.handleTransitionEnd}>
  168. <div className='column-header__collapsible-inner'>
  169. {(!collapsed || animating) && collapsedContent}
  170. </div>
  171. </div>
  172. {appendContent}
  173. </div>
  174. );
  175. if (placeholder) {
  176. return component;
  177. } else {
  178. return (<ButtonInTabsBar>
  179. {component}
  180. </ButtonInTabsBar>);
  181. }
  182. }
  183. }
  184. export default injectIntl(withRouter(ColumnHeader));