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

169 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-09-27 16:32:37 +02:00
//@flow
import React from "react";
import { translate } from "react-i18next";
2018-10-19 16:02:21 +02:00
import { connect } from "react-redux";
2018-09-27 16:32:37 +02:00
import injectSheet from "react-jss";
import FileTreeLeaf from "./FileTreeLeaf";
2018-10-19 16:02:21 +02:00
import type { Repository, File } from "@scm-manager/ui-types";
2019-04-09 15:31:12 +02:00
import {
ErrorNotification,
Loading,
Notification
} from "@scm-manager/ui-components";
2018-10-19 16:02:21 +02:00
import {
getFetchSourcesFailure,
isFetchSourcesPending,
getSources
} from "../modules/sources";
import { withRouter } from "react-router-dom";
import { compose } from "redux";
import { binder } from "@scm-manager/ui-extensions";
2018-09-27 16:32:37 +02:00
const styles = {
iconColumn: {
width: "16px"
}
};
type Props = {
2018-10-19 16:02:21 +02:00
loading: boolean,
error: Error,
tree: File,
2018-10-19 16:02:21 +02:00
repository: Repository,
revision: string,
path: string,
baseUrl: string,
2018-09-27 16:32:37 +02:00
// context props
classes: any,
2018-10-19 16:02:21 +02:00
t: string => string,
match: any
2018-09-27 16:32:37 +02:00
};
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, classes, t } = this.props;
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) {
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) {
baseUrlWithRevision += "/" + encodeURIComponent(revision);
} else {
baseUrlWithRevision += "/" + encodeURIComponent(tree.revision);
}
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>
<th className={classes.iconColumn} />
<th>{t("sources.file-tree.name")}</th>
<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("sourceView.right") && (
<th className="is-hidden-mobile" />
)}
2019-02-01 10:12:45 +01:00
</tr>
</thead>
<tbody>
{files.map(file => (
<FileTreeLeaf
key={file.name}
file={file}
baseUrl={baseUrlWithRevision}
/>
))}
</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,
2018-10-19 16:02:21 +02:00
tree
};
};
export default compose(
withRouter,
connect(mapStateToProps)
2018-10-19 16:02:21 +02:00
)(injectSheet(styles)(translate("repos")(FileTree)));