Skip to content

Commit

Permalink
Revert "Upgrade to react-router v5 (mastodon#25047)"
Browse files Browse the repository at this point in the history
This reverts commit 1b70d7e.
  • Loading branch information
zunda committed Oct 23, 2023
1 parent 7bccac7 commit ecce296
Show file tree
Hide file tree
Showing 42 changed files with 363 additions and 407 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ module.exports = {
},
// Common React utilities
{
pattern: '{classnames,react-helmet,react-router,react-router-dom}',
pattern: '{classnames,react-helmet,react-router-dom}',
group: 'external',
position: 'before',
},
Expand Down
21 changes: 10 additions & 11 deletions app/javascript/mastodon/components/column_back_button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import { createPortal } from 'react-dom';

import { FormattedMessage } from 'react-intl';

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

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

class ColumnBackButton extends PureComponent {
export default class ColumnBackButton extends PureComponent {

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

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

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

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

Expand Down Expand Up @@ -59,5 +60,3 @@ class ColumnBackButton extends PureComponent {
}

}

export default withRouter(ColumnBackButton);
21 changes: 10 additions & 11 deletions app/javascript/mastodon/components/column_header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ 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 'mastodon/components/icon';
import { WithRouterPropTypes } from 'mastodon/utils/react_router';

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

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

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

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

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

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

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

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

this.props.onPin();
};

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

const wrapperClassName = classNames('column-header__wrapper', {
Expand Down Expand Up @@ -130,7 +129,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 && history.location?.state?.fromMastodon) || showBackButton)) {
if (!pinned && ((multiColumn && router.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 @@ -216,4 +215,4 @@ class ColumnHeader extends PureComponent {

}

export default injectIntl(withRouter(ColumnHeader));
export default injectIntl(ColumnHeader);
20 changes: 11 additions & 9 deletions app/javascript/mastodon/components/dropdown_menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ 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 'mastodon/components/circular_progress';
import { WithRouterPropTypes } from 'mastodon/utils/react_router';

import { CircularProgress } from "./circular_progress";
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 @@ -158,7 +159,11 @@ class DropdownMenu extends PureComponent {

}

class Dropdown extends PureComponent {
export default class Dropdown extends PureComponent {

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

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

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

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

}

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

import 'wicg-inert';

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

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

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

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

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

UNSAFE_componentWillReceiveProps (nextProps) {
Expand Down Expand Up @@ -156,5 +156,3 @@ class ModalRoot extends PureComponent {
}

}

export default withOptionalRouter(ModalRoot);
35 changes: 35 additions & 0 deletions app/javascript/mastodon/components/navigation_portal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PureComponent } from 'react';

import { Switch, Route, withRouter } from 'react-router-dom';

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

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

class NavigationPortal extends PureComponent {

render () {
return (
<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>
);
}

}
export default withRouter(NavigationPortal);
25 changes: 0 additions & 25 deletions app/javascript/mastodon/components/navigation_portal.tsx

This file was deleted.

28 changes: 7 additions & 21 deletions app/javascript/mastodon/components/router.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,41 @@
import type { PropsWithChildren } from 'react';
import React from 'react';

import { Router as OriginalRouter } from 'react-router';

import type { LocationDescriptor, Path } from 'history';
import { createBrowserHistory } from 'history';
import { Router as OriginalRouter } from 'react-router';

import { layoutFromWindow } from 'mastodon/is_mobile';

interface MastodonLocationState {
fromMastodon?: boolean;
mastodonModalKey?: string;
}
type HistoryPath = Path | LocationDescriptor<MastodonLocationState>;

const browserHistory = createBrowserHistory<
MastodonLocationState | undefined
>();
const originalPush = browserHistory.push.bind(browserHistory);
const originalReplace = browserHistory.replace.bind(browserHistory);

function extractRealPath(path: HistoryPath) {
if (typeof path === 'string') return path;
else return path.pathname;
}

browserHistory.push = (path: HistoryPath, state?: MastodonLocationState) => {
browserHistory.push = (path: string, state?: MastodonLocationState) => {
state = state ?? {};
state.fromMastodon = true;

const realPath = extractRealPath(path);
if (!realPath) return;

if (layoutFromWindow() === 'multi-column' && !realPath.startsWith('/deck')) {
originalPush(`/deck${realPath}`, state);
if (layoutFromWindow() === 'multi-column' && !path.startsWith('/deck')) {
originalPush(`/deck${path}`, state);
} else {
originalPush(path, state);
}
};

browserHistory.replace = (path: HistoryPath, state?: MastodonLocationState) => {
browserHistory.replace = (path: string, state?: MastodonLocationState) => {
if (browserHistory.location.state?.fromMastodon) {
state = state ?? {};
state.fromMastodon = true;
}

const realPath = extractRealPath(path);
if (!realPath) return;

if (layoutFromWindow() === 'multi-column' && !realPath.startsWith('/deck')) {
originalReplace(`/deck${realPath}`, state);
if (layoutFromWindow() === 'multi-column' && !path.startsWith('/deck')) {
originalReplace(`/deck${path}`, state);
} else {
originalReplace(path, state);
}
Expand Down
Loading

0 comments on commit ecce296

Please sign in to comment.