From ead3062f9c40c0da0caf682ae1cbf1f0f75449a8 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Tue, 19 Feb 2019 14:48:22 +0100 Subject: [PATCH] added protocol switcher to git protocol information --- .../src/main/js/CloneInformation.js | 57 ++++++++ .../src/main/js/ProtocolInformation.js | 133 ++++++++++++------ 2 files changed, 148 insertions(+), 42 deletions(-) create mode 100644 scm-plugins/scm-git-plugin/src/main/js/CloneInformation.js diff --git a/scm-plugins/scm-git-plugin/src/main/js/CloneInformation.js b/scm-plugins/scm-git-plugin/src/main/js/CloneInformation.js new file mode 100644 index 0000000000..177e662335 --- /dev/null +++ b/scm-plugins/scm-git-plugin/src/main/js/CloneInformation.js @@ -0,0 +1,57 @@ +//@flow +import React from "react"; +import { translate } from "react-i18next"; +import type { Repository } from "@scm-manager/ui-types"; + +type Props = { + url: string, + repository: Repository, + + // context props + t: (string) => string +}; + +class CloneInformation extends React.Component { + + render() { + const { url, repository, t } = this.props; + + return ( +
+

{t("scm-git-plugin.information.clone")}

+
+          git clone {url}
+        
+

{t("scm-git-plugin.information.create")}

+
+          
+            git init {repository.name}
+            
+ echo "# {repository.name}" > README.md +
+ git add README.md +
+ git commit -m "added readme" +
+ git remote add origin {url} +
+ git push -u origin master +
+
+
+

{t("scm-git-plugin.information.replace")}

+
+          
+            git remote add origin {url}
+            
+ git push -u origin master +
+
+
+
+ ); + } + +} + +export default translate("plugins")(CloneInformation); diff --git a/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js b/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js index c6aed483e7..e4a1c451d3 100644 --- a/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js +++ b/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js @@ -1,59 +1,108 @@ //@flow import React from "react"; -import { repositories } from "@scm-manager/ui-components"; +import { ButtonGroup, Button } from "@scm-manager/ui-components"; import type { Repository } from "@scm-manager/ui-types"; -import { translate } from "react-i18next"; +import CloneInformation from "./CloneInformation"; +import type { Link } from "@scm-manager/ui-types"; +import injectSheets from "react-jss"; + +const styles = { + protocols: { + position: "relative" + }, + switcher: { + position: "absolute", + top: 0, + right: 0 + } +}; type Props = { repository: Repository, - t: string => string + + // context props + classes: Object } -class ProtocolInformation extends React.Component { +type State = { + selected?: Link +}; - render() { - const { repository, t } = this.props; - const href = repositories.getProtocolLinkByType(repository, "http"); - if (!href) { - return null; +function selectHttpOrFirst(repository: Repository) { + const protocols = repository._links["protocol"] || []; + + for (let protocol of protocols) { + if (protocol.name === "http") { + return protocol; + } + } + + if (protocols.length > 0) { + return protocols[0]; + } + return undefined; +} + +class ProtocolInformation extends React.Component { + + constructor(props: Props) { + super(props); + this.state = { + selected: selectHttpOrFirst(props.repository) + }; + } + + selectProtocol = (protocol: Link) => { + this.setState({ + selected: protocol + }); + }; + + renderProtocolButton = (protocol: Link) => { + const name = protocol.name || "unknown"; + + let color = null; + + const { selected } = this.state; + if ( selected && protocol.name === selected.name ) { + color = "link is-selected"; } return ( -
-

{t("scm-git-plugin.information.clone")}

-
-          git clone {href}
-        
-

{t("scm-git-plugin.information.create")}

-
-          
-            git init {repository.name}
-            
- echo "# {repository.name}" > README.md -
- git add README.md -
- git commit -m "added readme" -
- git remote add origin {href} -
- git push -u origin master -
-
-
-

{t("scm-git-plugin.information.replace")}

-
-          
-            git remote add origin {href}
-            
- git push -u origin master -
-
-
-
+ + ); + }; + + render() { + const { repository, classes } = this.props; + + const protocols = repository._links["protocol"]; + if (!protocols || protocols.length === 0) { + return null; + } + + if (protocols.length === 1) { + return ; + } + + const { selected } = this.state; + let cloneInformation = null; + if (selected) { + cloneInformation = ; + } + + return ( +
+ + {protocols.map(this.renderProtocolButton)} + + { cloneInformation } +
); } } -export default translate("plugins")(ProtocolInformation); +export default injectSheets(styles)(ProtocolInformation);