diff --git a/scm-ui/src/repos/containers/ChangesetsRoot.js b/scm-ui/src/repos/containers/ChangesetsRoot.js index 8e0b44afbe..db440b17c0 100644 --- a/scm-ui/src/repos/containers/ChangesetsRoot.js +++ b/scm-ui/src/repos/containers/ChangesetsRoot.js @@ -43,8 +43,29 @@ type Props = { class ChangesetsRoot extends React.Component { componentDidMount() { this.props.fetchBranches(this.props.repository); + this.redirectToDefaultBranch(); } + redirectToDefaultBranch = () => { + if (this.shouldRedirectToDefaultBranch()) { + const defaultBranches = this.props.branches.filter( + b => b.defaultBranch === true + ); + if (defaultBranches.length > 0) { + this.branchSelected(defaultBranches[0]); + } + } + }; + + shouldRedirectToDefaultBranch = () => { + return ( + this.props.branches && + this.props.branches.length > 0 && + this.props.selected !== + this.props.branches.filter(b => b.defaultBranch === true)[0] + ); + }; + stripEndingSlash = (url: string) => { if (url.endsWith("/")) { return url.substring(0, url.length - 1); diff --git a/scm-ui/src/repos/sources/containers/Sources.js b/scm-ui/src/repos/sources/containers/Sources.js index d09e18e5be..065dfb5a51 100644 --- a/scm-ui/src/repos/sources/containers/Sources.js +++ b/scm-ui/src/repos/sources/containers/Sources.js @@ -80,20 +80,17 @@ class Sources extends React.Component { } redirectToDefaultBranch = () => { - const { branches, baseUrl } = this.props; - if (this.shouldRedirect()) { + const { branches } = this.props; + if (this.shouldRedirectToDefaultBranch()) { const defaultBranches = branches.filter(b => b.defaultBranch); if (defaultBranches.length > 0) { - this.props.history.push( - `${baseUrl}/${encodeURIComponent(defaultBranches[0].name)}/` - ); - this.setState({ selectedBranch: defaultBranches[0] }); + this.branchSelected(defaultBranches[0]); } } }; - shouldRedirect = () => { + shouldRedirectToDefaultBranch = () => { const { branches, revision } = this.props; return branches && !revision; };