Files
SCM-Manager/scm-ui/ui-webapp/src/repos/sources/components/FileTree.tsx

146 lines
3.8 KiB
TypeScript
Raw Normal View History

import React from "react";
import { compose } from "redux";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
import { WithTranslation, withTranslation } from "react-i18next";
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";
import FileTreeLeaf from "./FileTreeLeaf";
2018-09-27 16:32:37 +02:00
type Props = WithTranslation & {
loading: boolean;
error: Error;
tree: File;
repository: Repository;
revision: string;
path: string;
baseUrl: string;
2018-09-27 16:32:37 +02:00
// context props
match: any;
2018-09-27 16:32:37 +02:00
};
const FixedWidthTh = styled.th`
width: 16px;
`;
export function findParent(path: string) {
if (path.endsWith("/")) {
2018-10-19 16:02:21 +02:00
path = path.substring(0, path.length - 1);
}
const index = path.lastIndexOf("/");
if (index > 0) {
return path.substring(0, index);
}
return "";
}
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-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() {
const { tree, revision, path, baseUrl, t } = this.props;
2019-04-09 15:31:12 +02:00
const files = [];
if (path) {
files.push({
name: "..",
path: findParent(path),
directory: true
});
}
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) {
const children = [...tree._embedded.children].sort(compareFiles);
files.push(...children);
}
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) {
baseUrlWithRevision += "/" + encodeURIComponent(revision);
2019-04-09 15:31:12 +02:00
} else {
baseUrlWithRevision += "/" + encodeURIComponent(tree.revision);
2019-04-09 15:31:12 +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>
<FixedWidthTh />
<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
);
}
return <Notification type="info">{t("sources.noSources")}</Notification>;
2018-09-27 16:32:37 +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,
tree
2018-10-19 16:02:21 +02:00
};
};
export default compose(
withRouter,
connect(mapStateToProps)
)(withTranslation("repos")(FileTree));