From d6eacb79c6e94f8b9d962c040b3b709a551e4e52 Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Thu, 19 Oct 2023 19:44:55 +0200 Subject: [PATCH] [Glitch] Upgrade to react-router v5 Port 1b70d7ed7c0fd3a9fcf028bf76b8c62ac8b3897f to glitch-soc Co-authored-by: Claire Signed-off-by: Claire --- .../glitch/components/column_back_button.jsx | 21 +++---- .../components/column_back_button_slim.jsx | 20 ++++--- .../glitch/components/column_header.jsx | 23 +++---- .../glitch/components/dropdown_menu.jsx | 20 +++---- .../flavours/glitch/components/modal_root.jsx | 12 ++-- .../glitch/components/navigation_portal.jsx | 36 ----------- .../glitch/components/navigation_portal.tsx | 25 ++++++++ .../flavours/glitch/components/permalink.jsx | 13 ++-- .../flavours/glitch/components/router.tsx | 28 ++++++--- .../glitch/components/scrollable_list.jsx | 37 +++++++++--- .../flavours/glitch/components/status.jsx | 27 ++++----- .../glitch/components/status_action_bar.jsx | 25 ++++---- .../glitch/components/status_content.jsx | 7 ++- .../account/components/featured_tags.jsx | 4 -- .../features/account/components/header.jsx | 13 ++-- .../account_timeline/components/header.jsx | 16 ++--- .../components/moved_note.jsx | 16 ++--- .../features/community_timeline/index.jsx | 1 - .../compose/components/compose_form.jsx | 14 ++--- .../compose/components/reply_indicator.jsx | 12 +--- .../features/compose/components/search.jsx | 40 ++++++------- .../features/compose/components/upload.jsx | 4 -- .../components/conversation.jsx | 22 ++++--- .../glitch/features/directory/index.jsx | 4 -- .../glitch/features/explore/index.jsx | 1 - .../features/follow_recommendations/index.jsx | 14 ++--- .../components/announcements.jsx | 26 ++++---- .../glitch/features/getting_started/index.jsx | 1 - .../features/getting_started_misc/index.jsx | 1 - .../glitch/features/list_timeline/index.jsx | 13 ++-- .../notifications/components/admin_report.jsx | 18 +++--- .../notifications/components/admin_signup.jsx | 18 +++--- .../notifications/components/follow.jsx | 18 +++--- .../components/follow_request.jsx | 13 ++-- .../containers/notification_container.js | 6 +- .../picture_in_picture/components/footer.jsx | 17 +++--- .../glitch/features/public_timeline/index.jsx | 1 - .../features/status/components/action_bar.jsx | 16 ++--- .../status/components/detailed_status.jsx | 25 ++++---- .../flavours/glitch/features/status/index.jsx | 21 ++++--- .../features/ui/components/audio_modal.jsx | 4 -- .../features/ui/components/boost_modal.jsx | 12 ++-- .../features/ui/components/columns_area.jsx | 5 -- .../ui/components/favourite_modal.jsx | 11 ++-- .../features/ui/components/list_panel.jsx | 4 +- .../features/ui/components/media_modal.jsx | 4 -- .../ui/components/navigation_panel.jsx | 3 +- .../flavours/glitch/features/ui/index.jsx | 6 +- .../features/ui/util/react_router_helpers.jsx | 33 ++++------ .../flavours/glitch/utils/dom_helpers.js | 7 --- .../flavours/glitch/utils/react_router.jsx | 60 +++++++++++++++++++ 51 files changed, 413 insertions(+), 385 deletions(-) delete mode 100644 app/javascript/flavours/glitch/components/navigation_portal.jsx create mode 100644 app/javascript/flavours/glitch/components/navigation_portal.tsx delete mode 100644 app/javascript/flavours/glitch/utils/dom_helpers.js create mode 100644 app/javascript/flavours/glitch/utils/react_router.jsx diff --git a/app/javascript/flavours/glitch/components/column_back_button.jsx b/app/javascript/flavours/glitch/components/column_back_button.jsx index 5a11a3d5ea..0562fae29b 100644 --- a/app/javascript/flavours/glitch/components/column_back_button.jsx +++ b/app/javascript/flavours/glitch/components/column_back_button.jsx @@ -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('/'); } }; @@ -57,3 +56,5 @@ export default class ColumnBackButton extends PureComponent { } } + +export default withRouter(ColumnBackButton); diff --git a/app/javascript/flavours/glitch/components/column_back_button_slim.jsx b/app/javascript/flavours/glitch/components/column_back_button_slim.jsx index 7b3bac45f9..4e4185387d 100644 --- a/app/javascript/flavours/glitch/components/column_back_button_slim.jsx +++ b/app/javascript/flavours/glitch/components/column_back_button_slim.jsx @@ -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('/'); } }; @@ -35,3 +37,5 @@ export default class ColumnBackButtonSlim extends PureComponent { } } + +export default withRouter(ColumnBackButtonSlim); diff --git a/app/javascript/flavours/glitch/components/column_header.jsx b/app/javascript/flavours/glitch/components/column_header.jsx index 8a68036e9c..fa13f181d0 100644 --- a/app/javascript/flavours/glitch/components/column_header.jsx +++ b/app/javascript/flavours/glitch/components/column_header.jsx @@ -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' }, @@ -18,7 +20,6 @@ const messages = defineMessages({ class ColumnHeader extends PureComponent { static contextTypes = { - router: PropTypes.object, identity: PropTypes.object, }; @@ -38,6 +39,7 @@ class ColumnHeader extends PureComponent { onClick: PropTypes.func, appendContent: PropTypes.node, collapseIssues: PropTypes.bool, + ...WithRouterPropTypes, }; state = { @@ -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('/'); } }; @@ -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', { @@ -129,7 +130,7 @@ class ColumnHeader extends PureComponent { pinButton = ; } - if (!pinned && ((multiColumn && router.history.location?.state?.fromMastodon) || showBackButton)) { + if (!pinned && ((multiColumn && history.location?.state?.fromMastodon) || showBackButton)) { backButton = (