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

222 lines
6.1 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,
isFetchSourcesPending, isUpdateSourcePending
} 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
updateSources: (hunk: number) => void;
};
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;
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-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]
};
});
}
});
}
}
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 = () => {
// console.log("smth");
};
2018-09-27 16:32:37 +02:00
render() {
2020-02-19 18:37:09 +01:00
const { hunks, t } = 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
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
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);
}
}
};
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
2019-04-09 15:31:12 +02:00
if (files && files.length > 0) {
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-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
);
}
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 {
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));
}
};
};
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 loading = isFetchSourcesPending(state, repository, revision, path, 0);
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
loading,
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));