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

228 lines
6.5 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 { 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";
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;
loading: boolean;
error: Error;
2020-02-19 18:37:09 +01:00
};
type Props = WithTranslation & {
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[];
2020-02-19 18:37:09 +01:00
// dispatch props
fetchSources: (repository: Repository, revision: string, path: string, hunk: number) => void;
2020-02-21 09:45:43 +01:00
updateSources: (hunk: number) => () => void;
2018-09-27 16:32:37 +02:00
// context props
match: any;
2018-09-27 16:32:37 +02:00
};
type State = {
2020-02-19 18:37:09 +01:00
stoppableUpdateHandler: number[];
2019-12-17 12:42:05 +01: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 "";
}
class FileTree extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
2020-02-19 18:37:09 +01:00
this.state = { stoppableUpdateHandler: [] };
}
componentDidUpdate(prevProps: Readonly<Props>, prevState: Readonly<State>): void {
if (prevState.stoppableUpdateHandler === this.state.stoppableUpdateHandler) {
2020-02-21 09:45:43 +01:00
const { hunks, updateSources } = this.props;
2020-02-19 18:37:09 +01:00
hunks?.forEach((hunk, index) => {
if (hunk.tree?._embedded?.children && hunk.tree._embedded.children.find(c => c.partialResult)) {
2020-02-21 09:45:43 +01:00
const stoppableUpdateHandler = setTimeout(updateSources(index), 3000);
2020-02-19 18:37:09 +01:00
this.setState(prevState => {
return {
stoppableUpdateHandler: [...prevState.stoppableUpdateHandler, stoppableUpdateHandler]
};
});
}
});
}
}
componentWillUnmount(): void {
2020-02-19 18:37:09 +01:00
this.state.stoppableUpdateHandler.forEach(handler => clearTimeout(handler));
}
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
};
2020-02-21 11:06:21 +01:00
renderTruncatedInfo = () => {
2020-02-19 18:37:09 +01:00
const { hunks, t } = this.props;
2020-02-21 11:06:21 +01:00
const lastHunk = hunks[hunks.length - 1];
2020-03-09 09:48:14 +01:00
const fileCount = hunks
2020-02-21 11:06:21 +01:00
.filter(hunk => hunk?.tree?._embedded?.children)
2020-03-09 09:48:14 +01:00
.map(hunk => hunk.tree._embedded.children.filter(c => !c.directory).length)
2020-02-21 11:06:21 +01:00
.reduce((a, b) => a + b, 0);
if (lastHunk.tree?.truncated) {
return (
<Notification type={"info"}>
<div className={"columns is-centered"}>
2020-03-09 09:48:14 +01:00
<div className={"column"}>{t("sources.moreFilesAvailable", { count: fileCount })}</div>
2020-02-21 11:06:21 +01:00
<Button label={t("sources.loadMore")} action={this.loadMore} />
</div>
</Notification>
);
}
};
render() {
const { hunks } = this.props;
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
return (
<div className="panel-block">
{this.renderSourcesTable()}
2020-02-21 11:06:21 +01:00
{this.renderTruncatedInfo()}
2020-02-19 18:37:09 +01:00
</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
const files = [];
if (path) {
files.push({
name: "..",
path: findParent(path),
directory: true
});
}
2020-02-21 07:37:55 +01:00
if (hunks.every(hunk => hunk.loading)) {
return <Loading />;
}
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) {
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-21 07:37:55 +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>
{hunks[hunks.length - 1].loading && <Loading />}
</>
2019-04-09 15:31:12 +02:00
);
}
return <Notification type="info">{t("sources.noSources")}</Notification>;
2018-09-27 16:32:37 +02:00
}
}
const mapDispatchToProps = (dispatch: any, ownProps: Props) => {
const { repository, revision, path } = ownProps;
2020-02-19 18:37:09 +01:00
return {
2020-02-21 09:45:43 +01:00
updateSources: (hunk: number) => () => dispatch(fetchSources(repository, revision, path, false, hunk)),
2020-02-19 18:37:09 +01:00
fetchSources: (repository: Repository, revision: string, path: string, hunk: number) => {
dispatch(fetchSources(repository, revision, path, true, hunk));
}
};
};
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) {
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));