Skip to content

Commit

Permalink
[Glitch] Upgrade to react-router v5
Browse files Browse the repository at this point in the history
Port 1b70d7e to glitch-soc

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
  • Loading branch information
renchap and ClearlyClaire committed Oct 21, 2023
1 parent 245513d commit d6eacb7
Show file tree
Hide file tree
Showing 51 changed files with 414 additions and 386 deletions.
21 changes: 11 additions & 10 deletions app/javascript/flavours/glitch/components/column_back_button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ import { createPortal } from 'react-dom';

import { FormattedMessage } from 'react-intl';

import { Icon } from 'flavours/glitch/components/icon';
import { withRouter } from 'react-router-dom';

import { Icon } from 'flavours/glitch/components/icon';
import { WithRouterPropTypes } from 'flavours/glitch/utils/react_router';

export default class ColumnBackButton extends PureComponent {

static contextTypes = {
router: PropTypes.object,
};
class ColumnBackButton extends PureComponent {

static propTypes = {
multiColumn: PropTypes.bool,
...WithRouterPropTypes,
};

handleClick = () => {
const { router } = this.context;
const { history } = this.props;

if (router.history.location?.state?.fromMastodon) {
router.history.goBack();
if (history.location?.state?.fromMastodon) {
history.goBack();
} else {
router.history.push('/');
history.push('/');
}
};

Expand Down Expand Up @@ -57,3 +56,5 @@ export default class ColumnBackButton extends PureComponent {
}

}

export default withRouter(ColumnBackButton);
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import PropTypes from 'prop-types';
import { PureComponent } from 'react';

import { FormattedMessage } from 'react-intl';

import { withRouter } from 'react-router-dom';

import { Icon } from 'flavours/glitch/components/icon';
import { WithRouterPropTypes } from 'flavours/glitch/utils/react_router';

export default class ColumnBackButtonSlim extends PureComponent {
class ColumnBackButtonSlim extends PureComponent {

static contextTypes = {
router: PropTypes.object,
static propTypes = {
...WithRouterPropTypes,
};

handleClick = () => {
const { router } = this.context;
const { location, history } = this.props;

// Check if there is a previous page in the app to go back to per https://stackoverflow.com/a/70532858/9703201
// When upgrading to V6, check `location.key !== 'default'` instead per https://github.com/remix-run/history/blob/main/docs/api-reference.md#location
if (router.route.location.key) {
router.history.goBack();
if (location.key) {
history.goBack();
} else {
router.history.push('/');
history.push('/');
}
};

Expand All @@ -35,3 +37,5 @@ export default class ColumnBackButtonSlim extends PureComponent {
}

}

export default withRouter(ColumnBackButtonSlim);
23 changes: 12 additions & 11 deletions app/javascript/flavours/glitch/components/column_header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { createPortal } from 'react-dom';
import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';

import classNames from 'classnames';
import { withRouter } from 'react-router-dom';

import { Icon } from 'flavours/glitch/components/icon';
import { Icon } from 'flavours/glitch/components/icon';
import { WithRouterPropTypes } from 'flavours/glitch/utils/react_router';

const messages = defineMessages({
show: { id: 'column_header.show_settings', defaultMessage: 'Show settings' },
Expand All @@ -18,7 +20,6 @@ const messages = defineMessages({
class ColumnHeader extends PureComponent {

static contextTypes = {
router: PropTypes.object,
identity: PropTypes.object,
};

Expand All @@ -38,6 +39,7 @@ class ColumnHeader extends PureComponent {
onClick: PropTypes.func,
appendContent: PropTypes.node,
collapseIssues: PropTypes.bool,
...WithRouterPropTypes,
};

state = {
Expand All @@ -63,12 +65,12 @@ class ColumnHeader extends PureComponent {
};

handleBackClick = () => {
const { router } = this.context;
const { history } = this.props;

if (router.history.location?.state?.fromMastodon) {
router.history.goBack();
if (history.location?.state?.fromMastodon) {
history.goBack();
} else {
router.history.push('/');
history.push('/');
}
};

Expand All @@ -78,15 +80,14 @@ class ColumnHeader extends PureComponent {

handlePin = () => {
if (!this.props.pinned) {
this.context.router.history.replace('/');
this.props.history.replace('/');
}

this.props.onPin();
};

render () {
const { router } = this.context;
const { title, icon, active, children, pinned, multiColumn, extraButton, showBackButton, intl: { formatMessage }, placeholder, appendContent, collapseIssues } = this.props;
const { title, icon, active, children, pinned, multiColumn, extraButton, showBackButton, intl: { formatMessage }, placeholder, appendContent, collapseIssues, history } = this.props;
const { collapsed, animating } = this.state;

const wrapperClassName = classNames('column-header__wrapper', {
Expand Down Expand Up @@ -129,7 +130,7 @@ class ColumnHeader extends PureComponent {
pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={this.handlePin}><Icon id='plus' /> <FormattedMessage id='column_header.pin' defaultMessage='Pin' /></button>;
}

if (!pinned && ((multiColumn && router.history.location?.state?.fromMastodon) || showBackButton)) {
if (!pinned && ((multiColumn && history.location?.state?.fromMastodon) || showBackButton)) {
backButton = (
<button onClick={this.handleBackClick} className='column-header__back-button'>
<Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
Expand Down Expand Up @@ -215,4 +216,4 @@ class ColumnHeader extends PureComponent {

}

export default injectIntl(ColumnHeader);
export default injectIntl(withRouter(ColumnHeader));
20 changes: 9 additions & 11 deletions app/javascript/flavours/glitch/components/dropdown_menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import PropTypes from 'prop-types';
import { PureComponent, cloneElement, Children } from 'react';

import classNames from 'classnames';
import { withRouter } from 'react-router-dom';

import ImmutablePropTypes from 'react-immutable-proptypes';

import { supportsPassiveEvents } from 'detect-passive-events';
import Overlay from 'react-overlays/Overlay';

import { CircularProgress } from "./circular_progress";
import { CircularProgress } from 'flavours/glitch/components/circular_progress';
import { WithRouterPropTypes } from 'flavours/glitch/utils/react_router';

import { IconButton } from './icon_button';

const listenerOptions = supportsPassiveEvents ? { passive: true, capture: true } : true;
let id = 0;

class DropdownMenu extends PureComponent {

static contextTypes = {
router: PropTypes.object,
};

static propTypes = {
items: PropTypes.oneOfType([PropTypes.array, ImmutablePropTypes.list]).isRequired,
loading: PropTypes.bool,
Expand Down Expand Up @@ -159,11 +158,7 @@ class DropdownMenu extends PureComponent {

}

export default class Dropdown extends PureComponent {

static contextTypes = {
router: PropTypes.object,
};
class Dropdown extends PureComponent {

static propTypes = {
children: PropTypes.node,
Expand All @@ -183,6 +178,7 @@ export default class Dropdown extends PureComponent {
renderItem: PropTypes.func,
renderHeader: PropTypes.func,
onItemClick: PropTypes.func,
...WithRouterPropTypes
};

static defaultProps = {
Expand Down Expand Up @@ -250,7 +246,7 @@ export default class Dropdown extends PureComponent {
item.action();
} else if (item && item.to) {
e.preventDefault();
this.context.router.history.push(item.to);
this.props.history.push(item.to);
}
};

Expand Down Expand Up @@ -338,3 +334,5 @@ export default class Dropdown extends PureComponent {
}

}

export default withRouter(Dropdown);
12 changes: 7 additions & 5 deletions app/javascript/flavours/glitch/components/modal_root.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import PropTypes from 'prop-types';
import { PureComponent } from 'react';

import 'wicg-inert';

import { multiply } from 'color-blend';
import { createBrowserHistory } from 'history';

export default class ModalRoot extends PureComponent {
import { WithOptionalRouterPropTypes, withOptionalRouter } from 'flavours/glitch/utils/react_router';

static contextTypes = {
router: PropTypes.object,
};
class ModalRoot extends PureComponent {

static propTypes = {
children: PropTypes.node,
Expand All @@ -21,6 +20,7 @@ export default class ModalRoot extends PureComponent {
}),
noEsc: PropTypes.bool,
ignoreFocus: PropTypes.bool,
...WithOptionalRouterPropTypes,
};

activeElement = this.props.children ? document.activeElement : null;
Expand Down Expand Up @@ -56,7 +56,7 @@ export default class ModalRoot extends PureComponent {
componentDidMount () {
window.addEventListener('keyup', this.handleKeyUp, false);
window.addEventListener('keydown', this.handleKeyDown, false);
this.history = this.context.router ? this.context.router.history : createBrowserHistory();
this.history = this.props.history || createBrowserHistory();

if (this.props.children) {
this._handleModalOpen();
Expand Down Expand Up @@ -160,3 +160,5 @@ export default class ModalRoot extends PureComponent {
}

}

export default withOptionalRouter(ModalRoot);
36 changes: 0 additions & 36 deletions app/javascript/flavours/glitch/components/navigation_portal.jsx

This file was deleted.

25 changes: 25 additions & 0 deletions app/javascript/flavours/glitch/components/navigation_portal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Switch, Route } from 'react-router-dom';

import AccountNavigation from 'flavours/glitch/features/account/navigation';
import Trends from 'flavours/glitch/features/getting_started/containers/trends_container';
import { showTrends } from 'flavours/glitch/initial_state';

const DefaultNavigation: React.FC = () =>
showTrends ? (
<>
<div className='flex-spacer' />
<Trends />
</>
) : null;

export const NavigationPortal: React.FC = () => (
<Switch>
<Route path='/@:acct' exact component={AccountNavigation} />
<Route path='/@:acct/tagged/:tagged?' exact component={AccountNavigation} />
<Route path='/@:acct/with_replies' exact component={AccountNavigation} />
<Route path='/@:acct/followers' exact component={AccountNavigation} />
<Route path='/@:acct/following' exact component={AccountNavigation} />
<Route path='/@:acct/media' exact component={AccountNavigation} />
<Route component={DefaultNavigation} />
</Switch>
);
13 changes: 7 additions & 6 deletions app/javascript/flavours/glitch/components/permalink.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import PropTypes from 'prop-types';
import { PureComponent } from 'react';

export default class Permalink extends PureComponent {
import { withOptionalRouter, WithOptionalRouterPropTypes } from 'flavours/glitch/utils/react_router';

static contextTypes = {
router: PropTypes.object,
};
class Permalink extends PureComponent {

static propTypes = {
className: PropTypes.string,
href: PropTypes.string.isRequired,
to: PropTypes.string.isRequired,
children: PropTypes.node,
onInterceptClick: PropTypes.func,
...WithOptionalRouterPropTypes,
};

handleClick = (e) => {
Expand All @@ -22,9 +21,9 @@ export default class Permalink extends PureComponent {
return;
}

if (this.context.router) {
if (this.props.history) {
e.preventDefault();
this.context.router.history.push(this.props.to);
this.props.history.push(this.props.to);
}
}
};
Expand All @@ -47,3 +46,5 @@ export default class Permalink extends PureComponent {
}

}

export default withOptionalRouter(Permalink);
Loading

0 comments on commit d6eacb7

Please sign in to comment.