2020-01-07 10:30:46 +01:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
2020-01-08 10:31:43 +01:00
|
|
|
import { Route, RouteComponentProps, withRouter } from "react-router-dom";
|
2020-01-07 10:30:46 +01:00
|
|
|
import Sources from "../../sources/containers/Sources";
|
|
|
|
|
import ChangesetsRoot from "../../containers/ChangesetsRoot";
|
2020-01-08 10:31:43 +01:00
|
|
|
import { Branch, Repository } from "@scm-manager/ui-types";
|
2020-01-08 11:01:00 +01:00
|
|
|
import { BranchSelector, ErrorPage, Level, Loading } from "@scm-manager/ui-components";
|
2020-01-07 10:30:46 +01:00
|
|
|
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;
|
2020-01-08 11:01:00 +01:00
|
|
|
loading: boolean;
|
2020-01-07 10:30:46 +01:00
|
|
|
selectedBranch: string;
|
|
|
|
|
|
|
|
|
|
// Dispatch props
|
|
|
|
|
fetchBranches: (p: Repository) => void;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
const CodeActionBar = styled.div.attrs(() => ({}))`
|
2020-01-07 10:30:46 +01:00
|
|
|
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;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
class CodeOverview extends React.Component<Props> {
|
2020-01-07 10:30:46 +01:00
|
|
|
componentDidMount() {
|
2020-01-08 10:31:43 +01:00
|
|
|
const { repository, branches } = this.props;
|
|
|
|
|
new Promise(() => {
|
|
|
|
|
this.props.fetchBranches(repository);
|
|
|
|
|
}).then(() => {
|
2020-01-08 11:01:00 +01:00
|
|
|
if (this.props.branches?.length > 0) {
|
2020-01-08 10:31:43 +01:00
|
|
|
const defaultBranch = branches.filter((branch: Branch) => branch.defaultBranch === true)[0];
|
|
|
|
|
this.branchSelected(defaultBranch);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-01-07 10:30:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findSelectedBranch = () => {
|
|
|
|
|
const { selectedBranch, branches } = this.props;
|
|
|
|
|
return branches?.find((branch: Branch) => branch.name === selectedBranch);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
branchSelected = (branch?: Branch) => {
|
2020-01-08 10:31:43 +01:00
|
|
|
let splittedUrl = this.props.location.pathname.split("/");
|
2020-01-10 09:19:09 +01:00
|
|
|
if (
|
2020-01-10 09:31:32 +01:00
|
|
|
this.props.location.pathname.includes("/code/sources") ||
|
|
|
|
|
this.props.location.pathname.includes("/code/branch")
|
2020-01-10 09:19:09 +01:00
|
|
|
) {
|
|
|
|
|
if (branch) {
|
|
|
|
|
splittedUrl[6] = encodeURIComponent(branch.name);
|
|
|
|
|
}
|
|
|
|
|
this.props.history.push(splittedUrl.join("/"));
|
|
|
|
|
}
|
2020-01-10 09:31:32 +01:00
|
|
|
if (this.props.location.pathname.includes("/code/changesets")) {
|
2020-01-10 09:19:09 +01:00
|
|
|
this.props.history.push(
|
|
|
|
|
`${splittedUrl[0]}/${splittedUrl[1]}/${splittedUrl[2]}/${splittedUrl[3]}/${
|
|
|
|
|
splittedUrl[4]
|
|
|
|
|
}/branch/${encodeURIComponent(branch.name)}/${splittedUrl[5]}/`
|
|
|
|
|
);
|
2020-01-07 10:30:46 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2020-01-08 11:01:00 +01:00
|
|
|
const { repository, baseUrl, branches, error, loading, t } = this.props;
|
2020-01-07 10:30:46 +01:00
|
|
|
const url = baseUrl;
|
|
|
|
|
|
2020-01-08 12:38:21 +01:00
|
|
|
if (loading) {
|
2020-01-08 11:01:00 +01:00
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<ErrorPage title={t("repositoryRoot.errorTitle")} subtitle={t("repositoryRoot.errorSubtitle")} error={error} />
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 10:30:46 +01:00
|
|
|
return (
|
|
|
|
|
<div>
|
2020-01-08 10:31:43 +01:00
|
|
|
<CodeActionBar>
|
2020-01-07 10:30:46 +01:00
|
|
|
<Level
|
|
|
|
|
left={
|
2020-01-08 10:31:43 +01:00
|
|
|
<BranchSelector
|
|
|
|
|
label={t("code.branchSelector")}
|
|
|
|
|
branches={branches}
|
|
|
|
|
selectedBranch={this.props.selectedBranch}
|
|
|
|
|
onSelectBranch={this.branchSelected}
|
|
|
|
|
/>
|
2020-01-07 10:30:46 +01:00
|
|
|
}
|
2020-01-10 09:19:09 +01:00
|
|
|
right={<CodeViewSwitcher url={this.props.location.pathname} branches={branches} />}
|
2020-01-07 10:30:46 +01:00
|
|
|
/>
|
2020-01-08 10:31:43 +01:00
|
|
|
</CodeActionBar>
|
2020-01-07 10:30:46 +01:00
|
|
|
<Route
|
|
|
|
|
path={`${url}/sources`}
|
|
|
|
|
exact={true}
|
2020-01-08 11:01:00 +01:00
|
|
|
render={() => <Sources repository={repository} baseUrl={`${url}/sources`} branches={branches} />}
|
2020-01-07 10:30:46 +01:00
|
|
|
/>
|
|
|
|
|
<Route
|
|
|
|
|
path={`${url}/sources/:revision/:path*`}
|
2020-01-08 11:01:00 +01:00
|
|
|
render={() => <Sources repository={repository} baseUrl={`${url}/sources`} branches={branches} />}
|
2020-01-07 10:30:46 +01:00
|
|
|
/>
|
|
|
|
|
<Route
|
|
|
|
|
path={`${url}/changesets`}
|
|
|
|
|
render={() => <ChangesetsRoot repository={repository} baseUrl={`${url}/changesets`} />}
|
|
|
|
|
/>
|
|
|
|
|
<Route
|
2020-01-10 09:19:09 +01:00
|
|
|
path={`${url}/branch/:branch/changesets/`}
|
2020-01-07 10:30:46 +01:00
|
|
|
render={() => <ChangesetsRoot repository={repository} baseUrl={`${url}/changesets`} />}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
const mapDispatchToProps = (dispatch: any) => {
|
2020-01-07 10:30:46 +01:00
|
|
|
return {
|
|
|
|
|
fetchBranches: (repo: Repository) => {
|
|
|
|
|
dispatch(fetchBranches(repo));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
|
|
|
|
const { repository, location } = ownProps;
|
|
|
|
|
const error = getFetchBranchesFailure(state, repository);
|
2020-01-08 11:01:00 +01:00
|
|
|
const loading = isFetchBranchesPending(state, repository);
|
2020-01-07 10:30:46 +01:00
|
|
|
const branches = getBranches(state, repository);
|
2020-01-10 09:19:09 +01:00
|
|
|
const branchFromURL =
|
|
|
|
|
!location.pathname.includes("/code/changesets/") && decodeURIComponent(location.pathname.split("/")[6]);
|
2020-01-08 15:17:07 +01:00
|
|
|
const selectedBranch = branchFromURL && branchFromURL !== "undefined" ? branchFromURL : "";
|
2020-01-07 10:30:46 +01:00
|
|
|
return {
|
|
|
|
|
error,
|
2020-01-08 11:01:00 +01:00
|
|
|
loading,
|
2020-01-07 10:30:46 +01:00
|
|
|
branches,
|
|
|
|
|
selectedBranch
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withRouter,
|
|
|
|
|
withTranslation("repos"),
|
|
|
|
|
connect(mapStateToProps, mapDispatchToProps)
|
2020-01-08 10:31:43 +01:00
|
|
|
)(CodeOverview);
|