Skip to content

Commit

Permalink
[Glitch] Convert <Button> to Typescript
Browse files Browse the repository at this point in the history
Port 9d45a44 to glitch-soc

Signed-off-by: Claire <[email protected]>
  • Loading branch information
renchap authored and ClearlyClaire committed Oct 24, 2023
1 parent 1138d44 commit 5c37d37
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 78 deletions.
58 changes: 58 additions & 0 deletions app/javascript/flavours/glitch/components/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useCallback } from 'react';

import classNames from 'classnames';

interface BaseProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
block?: boolean;
secondary?: boolean;
text?: JSX.Element;
}

interface PropsWithChildren extends BaseProps {
text?: never;
}

interface PropsWithText extends BaseProps {
text: JSX.Element;
children: never;
}

type Props = PropsWithText | PropsWithChildren;

export const Button: React.FC<Props> = ({
text,
type = 'button',
onClick,
disabled,
block,
secondary,
className,
title,
children,
...props
}) => {
const handleClick = useCallback<React.MouseEventHandler<HTMLButtonElement>>(
(e) => {
if (!disabled && onClick) {
onClick(e);
}
},
[disabled, onClick],
);

return (
<button
className={classNames('button', className, {
'button-secondary': secondary,
'button--block': block,
})}
disabled={disabled}
onClick={handleClick}
title={title}
type={type}
{...props}
>
{text ?? children}
</button>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';

import { Avatar } from 'flavours/glitch/components/avatar';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { Icon } from 'flavours/glitch/components/icon';
import { IconButton } from 'flavours/glitch/components/icon_button';
import DropdownMenuContainer from 'flavours/glitch/containers/dropdown_menu_container';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';

import { revealAccount } from 'flavours/glitch/actions/accounts';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { domain } from 'flavours/glitch/initial_state';

const mapDispatchToProps = (dispatch, { accountId }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ImmutablePureComponent from 'react-immutable-pure-component';

import { length } from 'stringz';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { Icon } from 'flavours/glitch/components/icon';
import { maxChars } from 'flavours/glitch/initial_state';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from 'flavours/glitch/actions/accounts';
import { openModal } from 'flavours/glitch/actions/modal';
import { Avatar } from 'flavours/glitch/components/avatar';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { DisplayName } from 'flavours/glitch/components/display_name';
import { IconButton } from 'flavours/glitch/components/icon_button';
import Permalink from 'flavours/glitch/components/permalink';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormattedMessage } from 'react-intl';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { connect } from 'react-redux';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { toServerSideType } from 'flavours/glitch/utils/filters';

const mapStateToProps = (state, { filterId }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { requestBrowserPermission } from 'flavours/glitch/actions/notifications'
import { changeSetting, saveSettings } from 'flavours/glitch/actions/settings';
import { fetchSuggestions } from 'flavours/glitch/actions/suggestions';
import { markAsPartial } from 'flavours/glitch/actions/timelines';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import Column from 'flavours/glitch/features/ui/components/column';
import { WithRouterPropTypes } from 'flavours/glitch/utils/react_router';
import imageGreeting from 'mastodon/../images/elephant_ui_greeting.svg';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';

import ImmutablePropTypes from 'react-immutable-proptypes';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { ShortNumber } from 'flavours/glitch/components/short_number';

const messages = defineMessages({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { throttle, escapeRegExp } from 'lodash';

import { openModal, closeModal } from 'flavours/glitch/actions/modal';
import api from 'flavours/glitch/api';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { Icon } from 'flavours/glitch/components/icon';
import { registrationsOpen, sso_redirect } from 'flavours/glitch/initial_state';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux';

import { requestBrowserPermission } from 'flavours/glitch/actions/notifications';
import { changeSetting } from 'flavours/glitch/actions/settings';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { Icon } from 'flavours/glitch/components/icon';
import { IconButton } from 'flavours/glitch/components/icon_button';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { List as ImmutableList } from 'immutable';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { connect } from 'react-redux';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';

import Option from './components/option';

Expand Down
2 changes: 1 addition & 1 deletion app/javascript/flavours/glitch/features/report/comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createSelector } from 'reselect';
import Toggle from 'react-toggle';

import { fetchAccount } from 'flavours/glitch/actions/accounts';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { useAppDispatch, useAppSelector } from 'flavours/glitch/store';

const messages = defineMessages({
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/flavours/glitch/features/report/rules.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormattedMessage } from 'react-intl';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { connect } from 'react-redux';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';

import Option from './components/option';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OrderedSet } from 'immutable';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { connect } from 'react-redux';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { LoadingIndicator } from 'flavours/glitch/components/loading_indicator';
import StatusCheckBox from 'flavours/glitch/features/report/containers/status_check_box_container';

Expand Down
2 changes: 1 addition & 1 deletion app/javascript/flavours/glitch/features/report/thanks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
muteAccount,
blockAccount,
} from 'flavours/glitch/actions/accounts';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';

const mapStateToProps = () => ({});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { connect } from 'react-redux';
import { createSelector } from 'reselect';

import { followAccount } from 'flavours/glitch/actions/accounts';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { IconButton } from 'flavours/glitch/components/icon_button';
import Option from 'flavours/glitch/features/report/components/option';
import { languages as preloadedLanguages } from 'flavours/glitch/initial_state';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { connect } from 'react-redux';
import { blockAccount } from '../../../actions/accounts';
import { closeModal } from '../../../actions/modal';
import { initReport } from '../../../actions/reports';
import Button from '../../../components/button';
import { Button } from '../../../components/button';
import { makeGetAccount } from '../../../selectors';


Expand Down Expand Up @@ -52,10 +52,6 @@ class BlockModal extends PureComponent {
intl: PropTypes.object.isRequired,
};

componentDidMount() {
this.button.focus();
}

handleClick = () => {
this.props.onClose();
this.props.onConfirm(this.props.account);
Expand All @@ -70,10 +66,6 @@ class BlockModal extends PureComponent {
this.props.onClose();
};

setRef = (c) => {
this.button = c;
};

render () {
const { account } = this.props;

Expand All @@ -96,7 +88,7 @@ class BlockModal extends PureComponent {
<Button onClick={this.handleSecondary} className='confirmation-modal__secondary-button'>
<FormattedMessage id='confirmations.block.block_and_report' defaultMessage='Block & Report' />
</Button>
<Button onClick={this.handleClick} ref={this.setRef}>
<Button onClick={this.handleClick} autoFocus>
<FormattedMessage id='confirmations.block.confirm' defaultMessage='Block' />
</Button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { connect } from 'react-redux';
import { changeBoostPrivacy } from 'flavours/glitch/actions/boosts';
import AttachmentList from 'flavours/glitch/components/attachment_list';
import { Avatar } from 'flavours/glitch/components/avatar';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { DisplayName } from 'flavours/glitch/components/display_name';
import { Icon } from 'flavours/glitch/components/icon';
import { RelativeTimestamp } from 'flavours/glitch/components/relative_timestamp';
Expand Down Expand Up @@ -50,10 +50,6 @@ class BoostModal extends ImmutablePureComponent {
...WithRouterPropTypes,
};

componentDidMount() {
this.button.focus();
}

handleReblog = () => {
this.props.onReblog(this.props.status, this.props.privacy);
this.props.onClose();
Expand All @@ -71,10 +67,6 @@ class BoostModal extends ImmutablePureComponent {
return document.getElementsByClassName('modal-root__container')[0];
};

setRef = (c) => {
this.button = c;
};

render () {
const { status, missingMediaDescription, privacy, intl } = this.props;
const buttonText = status.get('reblogged') ? messages.cancel_reblog : messages.reblog;
Expand Down Expand Up @@ -127,7 +119,7 @@ class BoostModal extends ImmutablePureComponent {
onChange={this.props.onChangeBoostPrivacy}
/>
)}
<Button text={intl.formatMessage(buttonText)} onClick={this.handleReblog} ref={this.setRef} />
<Button text={intl.formatMessage(buttonText)} onClick={this.handleReblog} autoFocus />
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classNames from 'classnames';
import { Helmet } from 'react-helmet';
import { Link } from 'react-router-dom';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import Column from 'flavours/glitch/components/column';
import { autoPlayGif } from 'flavours/glitch/initial_state';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PureComponent } from 'react';

import { injectIntl, FormattedMessage } from 'react-intl';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';

class ConfirmationModal extends PureComponent {

Expand All @@ -23,10 +23,6 @@ class ConfirmationModal extends PureComponent {
closeWhenConfirm: true,
};

componentDidMount() {
this.button.focus();
}

handleClick = () => {
if (this.props.closeWhenConfirm) {
this.props.onClose();
Expand All @@ -46,10 +42,6 @@ class ConfirmationModal extends PureComponent {
this.props.onClose();
};

setRef = (c) => {
this.button = c;
};

setDoNotAskRef = (c) => {
this.doNotAskCheckbox = c;
};
Expand Down Expand Up @@ -79,7 +71,7 @@ class ConfirmationModal extends PureComponent {
{secondary !== undefined && (
<Button text={secondary} onClick={this.handleSecondary} className='confirmation-modal__secondary-button' />
)}
<Button text={confirm} onClick={this.handleClick} ref={this.setRef} />
<Button text={confirm} onClick={this.handleClick} autoFocus />
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';

import ImmutablePropTypes from 'react-immutable-proptypes';

import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { Icon } from 'flavours/glitch/components/icon';
import illustration from 'flavours/glitch/images/logo_warn_glitch.svg';
import { preferenceLink } from 'flavours/glitch/utils/backend_links';
Expand All @@ -25,19 +25,11 @@ class DeprecatedSettingsModal extends PureComponent {
intl: PropTypes.object.isRequired,
};

componentDidMount() {
this.button.focus();
}

handleClick = () => {
this.props.onConfirm();
this.props.onClose();
};

setRef = (c) => {
this.button = c;
};

render () {
const { settings, intl } = this.props;

Expand Down Expand Up @@ -78,7 +70,7 @@ class DeprecatedSettingsModal extends PureComponent {
<div>
<div className='confirmation-modal__action-bar'>
<div />
<Button text={intl.formatMessage(messages.discardChanges)} onClick={this.handleClick} ref={this.setRef} />
<Button text={intl.formatMessage(messages.discardChanges)} onClick={this.handleClick} autoFocus />
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Atrament from 'atrament'; // the doodling library
import { debounce, mapValues } from 'lodash';

import { doodleSet, uploadCompose } from 'flavours/glitch/actions/compose';
import Button from 'flavours/glitch/components/button';
import { Button } from 'flavours/glitch/components/button';
import { IconButton } from 'flavours/glitch/components/icon_button';
// palette nicked from MyPaint, CC0
const palette = [
Expand Down
Loading

0 comments on commit 5c37d37

Please sign in to comment.