communitas 0.2.6

A diagnostic chat application for the P2P Foundation network
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import React, { useState, useEffect } from 'react'
import {
  Grid,
  Card,
  CardContent,
  Typography,
  Box,
  LinearProgress,
  Chip,
  Table,
  TableBody,
  TableCell,
  TableContainer,
  TableHead,
  TableRow,
  Paper,
  Divider,
  Alert,
} from '@mui/material'
import {
  NetworkCheck,
  Router,
  Speed,
  SignalWifi4Bar,
  SignalWifiOff,
  Hub,
  Security,
} from '@mui/icons-material'

interface PeerConnection {
  id: string
  address: string
  latency: number
  status: 'Connected' | 'Connecting' | 'Disconnected' | 'Failed'
  natType: 'Direct' | 'STUN' | 'TURN' | 'Relay'
  connectionQuality: number // 0-100
  bandwidth: { up: number; down: number }
  lastSeen: Date
}

interface NetworkMetrics {
  bandwidth_up: number
  bandwidth_down: number
  packet_loss: number
  jitter: number
  nat_type: 'Open' | 'Moderate' | 'Strict' | 'Unknown'
  upnp_available: boolean
  ipv6_support: boolean
  total_connections: number
  active_connections: number
}

const NetworkTab: React.FC = () => {
  const [metrics, setMetrics] = useState<NetworkMetrics>({
    bandwidth_up: 850,
    bandwidth_down: 1200,
    packet_loss: 0.1,
    jitter: 5.2,
    nat_type: 'Moderate',
    upnp_available: true,
    ipv6_support: true,
    total_connections: 8,
    active_connections: 6,
  })

  const [peers] = useState<PeerConnection[]>([
    {
      id: '1',
      address: 'warm-ocean-breeze',
      latency: 23,
      status: 'Connected',
      natType: 'Direct',
      connectionQuality: 95,
      bandwidth: { up: 245, down: 780 },
      lastSeen: new Date(),
    },
    {
      id: '2',
      address: 'bright-mountain-peak',
      latency: 45,
      status: 'Connected',
      natType: 'STUN',
      connectionQuality: 87,
      bandwidth: { up: 180, down: 650 },
      lastSeen: new Date(),
    },
    {
      id: '3',
      address: 'gentle-river-flow',
      latency: 67,
      status: 'Connected',
      natType: 'Direct',
      connectionQuality: 78,
      bandwidth: { up: 320, down: 420 },
      lastSeen: new Date(),
    },
    {
      id: '4',
      address: 'dancing-forest-leaf',
      latency: 89,
      status: 'Connecting',
      natType: 'TURN',
      connectionQuality: 45,
      bandwidth: { up: 50, down: 120 },
      lastSeen: new Date(Date.now() - 30000),
    },
    {
      id: '5',
      address: 'silent-snow-crystal',
      latency: 156,
      status: 'Failed',
      natType: 'Relay',
      connectionQuality: 0,
      bandwidth: { up: 0, down: 0 },
      lastSeen: new Date(Date.now() - 300000),
    },
    {
      id: '6',
      address: 'endless-sky-horizon',
      latency: 234,
      status: 'Connected',
      natType: 'TURN',
      connectionQuality: 42,
      bandwidth: { up: 78, down: 145 },
      lastSeen: new Date(),
    },
  ])

  const getStatusColor = (status: string) => {
    switch (status) {
      case 'Connected': return 'success'
      case 'Connecting': return 'warning'
      case 'Disconnected': return 'default'
      case 'Failed': return 'error'
      default: return 'default'
    }
  }

  const getNatTypeColor = (natType: string) => {
    switch (natType) {
      case 'Direct': return 'success'
      case 'STUN': return 'info'
      case 'TURN': return 'warning'
      case 'Relay': return 'error'
      default: return 'default'
    }
  }

  const getNatDifficulty = (natType: string): { text: string; severity: 'success' | 'info' | 'warning' | 'error' } => {
    switch (natType) {
      case 'Open': return { text: 'Excellent connectivity - Direct peer connections possible', severity: 'success' }
      case 'Moderate': return { text: 'Good connectivity - Some NAT traversal required', severity: 'info' }
      case 'Strict': return { text: 'Limited connectivity - Relay servers may be needed', severity: 'warning' }
      case 'Unknown': return { text: 'Connectivity unknown - Testing in progress', severity: 'error' }
      default: return { text: 'Unknown NAT configuration', severity: 'error' }
    }
  }

  const getQualityIcon = (quality: number) => {
    if (quality >= 80) return <SignalWifi4Bar color="success" />
    if (quality >= 60) return <SignalWifi4Bar color="warning" />
    if (quality >= 40) return <SignalWifi4Bar color="error" />
    return <SignalWifiOff color="error" />
  }

  // Simulate real-time updates
  useEffect(() => {
    const interval = setInterval(() => {
      setMetrics(prev => ({
        ...prev,
        bandwidth_up: prev.bandwidth_up + (Math.random() - 0.5) * 100,
        bandwidth_down: prev.bandwidth_down + (Math.random() - 0.5) * 150,
        active_connections: Math.max(4, Math.min(8, prev.active_connections + Math.floor((Math.random() - 0.5) * 2))),
      }))
    }, 5000)

    return () => clearInterval(interval)
  }, [])

  const natDiagnostic = getNatDifficulty(metrics.nat_type)

  return (
    <Box>
      <Typography variant="h4" gutterBottom sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
        <NetworkCheck />
        Network Diagnostics
      </Typography>
      
      {/* NAT & Connectivity Status */}
      <Card sx={{ mb: 3 }}>
        <CardContent>
          <Typography variant="h6" gutterBottom sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
            <Security />
            NAT & Connectivity Analysis
          </Typography>
          <Alert severity={natDiagnostic.severity} sx={{ mb: 2 }}>
            <strong>NAT Type: {metrics.nat_type}</strong> - {natDiagnostic.text}
          </Alert>
          <Grid container spacing={2}>
            <Grid item xs={12} md={4}>
              <Box sx={{ textAlign: 'center', p: 2 }}>
                <Typography variant="body2" color="textSecondary">UPnP Support</Typography>
                <Chip 
                  label={metrics.upnp_available ? 'Available' : 'Not Available'} 
                  color={metrics.upnp_available ? 'success' : 'error'}
                  sx={{ mt: 1 }}
                />
              </Box>
            </Grid>
            <Grid item xs={12} md={4}>
              <Box sx={{ textAlign: 'center', p: 2 }}>
                <Typography variant="body2" color="textSecondary">IPv6 Support</Typography>
                <Chip 
                  label={metrics.ipv6_support ? 'Enabled' : 'Disabled'} 
                  color={metrics.ipv6_support ? 'success' : 'warning'}
                  sx={{ mt: 1 }}
                />
              </Box>
            </Grid>
            <Grid item xs={12} md={4}>
              <Box sx={{ textAlign: 'center', p: 2 }}>
                <Typography variant="body2" color="textSecondary">Active Connections</Typography>
                <Typography variant="h6" sx={{ mt: 1 }}>
                  {metrics.active_connections}/{metrics.total_connections}
                </Typography>
              </Box>
            </Grid>
          </Grid>
        </CardContent>
      </Card>

      <Grid container spacing={3}>
        {/* Network Performance */}
        <Grid item xs={12} md={6}>
          <Card>
            <CardContent>
              <Typography variant="h6" gutterBottom sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
                <Speed />
                Network Performance
              </Typography>
              <Box sx={{ mb: 2 }}>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                  <Typography variant="body2">Upload</Typography>
                  <Typography variant="body2" fontWeight="bold">{Math.round(metrics.bandwidth_up)} kbps</Typography>
                </Box>
                <LinearProgress
                  variant="determinate"
                  value={Math.min(100, (metrics.bandwidth_up / 2000) * 100)}
                  sx={{ mt: 1, height: 8, borderRadius: 4 }}
                />
              </Box>
              <Box sx={{ mb: 2 }}>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                  <Typography variant="body2">Download</Typography>
                  <Typography variant="body2" fontWeight="bold">{Math.round(metrics.bandwidth_down)} kbps</Typography>
                </Box>
                <LinearProgress
                  variant="determinate"
                  value={Math.min(100, (metrics.bandwidth_down / 2000) * 100)}
                  sx={{ mt: 1, height: 8, borderRadius: 4 }}
                />
              </Box>
              <Divider sx={{ my: 2 }} />
              <Grid container spacing={2}>
                <Grid item xs={6}>
                  <Typography variant="body2" color="textSecondary">Packet Loss</Typography>
                  <Typography variant="h6" color={metrics.packet_loss > 1 ? 'error' : 'success'}>
                    {metrics.packet_loss}%
                  </Typography>
                </Grid>
                <Grid item xs={6}>
                  <Typography variant="body2" color="textSecondary">Jitter</Typography>
                  <Typography variant="h6" color={metrics.jitter > 10 ? 'error' : 'success'}>
                    {metrics.jitter} ms
                  </Typography>
                </Grid>
              </Grid>
            </CardContent>
          </Card>
        </Grid>
        
        {/* Peer Connection Graph */}
        <Grid item xs={12} md={6}>
          <Card>
            <CardContent>
              <Typography variant="h6" gutterBottom sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
                <Hub />
                Peer Connection Topology
              </Typography>
              <Box sx={{ textAlign: 'center', p: 2, position: 'relative', minHeight: 200 }}>
                {/* Simple visual representation of peer connections */}
                <Box sx={{ 
                  display: 'flex', 
                  flexDirection: 'column', 
                  alignItems: 'center',
                  gap: 2 
                }}>
                  {/* Central node (you) */}
                  <Box sx={{ 
                    width: 60, 
                    height: 60, 
                    borderRadius: '50%', 
                    bgcolor: 'primary.main', 
                    display: 'flex', 
                    alignItems: 'center', 
                    justifyContent: 'center',
                    color: 'white',
                    fontWeight: 'bold'
                  }}>
                    YOU
                  </Box>
                  
                  {/* Connection lines and peer nodes */}
                  <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 1, justifyContent: 'center' }}>
                    {peers.filter(p => p.status === 'Connected').map((peer) => (
                      <Box key={peer.id} sx={{ 
                        display: 'flex', 
                        flexDirection: 'column', 
                        alignItems: 'center',
                        minWidth: 80
                      }}>
                        <Box sx={{ 
                          width: 40, 
                          height: 40, 
                          borderRadius: '50%', 
                          bgcolor: peer.connectionQuality > 70 ? 'success.main' : peer.connectionQuality > 40 ? 'warning.main' : 'error.main',
                          display: 'flex', 
                          alignItems: 'center', 
                          justifyContent: 'center',
                          color: 'white',
                          fontSize: '0.75rem'
                        }}>
                          {peer.latency}ms
                        </Box>
                        <Typography variant="caption" sx={{ mt: 0.5, textAlign: 'center' }}>
                          {peer.address.split('-')[0]}
                        </Typography>
                      </Box>
                    ))}
                  </Box>
                </Box>
              </Box>
            </CardContent>
          </Card>
        </Grid>
        
        {/* Detailed Peer List */}
        <Grid item xs={12}>
          <Card>
            <CardContent>
              <Typography variant="h6" gutterBottom sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
                <Router />
                Detailed Peer Connections
              </Typography>
              <TableContainer component={Paper} variant="outlined">
                <Table>
                  <TableHead>
                    <TableRow>
                      <TableCell>Quality</TableCell>
                      <TableCell>Address</TableCell>
                      <TableCell>Status</TableCell>
                      <TableCell>NAT Type</TableCell>
                      <TableCell>Latency</TableCell>
                      <TableCell>Bandwidth</TableCell>
                      <TableCell>Last Seen</TableCell>
                    </TableRow>
                  </TableHead>
                  <TableBody>
                    {peers.map((peer) => (
                      <TableRow key={peer.id} sx={{ 
                        backgroundColor: peer.status === 'Failed' ? 'error.light' : 'inherit',
                        opacity: peer.status === 'Failed' ? 0.6 : 1
                      }}>
                        <TableCell>
                          <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
                            {getQualityIcon(peer.connectionQuality)}
                            <Typography variant="body2">{peer.connectionQuality}%</Typography>
                          </Box>
                        </TableCell>
                        <TableCell sx={{ fontFamily: 'monospace' }}>{peer.address}</TableCell>
                        <TableCell>
                          <Chip
                            label={peer.status}
                            color={getStatusColor(peer.status)}
                            size="small"
                          />
                        </TableCell>
                        <TableCell>
                          <Chip
                            label={peer.natType}
                            color={getNatTypeColor(peer.natType)}
                            size="small"
                            variant="outlined"
                          />
                        </TableCell>
                        <TableCell>{peer.latency}ms</TableCell>
                        <TableCell>
                          <Typography variant="body2">
                            ↑{peer.bandwidth.up} ↓{peer.bandwidth.down} kbps
                          </Typography>
                        </TableCell>
                        <TableCell>
                          <Typography variant="body2" color="textSecondary">
                            {peer.lastSeen.toLocaleTimeString()}
                          </Typography>
                        </TableCell>
                      </TableRow>
                    ))}
                  </TableBody>
                </Table>
              </TableContainer>
            </CardContent>
          </Card>
        </Grid>
      </Grid>
    </Box>
  )
}

export default NetworkTab