Files
SCM-Manager/scm-ui/ui-webapp/src/repos/sources/components/FileTree.tsx
Konstantin Schaper 4d203ff36f codify extension points docs (#1947)
This pull request converts the current incomplete textual documentation of the available frontend extension points to in-code definitions that act both as documentation and as type helpers for improving overall code quality. All extension points available in the SCM-Manager core are now available, but no plugin was updated and only those parts of the core codebase had the new types added that did not require runtime changes. The only exception to this is the breadcrumbs, which was a simple change that is fully backwards-compatible.
2022-03-29 15:04:14 +02:00

124 lines
3.9 KiB
TypeScript

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { FC } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { binder, ExtensionPoint, extensionPoints } from "@scm-manager/ui-extensions";
import { File, Repository } from "@scm-manager/ui-types";
import FileTreeLeaf from "./FileTreeLeaf";
import TruncatedNotification from "./TruncatedNotification";
import { isRootPath } from "../utils/files";
type Props = {
repository: Repository;
directory: File;
baseUrl: string;
revision: string;
fetchNextPage: () => void;
isFetchingNextPage: boolean;
};
const FixedWidthTh = styled.th`
width: 16px;
`;
export function findParent(path: string) {
if (path.endsWith("/")) {
path = path.substring(0, path.length - 1);
}
const index = path.lastIndexOf("/");
if (index > 0) {
return path.substring(0, index);
}
return "";
}
const FileTree: FC<Props> = ({ repository, directory, baseUrl, revision, fetchNextPage, isFetchingNextPage }) => {
const [t] = useTranslation("repos");
const { path } = directory;
const files: File[] = [];
if (!isRootPath(path)) {
files.push({
name: "..",
path: findParent(path),
directory: true,
revision,
_links: {},
_embedded: {
children: []
}
});
}
files.push(...(directory._embedded?.children || []));
const baseUrlWithRevision = baseUrl + "/" + encodeURIComponent(revision);
const extProps: extensionPoints.ReposSourcesTreeWrapperProps = {
repository,
directory,
baseUrl,
revision
};
return (
<div className="panel-block">
<ExtensionPoint<extensionPoints.ReposSourcesTreeWrapper>
name="repos.source.tree.wrapper"
props={extProps}
renderAll={true}
wrapper={true}
>
<table className="table table-hover table-sm is-fullwidth">
<thead>
<tr>
<FixedWidthTh />
<th>{t("sources.fileTree.name")}</th>
<th className="is-hidden-mobile">{t("sources.fileTree.length")}</th>
<th className="is-hidden-mobile">{t("sources.fileTree.commitDate")}</th>
<th className="is-hidden-touch">{t("sources.fileTree.description")}</th>
{binder.hasExtension("repos.sources.tree.row.right") && <th className="is-hidden-mobile" />}
</tr>
</thead>
<tbody>
{files.map((file: File) => (
<FileTreeLeaf key={file.name} file={file} baseUrl={baseUrlWithRevision} repository={repository} />
))}
</tbody>
</table>
<TruncatedNotification
directory={directory}
fetchNextPage={fetchNextPage}
isFetchingNextPage={isFetchingNextPage}
/>
</ExtensionPoint>
</div>
);
};
export default FileTree;