2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { connect } from "react-redux";
|
2020-01-14 18:07:57 +01:00
|
|
|
import { withRouter, RouteComponentProps } from "react-router-dom";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { Branch, Repository } from "@scm-manager/ui-types";
|
2020-01-08 10:31:43 +01:00
|
|
|
import { Breadcrumb, ErrorNotification, Loading } from "@scm-manager/ui-components";
|
2019-10-23 15:47:08 +02:00
|
|
|
import FileTree from "../components/FileTree";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { getFetchBranchesFailure, isFetchBranchesPending } from "../../branches/modules/branches";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { compose } from "redux";
|
|
|
|
|
import Content from "./Content";
|
2020-01-07 10:30:46 +01:00
|
|
|
import { fetchSources, getSources, isDirectory } from "../modules/sources";
|
2020-01-14 18:07:57 +01:00
|
|
|
import CodeActionBar from "../../codeSection/components/CodeActionBar";
|
|
|
|
|
|
|
|
|
|
type Props = WithTranslation &
|
|
|
|
|
RouteComponentProps & {
|
|
|
|
|
repository: Repository;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
baseUrl: string;
|
|
|
|
|
branches: Branch[];
|
|
|
|
|
revision: string;
|
|
|
|
|
path: string;
|
|
|
|
|
currentFileIsDirectory: boolean;
|
|
|
|
|
sources: File;
|
|
|
|
|
selectedBranch: string;
|
|
|
|
|
|
|
|
|
|
// dispatch props
|
|
|
|
|
fetchSources: (p1: Repository, p2: string, p3: string) => void;
|
|
|
|
|
};
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2020-01-14 18:07:57 +01:00
|
|
|
class Sources extends React.Component<Props> {
|
2018-09-27 16:32:37 +02:00
|
|
|
componentDidMount() {
|
2020-01-14 18:07:57 +01:00
|
|
|
const { repository, branches, selectedBranch, baseUrl, revision, path, fetchSources } = this.props;
|
2019-11-01 13:52:53 +01:00
|
|
|
fetchSources(repository, this.decodeRevision(revision), path);
|
2020-01-14 18:07:57 +01:00
|
|
|
if (branches?.length > 0 && !selectedBranch) {
|
|
|
|
|
const defaultBranch = branches?.filter(b => b.defaultBranch === true)[0];
|
|
|
|
|
this.props.history.push(`${baseUrl}/sources/${encodeURIComponent(defaultBranch.name)}/`);
|
|
|
|
|
}
|
2018-10-25 11:27:31 +02:00
|
|
|
}
|
2019-09-04 10:17:57 +02:00
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-09-04 15:06:04 +02:00
|
|
|
const { fetchSources, repository, revision, path } = this.props;
|
2018-10-25 11:27:31 +02:00
|
|
|
if (prevProps.revision !== revision || prevProps.path !== path) {
|
2019-11-01 13:52:53 +01:00
|
|
|
fetchSources(repository, this.decodeRevision(revision), path);
|
2018-10-25 11:27:31 +02:00
|
|
|
}
|
2019-09-04 15:06:04 +02:00
|
|
|
}
|
2019-09-04 10:17:57 +02:00
|
|
|
|
2019-11-01 13:52:53 +01:00
|
|
|
decodeRevision = (revision: string) => {
|
|
|
|
|
return revision ? decodeURIComponent(revision) : revision;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-14 18:07:57 +01:00
|
|
|
onSelectBranch = (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) {
|
2020-01-14 18:07:57 +01:00
|
|
|
url = `${baseUrl}/sources/${encodeURIComponent(branch.name)}/${path}`;
|
|
|
|
|
url = !url.endsWith("/") ? url + "/" : url;
|
2018-10-22 11:26:03 +02:00
|
|
|
} else {
|
2020-01-14 18:07:57 +01:00
|
|
|
url = `${baseUrl}/sources/${encodeURIComponent(branch.name)}/`;
|
2018-10-22 11:26:03 +02:00
|
|
|
}
|
2018-10-19 09:17:26 +02:00
|
|
|
} else {
|
2020-01-14 18:07:57 +01:00
|
|
|
return;
|
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
|
|
|
};
|
|
|
|
|
|
2020-01-14 18:07:57 +01:00
|
|
|
evaluateSwitchViewLink = () => {
|
|
|
|
|
const { baseUrl, selectedBranch } = this.props;
|
|
|
|
|
if (selectedBranch) {
|
|
|
|
|
return `${baseUrl}/branch/${encodeURIComponent(selectedBranch)}/changesets/`;
|
|
|
|
|
}
|
|
|
|
|
return `${baseUrl}/changesets/`;
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
render() {
|
2020-01-14 18:07:57 +01:00
|
|
|
const {
|
|
|
|
|
repository,
|
|
|
|
|
baseUrl,
|
|
|
|
|
branches,
|
|
|
|
|
selectedBranch,
|
|
|
|
|
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 (
|
2020-01-14 18:07:57 +01:00
|
|
|
<>
|
|
|
|
|
<CodeActionBar
|
|
|
|
|
selectedBranch={selectedBranch}
|
|
|
|
|
branches={branches}
|
|
|
|
|
onSelectBranch={this.onSelectBranch}
|
|
|
|
|
switchViewLink={this.evaluateSwitchViewLink()}
|
|
|
|
|
/>
|
|
|
|
|
<div className="panel">
|
|
|
|
|
{this.renderBreadcrumb()}
|
|
|
|
|
<FileTree repository={repository} revision={revision} path={path} baseUrl={baseUrl + "/sources"} />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
2018-10-25 11:27:31 +02:00
|
|
|
);
|
|
|
|
|
} else {
|
2020-01-14 18:07:57 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<CodeActionBar
|
|
|
|
|
selectedBranch={selectedBranch}
|
|
|
|
|
branches={branches}
|
|
|
|
|
onSelectBranch={this.onSelectBranch}
|
|
|
|
|
switchViewLink={this.evaluateSwitchViewLink()}
|
|
|
|
|
/>
|
|
|
|
|
<Content repository={repository} revision={revision} path={path} breadcrumb={this.renderBreadcrumb()} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
2018-10-25 11:27:31 +02:00
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
2018-10-19 09:17:26 +02:00
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
renderBreadcrumb = () => {
|
2020-01-14 18:07:57 +01:00
|
|
|
const { revision, selectedBranch, path, baseUrl, branches, sources, repository } = this.props;
|
2019-10-09 16:17:02 +02:00
|
|
|
|
2019-10-25 12:38:48 +02:00
|
|
|
return (
|
|
|
|
|
<Breadcrumb
|
2019-10-30 12:59:54 +01:00
|
|
|
repository={repository}
|
2019-10-25 12:38:48 +02:00
|
|
|
revision={revision}
|
|
|
|
|
path={path}
|
2020-01-14 18:07:57 +01:00
|
|
|
baseUrl={baseUrl + "/sources"}
|
|
|
|
|
branch={branches?.filter(b => b.name === selectedBranch)[0]}
|
|
|
|
|
defaultBranch={branches?.filter(b => b.defaultBranch === true)[0]}
|
2019-10-28 15:52:59 +01:00
|
|
|
sources={sources}
|
2019-10-25 12:38:48 +02:00
|
|
|
/>
|
|
|
|
|
);
|
2019-10-09 16:17:02 +02:00
|
|
|
};
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
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;
|
2019-11-01 13:52:53 +01: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);
|
2019-11-01 10:18:13 +01:00
|
|
|
const currentFileIsDirectory = decodedRevision
|
2018-10-25 13:59:13 +02:00
|
|
|
? isDirectory(state, repository, decodedRevision, path)
|
2019-11-01 10:18:13 +01:00
|
|
|
: isDirectory(state, repository, revision, path);
|
2019-11-01 09:29:57 +01:00
|
|
|
const sources = getSources(state, repository, decodedRevision, path);
|
2018-09-27 16:32:37 +02:00
|
|
|
|
|
|
|
|
return {
|
2018-10-19 16:02:21 +02:00
|
|
|
repository,
|
2019-11-01 09:29:57 +01:00
|
|
|
revision,
|
2018-10-22 10:23:45 +02:00
|
|
|
path,
|
2018-09-27 16:32:37 +02:00
|
|
|
loading,
|
|
|
|
|
error,
|
2019-10-28 15:52:59 +01:00
|
|
|
currentFileIsDirectory,
|
|
|
|
|
sources
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
const mapDispatchToProps = (dispatch: any) => {
|
2018-09-27 16:32:37 +02:00
|
|
|
return {
|
2018-10-25 11:27:31 +02:00
|
|
|
fetchSources: (repository: Repository, revision: string, path: string) => {
|
2019-11-01 13:52:53 +01:00
|
|
|
dispatch(fetchSources(repository, revision, path));
|
2019-10-20 18:02:52 +02:00
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-07 10:30:46 +01:00
|
|
|
export default compose(withTranslation("repos"), withRouter, connect(mapStateToProps, mapDispatchToProps))(Sources);
|