From 9124b694235098c26e91b27a7a8d48d4a51f786e Mon Sep 17 00:00:00 2001 From: shamanec Date: Mon, 10 Jun 2024 18:01:57 +0300 Subject: [PATCH 1/5] initially show version --- .github/workflows/main.yml | 1 + common/models/config.go | 1 + hub/gads-ui/src/Gads.js | 10 ++++++++-- hub/gads-ui/src/components/Login/Login.js | 16 ++++++++++++++-- .../TopNavigationBar/TopNavigationBar.js | 6 +++++- hub/hub.go | 4 +++- main.go | 2 +- 7 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b784cff9..3bc1ab7e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,6 +28,7 @@ jobs: CI: false run: | cd hub/gads-ui && npm install && npm run build --verbose + echo "{\"version\": \"${{ github.ref_name }}\"}" > hub/gads-ui/build/version.json - name: Build binaries run: | diff --git a/common/models/config.go b/common/models/config.go index 1b49d46f..ceaaf162 100644 --- a/common/models/config.go +++ b/common/models/config.go @@ -37,4 +37,5 @@ type HubConfig struct { SeleniumGridInstance string `json:"selenium_grid_instance"` OSTempDir string `json:"-"` UIFilesTempDir string `json:"-"` + CurrentVersion string `json:"-"` } diff --git a/hub/gads-ui/src/Gads.js b/hub/gads-ui/src/Gads.js index e2d2e464..7711afe6 100644 --- a/hub/gads-ui/src/Gads.js +++ b/hub/gads-ui/src/Gads.js @@ -4,16 +4,22 @@ import {Routes, Route, Navigate} from 'react-router-dom' import NavBar from './components/TopNavigationBar/TopNavigationBar' import DeviceControl from './components/DeviceControl/DeviceControl' import Login from './components/Login/Login' -import { useContext } from 'react' +import {useContext, useEffect} from 'react' import { Auth } from './contexts/Auth' import AdminDashboard from './components/Admin/AdminDashboard' import axiosInterceptor from './services/axiosInterceptor' +import versionData from './version.json' function Gads() { const {authToken, logout} = useContext(Auth) // Set the logout function from the Auth context on the axiosInterceptor to automatically logout on each 401 axiosInterceptor(logout) + useEffect(() => { + localStorage.removeItem('gadsVersion') + localStorage.setItem('gadsVersion', versionData.version) + }, []) + if (!authToken) { return } @@ -28,7 +34,7 @@ function Gads() { } /> - ); + ) } export default Gads diff --git a/hub/gads-ui/src/components/Login/Login.js b/hub/gads-ui/src/components/Login/Login.js index 0048bc64..5befa598 100644 --- a/hub/gads-ui/src/components/Login/Login.js +++ b/hub/gads-ui/src/components/Login/Login.js @@ -50,6 +50,8 @@ export default function Login() { }) } + let gadsVersion = localStorage.getItem('gadsVersion') + return (
@@ -76,7 +78,7 @@ export default function Login() { label="Username" required id="outlined-required" - style={{ color: "#9ba984" }} + style={{color: "#9ba984"}} sx={{ input: { background: "#9ba984" @@ -96,7 +98,7 @@ export default function Login() { label="Password" required id="outlined-required" - style={{ color: "#9ba984" }} + style={{color: "#9ba984"}} />
@@ -113,6 +115,16 @@ export default function Login() {
{showAlert && {alertText}} +

{gadsVersion.startsWith('v') ? gadsVersion : "DEV"} +

diff --git a/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js b/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js index 53a25007..ba0a1ca3 100644 --- a/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js +++ b/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js @@ -4,6 +4,7 @@ import { NavLink } from 'react-router-dom' import { Auth } from '../../contexts/Auth' import Button from '@mui/material/Button' import { api } from '../../services/api.js' +import versionData from "../../version.json"; export default function NavBar() { const {userName} = useContext(Auth) @@ -12,12 +13,14 @@ export default function NavBar() { const roleFromStorage = localStorage.getItem('userRole') - if (roleFromStorage == 'admin') { + if (roleFromStorage === 'admin') { if (!showAdmin) { setShowAdmin(true) } } + let appVersion = localStorage.getItem('gadsVersion') + return (
)} +
V: {appVersion}
diff --git a/hub/hub.go b/hub/hub.go index 027bcaa0..c5cfa4a2 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -17,12 +17,13 @@ import ( //go:embed gads-ui/build var uiFiles embed.FS -func StartHub(flags *pflag.FlagSet) { +func StartHub(flags *pflag.FlagSet, appVersion string) { port, _ := flags.GetString("port") if port == "" { log.Fatalf("Please provide a port on which the hub instance should run through the --port flag, e.g. --port=10000") } hostAddress, _ := flags.GetString("host-address") + fmt.Printf("Running hub version `%s`", appVersion) fmt.Printf("UI accessible on http://%s:%v. You can change the address and port with the --host-address and --port flags\n", hostAddress, port) mongoDB, _ := flags.GetString("mongo-db") @@ -56,6 +57,7 @@ func StartHub(flags *pflag.FlagSet) { MongoDB: mongoDB, OSTempDir: osTempDir, UIFilesTempDir: uiFilesTempDir, + CurrentVersion: appVersion, } devices.ConfigData = &config diff --git a/main.go b/main.go index afb3ab32..5b8df4ba 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { Use: "hub", Short: "Run a hub component", Run: func(cmd *cobra.Command, args []string) { - hub.StartHub(cmd.Flags()) + hub.StartHub(cmd.Flags(), AppVersion) }, } hubCmd.Flags().String("ui-files-dir", "", "Directory where the UI static files will be unpacked and served from."+ From ab132724d654b6b2379e65ed9f77c12b4994db9b Mon Sep 17 00:00:00 2001 From: shamanec Date: Mon, 10 Jun 2024 19:10:32 +0300 Subject: [PATCH 2/5] take another approach --- hub/gads-ui/.env.production | 1 + hub/gads-ui/src/.env.development | 1 + hub/gads-ui/src/Gads.js | 38 ++++++++++++++++---------------- 3 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 hub/gads-ui/.env.production create mode 100644 hub/gads-ui/src/.env.development diff --git a/hub/gads-ui/.env.production b/hub/gads-ui/.env.production new file mode 100644 index 00000000..13741616 --- /dev/null +++ b/hub/gads-ui/.env.production @@ -0,0 +1 @@ +REACT_APP_GADS_VERSION= \ No newline at end of file diff --git a/hub/gads-ui/src/.env.development b/hub/gads-ui/src/.env.development new file mode 100644 index 00000000..5335ed4b --- /dev/null +++ b/hub/gads-ui/src/.env.development @@ -0,0 +1 @@ +REACT_APP_GADS_VERSION=development \ No newline at end of file diff --git a/hub/gads-ui/src/Gads.js b/hub/gads-ui/src/Gads.js index 7711afe6..830fec59 100644 --- a/hub/gads-ui/src/Gads.js +++ b/hub/gads-ui/src/Gads.js @@ -8,33 +8,33 @@ import {useContext, useEffect} from 'react' import { Auth } from './contexts/Auth' import AdminDashboard from './components/Admin/AdminDashboard' import axiosInterceptor from './services/axiosInterceptor' -import versionData from './version.json' function Gads() { - const {authToken, logout} = useContext(Auth) - // Set the logout function from the Auth context on the axiosInterceptor to automatically logout on each 401 - axiosInterceptor(logout) + const {authToken, logout} = useContext(Auth) + // Set the logout function from the Auth context on the axiosInterceptor to automatically logout on each 401 + axiosInterceptor(logout) useEffect(() => { localStorage.removeItem('gadsVersion') - localStorage.setItem('gadsVersion', versionData.version) + let version = process.env.REACT_APP_VERSION || 'unknown' + localStorage.setItem('gadsVersion', version) }, []) - if (!authToken) { - return - } + if (!authToken) { + return + } - return ( -
- - - } /> - } /> - } /> - } /> - -
- ) + return ( +
+ + + }/> + }/> + }/> + }/> + +
+ ) } export default Gads From 52d93ff525294f957a4744304b62beb18fb816cb Mon Sep 17 00:00:00 2001 From: shamanec Date: Mon, 10 Jun 2024 19:23:52 +0300 Subject: [PATCH 3/5] use different approach for setting version --- .github/workflows/main.yml | 3 +-- common/models/config.go | 1 - hub/gads-ui/.env | 0 hub/gads-ui/.env.production | 1 - hub/gads-ui/src/components/Login/Login.js | 2 +- .../TopNavigationBar/TopNavigationBar.js | 19 +++++++++++++++---- hub/hub.go | 1 - main.go | 2 +- 8 files changed, 18 insertions(+), 11 deletions(-) delete mode 100644 hub/gads-ui/.env delete mode 100644 hub/gads-ui/.env.production diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3bc1ab7e..3adf8b04 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,8 +27,7 @@ jobs: env: CI: false run: | - cd hub/gads-ui && npm install && npm run build --verbose - echo "{\"version\": \"${{ github.ref_name }}\"}" > hub/gads-ui/build/version.json + cd hub/gads-ui && npm install && REACT_APP_VERSION=${{ github.ref_name }} npm run build --verbose - name: Build binaries run: | diff --git a/common/models/config.go b/common/models/config.go index ceaaf162..1b49d46f 100644 --- a/common/models/config.go +++ b/common/models/config.go @@ -37,5 +37,4 @@ type HubConfig struct { SeleniumGridInstance string `json:"selenium_grid_instance"` OSTempDir string `json:"-"` UIFilesTempDir string `json:"-"` - CurrentVersion string `json:"-"` } diff --git a/hub/gads-ui/.env b/hub/gads-ui/.env deleted file mode 100644 index e69de29b..00000000 diff --git a/hub/gads-ui/.env.production b/hub/gads-ui/.env.production deleted file mode 100644 index 13741616..00000000 --- a/hub/gads-ui/.env.production +++ /dev/null @@ -1 +0,0 @@ -REACT_APP_GADS_VERSION= \ No newline at end of file diff --git a/hub/gads-ui/src/components/Login/Login.js b/hub/gads-ui/src/components/Login/Login.js index 5befa598..7f8888dc 100644 --- a/hub/gads-ui/src/components/Login/Login.js +++ b/hub/gads-ui/src/components/Login/Login.js @@ -50,7 +50,7 @@ export default function Login() { }) } - let gadsVersion = localStorage.getItem('gadsVersion') + let gadsVersion = localStorage.getItem('gadsVersion') || 'unknown' return (
diff --git a/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js b/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js index ba0a1ca3..3bb6560d 100644 --- a/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js +++ b/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js @@ -4,7 +4,6 @@ import { NavLink } from 'react-router-dom' import { Auth } from '../../contexts/Auth' import Button from '@mui/material/Button' import { api } from '../../services/api.js' -import versionData from "../../version.json"; export default function NavBar() { const {userName} = useContext(Auth) @@ -19,7 +18,7 @@ export default function NavBar() { } } - let appVersion = localStorage.getItem('gadsVersion') + let appVersion = localStorage.getItem('gadsVersion') || 'unknown' return (
)} -
V: {appVersion}
+
+
{appVersion.startsWith('v') ? appVersion : "DEV"}
+
-

Welcome, {userName}

+

Welcome, {userName}

diff --git a/hub/hub.go b/hub/hub.go index c5cfa4a2..9393131b 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -57,7 +57,6 @@ func StartHub(flags *pflag.FlagSet, appVersion string) { MongoDB: mongoDB, OSTempDir: osTempDir, UIFilesTempDir: uiFilesTempDir, - CurrentVersion: appVersion, } devices.ConfigData = &config diff --git a/main.go b/main.go index 5b8df4ba..afb3ab32 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { Use: "hub", Short: "Run a hub component", Run: func(cmd *cobra.Command, args []string) { - hub.StartHub(cmd.Flags(), AppVersion) + hub.StartHub(cmd.Flags()) }, } hubCmd.Flags().String("ui-files-dir", "", "Directory where the UI static files will be unpacked and served from."+ From 25b39e33d0828011c2b9007d3b8947fa1a081947 Mon Sep 17 00:00:00 2001 From: shamanec Date: Mon, 10 Jun 2024 19:34:50 +0300 Subject: [PATCH 4/5] add a divider in the top navigation bar --- hub/gads-ui/src/.env.development | 1 - .../TopNavigationBar/TopNavigationBar.js | 47 ++++++++++--------- main.go | 2 +- 3 files changed, 27 insertions(+), 23 deletions(-) delete mode 100644 hub/gads-ui/src/.env.development diff --git a/hub/gads-ui/src/.env.development b/hub/gads-ui/src/.env.development deleted file mode 100644 index 5335ed4b..00000000 --- a/hub/gads-ui/src/.env.development +++ /dev/null @@ -1 +0,0 @@ -REACT_APP_GADS_VERSION=development \ No newline at end of file diff --git a/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js b/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js index 3bb6560d..df8f41a6 100644 --- a/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js +++ b/hub/gads-ui/src/components/TopNavigationBar/TopNavigationBar.js @@ -1,9 +1,10 @@ -import { useContext, useState } from 'react' +import {useContext, useState} from 'react' import './TopNavigationBar.css' -import { NavLink } from 'react-router-dom' -import { Auth } from '../../contexts/Auth' +import {NavLink} from 'react-router-dom' +import {Auth} from '../../contexts/Auth' import Button from '@mui/material/Button' -import { api } from '../../services/api.js' +import {api} from '../../services/api.js' +import Divider from '@mui/material/Divider'; export default function NavBar() { const {userName} = useContext(Auth) @@ -46,19 +47,22 @@ export default function NavBar() { /> )} +
-
{appVersion.startsWith('v') ? appVersion : "DEV"}
-
+ >{appVersion.startsWith('v') ? appVersion : "DEV"}
@@ -73,15 +77,15 @@ export default function NavBar() { ) } -function StyledNavLink({ to, linkText }) { +function StyledNavLink({to, linkText}) { return ( ({ - backgroundColor: isActive ? "#2f3b26" : "", - color: isActive ? "#9ba984" : "#2f3b26", - fontWeight: "bold" - })} - to={to} + style={({isActive}) => ({ + backgroundColor: isActive ? "#2f3b26" : "", + color: isActive ? "#9ba984" : "#2f3b26", + fontWeight: "bold" + })} + to={to} > {linkText} @@ -150,6 +154,7 @@ function LogoutButton() { console.log(e) }) } + return (