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-02-19 18:37:09 +01:00
|
|
|
import {
|
|
|
|
|
fetchSources,
|
|
|
|
|
getFetchSourcesFailure,
|
|
|
|
|
getHunkCount,
|
|
|
|
|
getSources,
|
2020-02-20 14:36:13 +01:00
|
|
|
isFetchSourcesPending
|
2020-02-19 18:37:09 +01:00
|
|
|
} from "../modules/sources";
|
2019-10-20 18:02:52 +02:00
|
|
|
import FileTreeLeaf from "./FileTreeLeaf";
|
2020-02-19 18:37:09 +01:00
|
|
|
import Button from "@scm-manager/ui-components/src/buttons/Button";
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
type Hunk = {
|
|
|
|
|
tree: File;
|
2019-10-19 16:38:07 +02:00
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
2020-02-19 18:37:09 +01:00
|
|
|
updateSources: (hunk: number) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
repository: Repository;
|
|
|
|
|
revision: string;
|
|
|
|
|
path: string;
|
|
|
|
|
baseUrl: string;
|
2020-02-18 17:56:22 +01:00
|
|
|
location: any;
|
2020-02-19 18:37:09 +01:00
|
|
|
hunks: Hunk[];
|
2019-10-09 16:17:02 +02:00
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
// dispatch props
|
|
|
|
|
fetchSources: (repository: Repository, revision: string, path: string, hunk: number) => void;
|
2019-12-17 10:25:53 +01: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-12-17 12:14:43 +01:00
|
|
|
type State = {
|
2020-02-19 18:37:09 +01:00
|
|
|
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);
|
2020-02-19 18:37:09 +01:00
|
|
|
this.state = { stoppableUpdateHandler: [] };
|
2019-12-17 12:14:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: Readonly<Props>, prevState: Readonly<State>): void {
|
|
|
|
|
if (prevState.stoppableUpdateHandler === this.state.stoppableUpdateHandler) {
|
2020-02-19 18:37:09 +01:00
|
|
|
const { hunks } = this.props;
|
|
|
|
|
hunks?.forEach((hunk, index) => {
|
|
|
|
|
if (hunk.tree?._embedded?.children && hunk.tree._embedded.children.find(c => c.partialResult)) {
|
|
|
|
|
const stoppableUpdateHandler = setTimeout(hunk.updateSources, 3000);
|
|
|
|
|
this.setState(prevState => {
|
|
|
|
|
return {
|
|
|
|
|
stoppableUpdateHandler: [...prevState.stoppableUpdateHandler, stoppableUpdateHandler]
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-12-17 12:14:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(): void {
|
2020-02-19 18:37:09 +01:00
|
|
|
this.state.stoppableUpdateHandler.forEach(handler => clearTimeout(handler));
|
2019-12-17 10:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
loadMore = () => {
|
2020-02-20 14:36:13 +01:00
|
|
|
this.props.fetchSources(this.props.repository, this.props.revision, this.props.path, this.props.hunks.length);
|
2020-02-19 18:37:09 +01:00
|
|
|
};
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
render() {
|
2020-02-19 18:37:09 +01:00
|
|
|
const { hunks, t } = this.props;
|
2018-10-17 16:01:40 +02:00
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
if (!hunks || hunks.length === 0) {
|
|
|
|
|
return null;
|
2018-10-19 16:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
if (hunks.some(hunk => hunk.error)) {
|
|
|
|
|
return <ErrorNotification error={hunks.map(hunk => hunk.error)[0]} />;
|
2018-10-19 16:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
const lastHunk = hunks[hunks.length - 1];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="panel-block">
|
|
|
|
|
{this.renderSourcesTable()}
|
|
|
|
|
{lastHunk.loading && <Loading />}
|
|
|
|
|
{lastHunk.tree?.truncated && <Button label={t("sources.loadMore")} action={this.loadMore} />}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2019-04-09 15:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderSourcesTable() {
|
2020-02-19 18:37:09 +01:00
|
|
|
const { hunks, 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
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
hunks
|
|
|
|
|
.filter(hunk => !hunk.loading)
|
|
|
|
|
.forEach(hunk => {
|
|
|
|
|
if (hunk.tree?._embedded && hunk.tree._embedded.children) {
|
|
|
|
|
const children = [...hunk.tree._embedded.children];
|
|
|
|
|
files.push(...children);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2020-02-20 14:36:13 +01:00
|
|
|
const loading = hunks.filter(hunk => hunk.loading).length > 0;
|
|
|
|
|
|
|
|
|
|
if (loading || (files && files.length > 0)) {
|
2019-04-09 15:31:12 +02:00
|
|
|
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 {
|
2020-02-19 18:37:09 +01:00
|
|
|
baseUrlWithRevision += "/" + encodeURIComponent(hunks[0].tree.revision);
|
2020-02-18 17:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-09 15:31:12 +02:00
|
|
|
return (
|
2020-02-19 18:37:09 +01:00
|
|
|
<table className="table table-hover table-sm is-fullwidth">
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<FixedWidthTh />
|
|
|
|
|
<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.commitDate")}</th>
|
|
|
|
|
<th className="is-hidden-touch">{t("sources.file-tree.description")}</th>
|
|
|
|
|
{binder.hasExtension("repos.sources.tree.row.right") && <th className="is-hidden-mobile" />}
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{files.map((file: any) => (
|
|
|
|
|
<FileTreeLeaf key={file.name} file={file} baseUrl={baseUrlWithRevision} />
|
|
|
|
|
))}
|
|
|
|
|
</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;
|
|
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
return {
|
|
|
|
|
updateSources: (hunk: number) => dispatch(fetchSources(repository, revision, path, false, hunk)),
|
|
|
|
|
fetchSources: (repository: Repository, revision: string, path: string, hunk: number) => {
|
|
|
|
|
dispatch(fetchSources(repository, revision, path, true, hunk));
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-12-17 10:25:53 +01: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
|
|
|
|
2020-02-19 18:37:09 +01:00
|
|
|
const error = getFetchSourcesFailure(state, repository, revision, path, 0);
|
|
|
|
|
const hunkCount = getHunkCount(state, repository, revision, path);
|
|
|
|
|
const hunks = [];
|
|
|
|
|
for (let i = 0; i < hunkCount; ++i) {
|
|
|
|
|
console.log(`getting data for hunk ${i}`);
|
|
|
|
|
const tree = getSources(state, repository, revision, path, i);
|
|
|
|
|
const loading = isFetchSourcesPending(state, repository, revision, path, i);
|
|
|
|
|
hunks.push({
|
|
|
|
|
tree,
|
|
|
|
|
loading
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-19 16:02:21 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
revision,
|
|
|
|
|
path,
|
2018-10-23 10:41:10 +02:00
|
|
|
error,
|
2020-02-19 18:37:09 +01:00
|
|
|
hunks
|
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));
|