Files
SCM-Manager/scm-ui/ui-webapp/src/repos/sources/containers/Sources.tsx

175 lines
5.2 KiB
TypeScript
Raw Normal View History

import React from "react";
import { connect } from "react-redux";
2020-01-14 18:07:57 +01:00
import { withRouter, RouteComponentProps } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next";
import { Branch, Repository } from "@scm-manager/ui-types";
import { Breadcrumb, ErrorNotification, Loading } from "@scm-manager/ui-components";
import FileTree from "../components/FileTree";
import { getFetchBranchesFailure, isFetchBranchesPending } from "../../branches/modules/branches";
import { compose } from "redux";
import Content from "./Content";
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;
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)}/`);
}
}
componentDidUpdate(prevProps: Props) {
2019-09-04 15:06:04 +02:00
const { fetchSources, repository, revision, path } = this.props;
if (prevProps.revision !== revision || prevProps.path !== path) {
fetchSources(repository, this.decodeRevision(revision), path);
}
2019-09-04 15:06:04 +02:00
}
decodeRevision = (revision: string) => {
return revision ? decodeURIComponent(revision) : revision;
};
2020-01-14 18:07:57 +01:00
onSelectBranch = (branch?: Branch) => {
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 />;
}
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>
</>
);
} 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-09-27 16:32:37 +02:00
}
2018-10-19 09:17:26 +02:00
renderBreadcrumb = () => {
2020-01-14 18:07:57 +01:00
const { revision, selectedBranch, path, baseUrl, branches, sources, repository } = this.props;
return (
<Breadcrumb
repository={repository}
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}
/>
);
};
2018-09-27 16:32:37 +02:00
}
const mapStateToProps = (state: any, ownProps: Props) => {
const { repository, match } = ownProps;
2018-10-23 10:41:10 +02:00
const { revision, path } = match.params;
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
? isDirectory(state, repository, decodedRevision, path)
2019-11-01 10:18:13 +01:00
: isDirectory(state, repository, revision, path);
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,
revision,
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
};
};
const mapDispatchToProps = (dispatch: any) => {
2018-09-27 16:32:37 +02:00
return {
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
}
2018-09-27 16:32:37 +02:00
};
};
export default compose(withTranslation("repos"), withRouter, connect(mapStateToProps, mapDispatchToProps))(Sources);