2018-09-27 16:32:37 +02:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2018-10-22 10:46:45 +02:00
|
|
|
import { withRouter } from "react-router-dom";
|
2018-10-22 08:00:34 +02:00
|
|
|
import type { Repository, Branch } from "@scm-manager/ui-types";
|
2018-09-27 16:32:37 +02:00
|
|
|
import FileTree from "../components/FileTree";
|
|
|
|
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
2018-10-19 09:17:26 +02:00
|
|
|
import BranchSelector from "../../containers/BranchSelector";
|
2018-10-19 16:02:21 +02:00
|
|
|
import {
|
|
|
|
|
fetchBranches,
|
|
|
|
|
getBranches,
|
|
|
|
|
getFetchBranchesFailure,
|
|
|
|
|
isFetchBranchesPending
|
|
|
|
|
} from "../../modules/branches";
|
|
|
|
|
import { compose } from "redux";
|
2018-10-25 14:03:45 +02:00
|
|
|
import Content from "./Content";
|
2018-10-25 13:59:13 +02:00
|
|
|
import { fetchSources, isDirectory } from "../modules/sources";
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
repository: Repository,
|
|
|
|
|
loading: boolean,
|
|
|
|
|
error: Error,
|
2018-09-28 11:31:38 +02:00
|
|
|
baseUrl: string,
|
2018-10-19 09:17:26 +02:00
|
|
|
branches: Branch[],
|
2018-10-23 10:41:10 +02:00
|
|
|
revision: string,
|
2018-10-22 10:23:45 +02:00
|
|
|
path: string,
|
2018-10-25 11:27:31 +02:00
|
|
|
currentFileIsDirectory: boolean,
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
// dispatch props
|
2018-10-19 10:32:01 +02:00
|
|
|
fetchBranches: Repository => void,
|
2018-10-25 11:27:31 +02:00
|
|
|
fetchSources: (Repository, string, string) => void,
|
2018-10-19 09:17:26 +02:00
|
|
|
|
|
|
|
|
// Context props
|
|
|
|
|
history: any,
|
2018-09-28 11:31:38 +02:00
|
|
|
match: any
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Sources extends React.Component<Props> {
|
|
|
|
|
componentDidMount() {
|
2018-10-25 11:27:31 +02:00
|
|
|
const {
|
|
|
|
|
fetchBranches,
|
|
|
|
|
repository,
|
|
|
|
|
revision,
|
|
|
|
|
path,
|
|
|
|
|
fetchSources
|
|
|
|
|
} = this.props;
|
2018-10-19 10:32:01 +02:00
|
|
|
|
2018-10-19 11:02:00 +02:00
|
|
|
fetchBranches(repository);
|
2018-10-25 11:27:31 +02:00
|
|
|
fetchSources(repository, revision, path);
|
|
|
|
|
}
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
|
const { fetchSources, repository, revision, path } = this.props;
|
|
|
|
|
if (prevProps.revision !== revision || prevProps.path !== path) {
|
|
|
|
|
fetchSources(repository, revision, path);
|
|
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-19 09:17:26 +02:00
|
|
|
branchSelected = (branch?: Branch) => {
|
2018-10-22 10:23:45 +02:00
|
|
|
const { baseUrl, history, path } = this.props;
|
2018-10-19 09:17:26 +02:00
|
|
|
let url;
|
|
|
|
|
if (branch) {
|
2018-10-22 11:26:03 +02:00
|
|
|
if (path) {
|
2018-10-23 13:03:15 +02:00
|
|
|
url = `${baseUrl}/${encodeURIComponent(branch.name)}/${path}`;
|
2018-10-22 11:26:03 +02:00
|
|
|
} else {
|
2018-10-23 13:03:15 +02:00
|
|
|
url = `${baseUrl}/${encodeURIComponent(branch.name)}/`;
|
2018-10-22 11:26:03 +02:00
|
|
|
}
|
2018-10-19 09:17:26 +02:00
|
|
|
} else {
|
2018-10-19 10:46:51 +02:00
|
|
|
url = `${baseUrl}/`;
|
2018-10-19 09:17:26 +02:00
|
|
|
}
|
2018-10-19 10:46:51 +02:00
|
|
|
history.push(url);
|
2018-10-19 09:17:26 +02:00
|
|
|
};
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
render() {
|
2018-10-25 11:27:31 +02:00
|
|
|
const {
|
|
|
|
|
repository,
|
|
|
|
|
baseUrl,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
revision,
|
|
|
|
|
path,
|
|
|
|
|
currentFileIsDirectory
|
|
|
|
|
} = this.props;
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 16:02:21 +02:00
|
|
|
if (loading) {
|
2018-09-27 16:32:37 +02:00
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 11:27:31 +02:00
|
|
|
if (currentFileIsDirectory) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{this.renderBranchSelector()}
|
|
|
|
|
<FileTree
|
|
|
|
|
repository={repository}
|
|
|
|
|
revision={revision}
|
|
|
|
|
path={path}
|
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2018-10-25 13:59:13 +02:00
|
|
|
return (
|
|
|
|
|
<Content repository={repository} revision={revision} path={path} />
|
|
|
|
|
);
|
2018-10-25 11:27:31 +02:00
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
2018-10-19 09:17:26 +02:00
|
|
|
|
|
|
|
|
renderBranchSelector = () => {
|
2018-10-23 11:24:23 +02:00
|
|
|
const { repository, branches, revision } = this.props;
|
2018-10-25 11:27:31 +02:00
|
|
|
|
2018-10-19 09:17:26 +02:00
|
|
|
if (repository._links.branches) {
|
|
|
|
|
return (
|
|
|
|
|
<BranchSelector
|
|
|
|
|
branches={branches}
|
2018-10-23 11:24:23 +02:00
|
|
|
selectedBranch={revision}
|
2018-10-19 09:17:26 +02:00
|
|
|
selected={(b: Branch) => {
|
|
|
|
|
this.branchSelected(b);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-10-22 10:23:45 +02:00
|
|
|
const { repository, match } = ownProps;
|
2018-10-23 10:41:10 +02:00
|
|
|
const { revision, path } = match.params;
|
2018-10-23 13:03:15 +02:00
|
|
|
const decodedRevision = revision ? decodeURIComponent(revision) : undefined;
|
2018-10-19 16:02:21 +02:00
|
|
|
const loading = isFetchBranchesPending(state, repository);
|
|
|
|
|
const error = getFetchBranchesFailure(state, repository);
|
2018-10-19 09:17:26 +02:00
|
|
|
const branches = getBranches(state, repository);
|
2018-10-25 13:59:13 +02:00
|
|
|
const currentFileIsDirectory = decodedRevision
|
|
|
|
|
? isDirectory(state, repository, decodedRevision, path)
|
|
|
|
|
: isDirectory(state, repository, revision, path);
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
return {
|
2018-10-19 16:02:21 +02:00
|
|
|
repository,
|
2018-10-23 13:03:15 +02:00
|
|
|
revision: decodedRevision,
|
2018-10-22 10:23:45 +02:00
|
|
|
path,
|
2018-09-27 16:32:37 +02:00
|
|
|
loading,
|
|
|
|
|
error,
|
2018-10-25 11:27:31 +02:00
|
|
|
branches,
|
2018-10-25 13:59:13 +02:00
|
|
|
currentFileIsDirectory
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
2018-10-19 10:32:01 +02:00
|
|
|
fetchBranches: (repository: Repository) => {
|
|
|
|
|
dispatch(fetchBranches(repository));
|
2018-10-25 11:27:31 +02:00
|
|
|
},
|
|
|
|
|
fetchSources: (repository: Repository, revision: string, path: string) => {
|
|
|
|
|
dispatch(fetchSources(repository, revision, path));
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-19 16:02:21 +02:00
|
|
|
export default compose(
|
|
|
|
|
withRouter,
|
|
|
|
|
connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)
|
2018-09-27 16:32:37 +02:00
|
|
|
)(Sources);
|