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";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { File, Repository } from "@scm-manager/ui-types";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { ErrorNotification, Loading, Notification } from "@scm-manager/ui-components";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { fetchSources, getFetchSourcesFailure, getSources, isFetchSourcesPending } 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
|
|
|
|
2019-12-17 10:25:53 +01:00
|
|
|
updateSources: () => void;
|
|
|
|
|
|
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-12-17 12:14:43 +01:00
|
|
|
type State = {
|
|
|
|
|
stoppableUpdateHandler?: number;
|
2019-12-17 12:42:05 +01:00
|
|
|
};
|
2019-12-17 12:14:43 +01: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
|
|
|
}
|
|
|
|
|
|
2019-12-17 12:14:43 +01:00
|
|
|
class FileTree extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: Readonly<Props>, prevState: Readonly<State>): void {
|
|
|
|
|
if (prevState.stoppableUpdateHandler === this.state.stoppableUpdateHandler) {
|
2019-12-17 12:42:05 +01:00
|
|
|
const { tree, updateSources } = this.props;
|
2019-12-17 12:14:43 +01:00
|
|
|
if (tree?._embedded?.children && tree._embedded.children.find(c => c.partialResult)) {
|
|
|
|
|
const stoppableUpdateHandler = setTimeout(updateSources, 3000);
|
2019-12-17 12:42:05 +01:00
|
|
|
this.setState({ stoppableUpdateHandler: stoppableUpdateHandler });
|
2019-12-17 12:14:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(): void {
|
|
|
|
|
if (this.state.stoppableUpdateHandler) {
|
|
|
|
|
clearTimeout(this.state.stoppableUpdateHandler);
|
2019-12-17 10:25:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
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) {
|
2019-10-30 14:52:46 +01:00
|
|
|
const children = [...tree._embedded.children].sort(compareFiles);
|
|
|
|
|
files.push(...children);
|
2018-10-23 13:40:27 +02:00
|
|
|
}
|
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>
|
2019-12-18 11:48:17 +01:00
|
|
|
<th className="is-hidden-mobile">{t("sources.file-tree.commitDate")}</th>
|
2019-12-11 23:15:52 +01:00
|
|
|
<th className="is-hidden-touch">{t("sources.file-tree.description")}</th>
|
2019-10-21 10:57:56 +02:00
|
|
|
{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
|
|
|
|
2019-12-17 10:25:53 +01:00
|
|
|
const mapDispatchToProps = (dispatch: any, ownProps: Props) => {
|
|
|
|
|
const { repository, revision, path } = ownProps;
|
|
|
|
|
|
|
|
|
|
const updateSources = () => dispatch(fetchSources(repository, revision, path, false));
|
|
|
|
|
|
|
|
|
|
return { updateSources };
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
export default compose(withRouter, connect(mapStateToProps, mapDispatchToProps))(withTranslation("repos")(FileTree));
|