1
1
import { Card , CardActionArea , Grid , Typography } from "@mui/material" ;
2
2
import { Box , Container } from "@mui/system" ;
3
3
import React , { useState } from "react" ;
4
+ import { useNavigate } from "react-router-dom" ;
4
5
import useWebSocket , { ReadyState } from "react-use-websocket" ;
5
- import { ReactComponent as ServerIcon } from "../assets/svg/server.svg" ;
6
- import LoadingContent from "../common/LoadingContent" ;
7
- import ThemeConfig from "../ThemeConfig" ;
8
- import { ServerResponse } from "./ServerType" ;
6
+ import { ReactComponent as ServerIcon } from "assets/svg/server.svg" ;
7
+ import LoadingContent from "common/LoadingContent" ;
8
+ import ThemeConfig from "ThemeConfig" ;
9
+ import {
10
+ ServerGroupedByHostResponseType ,
11
+ ServerResponseType ,
12
+ } from "./ServerType" ;
13
+
14
+ const wssMetricsBaseURL = `${ process . env . REACT_APP_WS_BASE_URL } /metrics` ;
9
15
10
16
export default function ServerList ( ) {
11
- const [ servers , setServers ] = useState < ServerResponse [ ] > ( [ ] ) ;
12
- const { sendJsonMessage, getWebSocket, readyState } = useWebSocket (
13
- `ws://localhost:3000/metrics` ,
14
- {
15
- onOpen : ( ) => console . log ( "WebSocket connection opened." ) ,
16
- onClose : ( ) => console . log ( "WebSocket connection closed." ) ,
17
- shouldReconnect : ( closeEvent ) => true ,
18
- onMessage : ( event : WebSocketEventMap [ "message" ] ) => {
19
- const newMessage : ServerResponse = JSON . parse ( event . data ) ;
20
- setServers ( ( prev : ServerResponse [ ] ) => {
21
- if ( ! newMessage . Error ) {
22
- return prev . concat ( newMessage ) ;
23
- }
24
- } ) ;
25
- } ,
26
- }
17
+ const navigate = useNavigate ( ) ;
18
+
19
+ const [ servers , setServers ] = useState < ServerResponseType [ ] > ( [ ] ) ;
20
+ const serversGroupedByHost : ServerGroupedByHostResponseType = servers . reduce (
21
+ ( group : any , server ) => {
22
+ const { Message } = server ;
23
+ const { Host } = Message ;
24
+ group [ Host ] = group [ Host ] ?? [ ] ;
25
+ group [ Host ] . push ( server ) ;
26
+ return group ;
27
+ } ,
28
+ { }
27
29
) ;
28
- getWebSocket ( ) ;
30
+
31
+ const { readyState } = useWebSocket ( wssMetricsBaseURL , {
32
+ onOpen : ( ) => console . log ( "WebSocket connection opened." ) ,
33
+ onClose : ( ) => console . log ( "WebSocket connection closed." ) ,
34
+ shouldReconnect : ( closeEvent ) => true ,
35
+ onMessage : ( event : WebSocketEventMap [ "message" ] ) => {
36
+ const newMessage : ServerResponseType = JSON . parse ( event . data ) ;
37
+ setServers ( ( prev ) => prev . concat ( newMessage ) ) ;
38
+ } ,
39
+ } ) ;
29
40
30
41
const connectionStatus : string = {
31
42
[ ReadyState . CONNECTING ] : "Connecting" ,
@@ -35,42 +46,49 @@ export default function ServerList() {
35
46
[ ReadyState . UNINSTANTIATED ] : "Uninstantiated" ,
36
47
} [ readyState ] ;
37
48
38
- console . log ( servers ) ;
39
- // console.log("getWebsocket", getWebSocket()?.OPEN);
40
49
return (
41
50
< Container >
42
51
< LoadingContent
43
52
loading = { connectionStatus === "Connecting" }
44
53
error = { connectionStatus === "Closed" }
45
54
>
46
55
< Grid container spacing = { 2 } my = { 10 } >
47
- { servers . map ( ( server : any , index : number ) => (
48
- < Grid item xs = { 6 } md = { 4 } >
49
- < Card key = { index } >
50
- < CardActionArea >
51
- < Box
52
- display = { "flex" }
53
- justifyContent = "center"
54
- alignItems = "center"
55
- flexDirection = "column"
56
- >
57
- < ServerIcon width = { "100px" } />
58
- < Typography
59
- textTransform = { "capitalize" }
60
- mb = { 2 }
61
- noWrap
62
- fontWeight = { 600 }
56
+ { Object . keys ( serversGroupedByHost ) ?. map (
57
+ ( serverHost : string , index : number ) => (
58
+ < Grid item xs = { 6 } md = { 4 } >
59
+ < Card key = { index } >
60
+ < CardActionArea onClick = { ( ) => navigate ( `/${ serverHost } ` ) } >
61
+ < Box
62
+ display = { "flex" }
63
+ justifyContent = "center"
64
+ alignItems = "center"
65
+ flexDirection = "column"
63
66
>
64
- localhost -{ " " }
65
- < span style = { { color : ThemeConfig . palette . success . dark } } >
66
- linux
67
- </ span >
68
- </ Typography >
69
- </ Box >
70
- </ CardActionArea >
71
- </ Card >
72
- </ Grid >
73
- ) ) }
67
+ < ServerIcon width = { "100px" } />
68
+ < Typography
69
+ textTransform = { "capitalize" }
70
+ mb = { 2 }
71
+ noWrap
72
+ fontWeight = { 600 }
73
+ >
74
+ < >
75
+ { serverHost } -{ " " }
76
+ < span
77
+ style = { { color : ThemeConfig . palette . success . dark } }
78
+ >
79
+ {
80
+ serversGroupedByHost [ serverHost ] ?. [ 0 ] ?. Message
81
+ ?. Platform
82
+ }
83
+ </ span >
84
+ </ >
85
+ </ Typography >
86
+ </ Box >
87
+ </ CardActionArea >
88
+ </ Card >
89
+ </ Grid >
90
+ )
91
+ ) }
74
92
</ Grid >
75
93
</ LoadingContent >
76
94
</ Container >
0 commit comments