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

185 lines
4.1 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";
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
import {
fetchSources,
getFetchSourcesFailure,
isFetchSourcesPending,
getSources
} from "../modules/sources";
import { withRouter } from "react-router-dom";
import { compose } from "redux";
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-10-19 16:02:21 +02:00
fetchSources: (Repository, string, string) => void,
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> {
2018-10-19 16:02:21 +02:00
componentDidMount() {
const { fetchSources, repository, revision, path } = this.props;
fetchSources(repository, revision, path);
}
2018-10-23 10:41:10 +02:00
componentDidUpdate(prevProps) {
const { fetchSources, repository, revision, path } = this.props;
if (prevProps.revision !== revision || prevProps.path !== path) {
fetchSources(repository, revision, path);
}
}
2018-09-27 16:32:37 +02:00
render() {
2018-10-19 16:02:21 +02:00
const {
error,
loading,
tree,
revision,
path,
baseUrl,
classes,
t
} = this.props;
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-10-19 16:02:21 +02:00
if (error) {
return <ErrorNotification error={error} />;
}
if (loading) {
return <Loading />;
}
if (!tree) {
return null;
}
const files = [];
if (path) {
files.push({
name: "..",
path: findParent(path),
directory: true
});
}
if (tree._embedded) {
files.push(...tree._embedded.children.sort(compareFiles));
}
2018-09-27 16:32:37 +02:00
let baseUrlWithRevision = baseUrl;
if (revision) {
baseUrlWithRevision += "/" + revision;
} else {
baseUrlWithRevision += "/" + tree.revision;
}
2018-09-27 16:32:37 +02:00
return (
<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>
2018-09-27 16:32:37 +02:00
<th>{t("sources.file-tree.description")}</th>
</tr>
</thead>
<tbody>
{files.map(file => (
<FileTreeLeaf
key={file.name}
file={file}
baseUrl={baseUrlWithRevision}
/>
2018-09-27 16:32:37 +02:00
))}
</tbody>
</table>
);
}
}
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
};
};
const mapDispatchToProps = dispatch => {
return {
fetchSources: (repository: Repository, revision: string, path: string) => {
dispatch(fetchSources(repository, revision, path));
}
};
};
export default compose(
withRouter,
connect(
mapStateToProps,
mapDispatchToProps
)
)(injectSheet(styles)(translate("repos")(FileTree)));