From 7b807fa880c705493869479bbc1a69ce649a941d Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 28 Sep 2018 12:12:56 +0200 Subject: [PATCH] keep entered revision in source browser --- .../src/repos/sources/components/FileTree.js | 11 +++++++-- .../repos/sources/components/FileTreeLeaf.js | 21 ++++++++++++---- .../sources/components/FileTreeLeaf.test.js | 24 +++++++++++++++++++ .../src/repos/sources/containers/Sources.js | 11 +++++++-- 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 scm-ui/src/repos/sources/components/FileTreeLeaf.test.js diff --git a/scm-ui/src/repos/sources/components/FileTree.js b/scm-ui/src/repos/sources/components/FileTree.js index 05a775c77a..d606ece0b8 100644 --- a/scm-ui/src/repos/sources/components/FileTree.js +++ b/scm-ui/src/repos/sources/components/FileTree.js @@ -13,6 +13,7 @@ const styles = { type Props = { tree: SourcesCollection, + revision: string, path: string, baseUrl: string, @@ -35,8 +36,14 @@ export function findParent(path: string) { class FileTree extends React.Component { render() { - const { tree, path, baseUrl, classes, t } = this.props; - const baseUrlWithRevision = baseUrl + "/" + tree.revision; + const { tree, revision, path, baseUrl, classes, t } = this.props; + + let baseUrlWithRevision = baseUrl; + if (revision) { + baseUrlWithRevision += "/" + revision; + } else { + baseUrlWithRevision += "/" + tree.revision; + } const files = []; if (path) { diff --git a/scm-ui/src/repos/sources/components/FileTreeLeaf.js b/scm-ui/src/repos/sources/components/FileTreeLeaf.js index 29d7d35ebe..7751f4dff9 100644 --- a/scm-ui/src/repos/sources/components/FileTreeLeaf.js +++ b/scm-ui/src/repos/sources/components/FileTreeLeaf.js @@ -21,13 +21,24 @@ type Props = { classes: any }; +export function createLink(base: string, file: File) { + let link = base; + if (file.path) { + let path = file.path; + if (path.startsWith("/")) { + path = path.substring(1); + } + link += "/" + path; + } + if (!link.endsWith("/")) { + link += "/"; + } + return link; +} + class FileTreeLeaf extends React.Component { createLink = (file: File) => { - let link = this.props.baseUrl; - if (file.path) { - link += "/" + file.path + "/"; - } - return link; + return createLink(this.props.baseUrl, file); }; createFileIcon = (file: File) => { diff --git a/scm-ui/src/repos/sources/components/FileTreeLeaf.test.js b/scm-ui/src/repos/sources/components/FileTreeLeaf.test.js new file mode 100644 index 0000000000..d5004521c8 --- /dev/null +++ b/scm-ui/src/repos/sources/components/FileTreeLeaf.test.js @@ -0,0 +1,24 @@ +// @flow + +import { createLink } from "./FileTreeLeaf"; +import type { File } from "@scm-manager/ui-types"; + +describe("create link tests", () => { + function dir(path: string): File { + return { + name: "dir", + path: path, + directory: true + }; + } + + it("should create link", () => { + expect(createLink("src", dir("main"))).toBe("src/main/"); + expect(createLink("src", dir("/main"))).toBe("src/main/"); + expect(createLink("src", dir("/main/"))).toBe("src/main/"); + }); + + it("should return base url if the directory path is empty", () => { + expect(createLink("src", dir(""))).toBe("src/"); + }); +}); diff --git a/scm-ui/src/repos/sources/containers/Sources.js b/scm-ui/src/repos/sources/containers/Sources.js index 12a1a29759..fcfd0c284a 100644 --- a/scm-ui/src/repos/sources/containers/Sources.js +++ b/scm-ui/src/repos/sources/containers/Sources.js @@ -37,7 +37,7 @@ class Sources extends React.Component { } render() { - const { sources, path, baseUrl, loading, error } = this.props; + const { sources, revision, path, baseUrl, loading, error } = this.props; if (error) { return ; @@ -47,7 +47,14 @@ class Sources extends React.Component { return ; } - return ; + return ( + + ); } }