Skip to content

Commit 9a19cd1

Browse files
committed
Refactor: remove react-trafficlight dependency and replace with custom TrafficLight component
added custom TrafficLight implementation uninstalled react-trafficlight package and removed all of its imports additionally, adjusted useEffect hook inside timeoffset widgets to update only on necessary prop updates Signed-off-by: Andrii Podriez <[email protected]>
1 parent fb4ea52 commit 9a19cd1

File tree

5 files changed

+132
-8
lines changed

5 files changed

+132
-8
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"react-router-dom": "^5.3.1",
5353
"react-svg-pan-zoom": "^3.11.0",
5454
"react-syntax-highlighter": "^15.5.0",
55-
"react-trafficlight": "^5.2.1",
5655
"superagent": "^7.1.2",
5756
"swagger-client": "3.19.10",
5857
"swagger-ui-react": "4.13.0",

src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Dashboard from "./pages/dashboards/dashboard.jsx";
3535
import Account from "./pages/account/account";
3636
import "./styles/app.css";
3737
import "./styles/login.css";
38+
import "./styles/Trafficlight.css";
3839
import branding from "./branding/branding";
3940
import Logout from "./pages/login/logout";
4041
import Infrastructure from "./pages/infrastructure/infrastructure";
@@ -65,7 +66,6 @@ const App = () => {
6566
console.log("APP redirecting to logout/login");
6667
return <Redirect to="/logout" />;
6768
} else {
68-
6969
console.log("APP rendering app");
7070
const pages = branding.values.pages;
7171

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* This file is part of VILLASweb.
3+
*
4+
* VILLASweb is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* VILLASweb is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
import { forwardRef } from "react";
19+
20+
const TrafficLight = ({
21+
isHorizontal,
22+
height,
23+
isRedOn,
24+
isYellowOn,
25+
isGreenOn,
26+
}) => {
27+
const lamps = [
28+
{ color: "red", on: isRedOn },
29+
{ color: "yellow", on: isYellowOn },
30+
{ color: "green", on: isGreenOn },
31+
];
32+
33+
const width = height * 2.5;
34+
const lampGap = height * 0.15;
35+
const lampSize = ((isHorizontal ? width : height) - 4 * lampGap) / 3.5;
36+
const borderRadius = height * 0.2;
37+
38+
return (
39+
<div
40+
className={`traffic-light ${isHorizontal ? "horizontal" : "vertical"}`}
41+
style={{
42+
width: isHorizontal ? width : height,
43+
height: isHorizontal ? height : width,
44+
gap: lampGap,
45+
padding: isHorizontal ? `0 ${lampGap}px` : `${lampGap}px 0`,
46+
borderRadius: borderRadius,
47+
}}
48+
>
49+
{lamps.map((lamp, i) => (
50+
<div
51+
key={i}
52+
className={`lamp ${lamp.color} ${lamp.on ? "on" : "off"}`}
53+
style={{
54+
width: lampSize,
55+
height: lampSize,
56+
flex: `0 0 ${lampSize}px`,
57+
}}
58+
/>
59+
))}
60+
</div>
61+
);
62+
};
63+
64+
export default TrafficLight;

src/pages/dashboards/widget/widgets/time-offset.jsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
1616
******************************************************************************/
1717
import React, { useState, useEffect } from "react";
18-
import TrafficLight from "react-trafficlight";
18+
import TrafficLight from "../widget-time-offset/trafficlight";
1919
import { OverlayTrigger, Tooltip } from "react-bootstrap";
2020

2121
function WidgetTimeOffset(props) {
@@ -79,7 +79,15 @@ function WidgetTimeOffset(props) {
7979
setState((prevState) => ({ ...prevState, ...derivedState }));
8080

8181
// eslint-disable-next-line
82-
}, [props.widget, props.ics, props.websockets, props.data]);
82+
}, [
83+
props.widget.customProperties.icID,
84+
props.widget.customProperties.showOffset,
85+
props.widget.customProperties.threshold_red,
86+
props.widget.customProperties.threshold_yellow,
87+
props.data && state.icID ? props.data[state.icID]?.output?.timestamp : null,
88+
props.websockets?.length,
89+
props.ics?.length,
90+
]);
8391

8492
const { timeOffset, icID, icName, websocketOpen } = state;
8593

@@ -123,20 +131,20 @@ function WidgetTimeOffset(props) {
123131
}
124132
>
125133
<TrafficLight
126-
Horizontal={props.widget.customProperties.horizontal}
127134
width={props.widget.width - 40}
128135
height={props.widget.height - 40}
129-
RedOn={
136+
isHorizontal={props.widget.customProperties.horizontal}
137+
isRedOn={
130138
props.widget.customProperties.threshold_red <= timeOffset ||
131139
!websocketOpen ||
132140
timeOffset < 0
133141
}
134-
YellowOn={
142+
isYellowOn={
135143
props.widget.customProperties.threshold_yellow <= timeOffset &&
136144
timeOffset < props.widget.customProperties.threshold_red &&
137145
websocketOpen
138146
}
139-
GreenOn={
147+
isGreenOn={
140148
timeOffset > 0 &&
141149
timeOffset < props.widget.customProperties.threshold_yellow &&
142150
websocketOpen

src/styles/Trafficlight.css

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* This file is part of VILLASweb.
3+
*
4+
* VILLASweb is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* VILLASweb is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with VILLASweb. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
.traffic-light {
19+
box-sizing: border-box;
20+
background-color: #000;
21+
border-radius: 16px;
22+
display: flex;
23+
align-items: center;
24+
justify-content: center;
25+
}
26+
27+
.traffic-light.horizontal {
28+
flex-direction: row;
29+
}
30+
.traffic-light.vertical {
31+
flex-direction: column;
32+
}
33+
34+
.lamp {
35+
border-radius: 50%;
36+
box-shadow: inset 0 0 3px #000;
37+
}
38+
39+
.lamp.red.on {
40+
background: #cc3232;
41+
box-shadow: 0 0 6px #cc3232, 0 0 12px #cc3232, 0 0 24px #cc3232;
42+
}
43+
.lamp.yellow.on {
44+
background: #e7b416;
45+
box-shadow: 0 0 6px #e7b416, 0 0 12px #e7b416, 0 0 24px #e7b416;
46+
}
47+
.lamp.green.on {
48+
background: #2dc937;
49+
box-shadow: 0 0 6px #2dc937, 0 0 12px #2dc937, 0 0 24px #2dc937;
50+
}
51+
.off {
52+
background: grey;
53+
}

0 commit comments

Comments
 (0)