2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { compose } from "redux";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { withRouter } 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 styled from "styled-components";
|
|
|
|
|
import { binder } from "@scm-manager/ui-extensions";
|
|
|
|
|
import { Repository, File } from "@scm-manager/ui-types";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { ErrorNotification, Loading, Notification } from "@scm-manager/ui-components";
|
|
|
|
|
import { getFetchSourcesFailure, isFetchSourcesPending, getSources } from "../modules/sources";
|
2019-10-20 18:02:52 +02:00
|
|
|
import FileTreeLeaf from "./FileTreeLeaf";
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
tree: File;
|
|
|
|
|
repository: Repository;
|
|
|
|
|
revision: string;
|
|
|
|
|
path: string;
|
|
|
|
|
baseUrl: string;
|
2019-10-09 16:17:02 +02:00
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
// context props
|
2019-10-19 16:38:07 +02:00
|
|
|
match: any;
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
const FixedWidthTh = styled.th`
|
|
|
|
|
width: 16px;
|
|
|
|
|
`;
|
|
|
|
|
|
2018-09-28 11:31:38 +02:00
|
|
|
export function findParent(path: string) {
|
2019-10-20 18:02:52 +02:00
|
|
|
if (path.endsWith("/")) {
|
2018-10-19 16:02:21 +02:00
|
|
|
path = path.substring(0, path.length - 1);
|
2018-09-28 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
const index = path.lastIndexOf("/");
|
2018-09-28 11:31:38 +02:00
|
|
|
if (index > 0) {
|
|
|
|
|
return path.substring(0, index);
|
|
|
|
|
}
|
2019-10-20 18:02:52 +02:00
|
|
|
return "";
|
2018-09-28 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
class FileTree extends React.Component<Props> {
|
|
|
|
|
render() {
|
2019-04-09 15:31:12 +02:00
|
|
|
const { error, loading, tree } = this.props;
|
2018-10-17 16:01:40 +02:00
|
|
|
|
2018-10-19 16:02:21 +02:00
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
if (!tree) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 15:31:12 +02:00
|
|
|
return <div className="panel-block">{this.renderSourcesTable()}</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderSourcesTable() {
|
2019-10-09 16:17:02 +02:00
|
|
|
const { tree, revision, path, baseUrl, t } = this.props;
|
2019-04-09 15:31:12 +02:00
|
|
|
|
2018-09-28 11:31:38 +02:00
|
|
|
const files = [];
|
2018-10-23 13:40:27 +02:00
|
|
|
|
2018-09-28 11:31:38 +02:00
|
|
|
if (path) {
|
|
|
|
|
files.push({
|
2019-10-20 18:02:52 +02:00
|
|
|
name: "..",
|
2018-09-28 11:31:38 +02:00
|
|
|
path: findParent(path),
|
2019-10-20 18:02:52 +02:00
|
|
|
directory: true
|
2018-09-28 11:31:38 +02:00
|
|
|
});
|
|
|
|
|
}
|
2018-10-17 16:01:40 +02:00
|
|
|
|
2019-04-09 15:31:12 +02:00
|
|
|
const compareFiles = function(f1: File, f2: File): number {
|
|
|
|
|
if (f1.directory) {
|
|
|
|
|
if (f2.directory) {
|
|
|
|
|
return f1.name.localeCompare(f2.name);
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (f2.directory) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return f1.name.localeCompare(f2.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-07 16:42:26 +01:00
|
|
|
if (tree._embedded && tree._embedded.children) {
|
2018-10-23 13:40:27 +02:00
|
|
|
files.push(...tree._embedded.children.sort(compareFiles));
|
|
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2019-04-09 15:31:12 +02:00
|
|
|
if (files && files.length > 0) {
|
|
|
|
|
let baseUrlWithRevision = baseUrl;
|
|
|
|
|
if (revision) {
|
2019-10-20 18:02:52 +02:00
|
|
|
baseUrlWithRevision += "/" + encodeURIComponent(revision);
|
2019-04-09 15:31:12 +02:00
|
|
|
} else {
|
2019-10-20 18:02:52 +02:00
|
|
|
baseUrlWithRevision += "/" + encodeURIComponent(tree.revision);
|
2019-04-09 15:31:12 +02:00
|
|
|
}
|
2018-10-22 08:00:34 +02:00
|
|
|
|
2019-04-09 15:31:12 +02:00
|
|
|
return (
|
2019-02-01 10:12:45 +01:00
|
|
|
<table className="table table-hover table-sm is-fullwidth">
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
2019-10-09 16:17:02 +02:00
|
|
|
<FixedWidthTh />
|
2019-10-20 18:02:52 +02:00
|
|
|
<th>{t("sources.file-tree.name")}</th>
|
2019-10-21 10:57:56 +02:00
|
|
|
<th className="is-hidden-mobile">{t("sources.file-tree.length")}</th>
|
|
|
|
|
<th className="is-hidden-mobile">{t("sources.file-tree.lastModified")}</th>
|
|
|
|
|
<th className="is-hidden-mobile">{t("sources.file-tree.description")}</th>
|
|
|
|
|
{binder.hasExtension("repos.sources.tree.row.right") && <th className="is-hidden-mobile" />}
|
2019-02-01 10:12:45 +01:00
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{files.map(file => (
|
2019-10-21 10:57:56 +02:00
|
|
|
<FileTreeLeaf key={file.name} file={file} baseUrl={baseUrlWithRevision} />
|
2019-02-01 10:12:45 +01:00
|
|
|
))}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2019-04-09 15:31:12 +02:00
|
|
|
);
|
|
|
|
|
}
|
2019-10-20 18:02:52 +02:00
|
|
|
return <Notification type="info">{t("sources.noSources")}</Notification>;
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-17 16:01:40 +02:00
|
|
|
|
2018-10-19 16:02:21 +02:00
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
2018-10-23 10:41:10 +02:00
|
|
|
const { repository, revision, path } = ownProps;
|
2018-10-19 16:02:21 +02:00
|
|
|
|
|
|
|
|
const loading = isFetchSourcesPending(state, repository, revision, path);
|
|
|
|
|
const error = getFetchSourcesFailure(state, repository, revision, path);
|
|
|
|
|
const tree = getSources(state, repository, revision, path);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
revision,
|
|
|
|
|
path,
|
2018-10-23 10:41:10 +02:00
|
|
|
loading,
|
|
|
|
|
error,
|
2019-10-20 18:02:52 +02:00
|
|
|
tree
|
2018-10-19 16:02:21 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withRouter,
|
2019-10-20 18:02:52 +02:00
|
|
|
connect(mapStateToProps)
|
2019-10-23 15:47:08 +02:00
|
|
|
)(withTranslation("repos")(FileTree));
|