import React from "react"; import styled from "styled-components"; import { Route, RouteComponentProps, withRouter } from "react-router-dom"; import Sources from "../../sources/containers/Sources"; import ChangesetsRoot from "../../containers/ChangesetsRoot"; import { Branch, Repository } from "@scm-manager/ui-types"; import { BranchSelector, ErrorPage, Level, Loading } from "@scm-manager/ui-components"; import CodeViewSwitcher from "../components/CodeViewSwitcher"; import { compose } from "redux"; import { WithTranslation, withTranslation } from "react-i18next"; import { connect } from "react-redux"; import { fetchBranches, getBranches, getFetchBranchesFailure, isFetchBranchesPending } from "../../branches/modules/branches"; type Props = RouteComponentProps & WithTranslation & { repository: Repository; baseUrl: string; // State props branches: Branch[]; error: Error; loading: boolean; selectedView: string; selectedBranch: string; // Dispatch props fetchBranches: (p: Repository) => void; }; const CodeActionBar = styled.div.attrs(() => ({}))` background-color: whitesmoke; border: 1px solid #dbdbdb; border-radius: 4px; color: #363636; font-size: 1.25em; font-weight: 300; line-height: 1.25; padding: 0.5em 0.75em; margin-bottom: 1em; `; class CodeOverview extends React.Component { componentDidMount() { const { repository, branches } = this.props; new Promise(() => { this.props.fetchBranches(repository); }).then(() => { if (this.props.branches?.length > 0) { const defaultBranch = branches.filter((branch: Branch) => branch.defaultBranch === true)[0]; this.branchSelected(defaultBranch); } }); } findSelectedBranch = () => { const { selectedBranch, branches } = this.props; return branches?.find((branch: Branch) => branch.name === selectedBranch); }; branchSelected = (branch?: Branch) => { let splittedUrl = this.props.location.pathname.split("/"); if (branch) { splittedUrl[6] = encodeURIComponent(branch.name); } this.props.history.push(splittedUrl.join("/")); }; render() { const { repository, baseUrl, branches, error, loading, t } = this.props; const url = baseUrl; if (loading) { return ; } if (error) { return ( ); } return (
} right={} /> } /> } /> } /> } />
); } } const mapDispatchToProps = (dispatch: any) => { return { fetchBranches: (repo: Repository) => { dispatch(fetchBranches(repo)); } }; }; const mapStateToProps = (state: any, ownProps: Props) => { const { repository, location } = ownProps; const error = getFetchBranchesFailure(state, repository); const loading = isFetchBranchesPending(state, repository); const branches = getBranches(state, repository); const selectedView = decodeURIComponent(location.pathname.split("/")[5]); const branchFromURL = decodeURIComponent(location.pathname.split("/")[6]); const selectedBranch = branchFromURL && branchFromURL !== "undefined" ? branchFromURL : ""; return { error, loading, branches, selectedView, selectedBranch }; }; export default compose( withRouter, withTranslation("repos"), connect(mapStateToProps, mapDispatchToProps) )(CodeOverview);