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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import React, { useState } from 'react'
import {
  Box,
  Paper,
  Typography,
  List,
  ListItem,
  ListItemIcon,
  ListItemText,
  ListItemButton,
  Switch,
  FormControlLabel,
  TextField,
  Button,
  Select,
  MenuItem,
  FormControl,
  InputLabel,
  Slider,
  Card,
  CardContent,
  Divider,




  Alert,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
} from '@mui/material'
import {
  Person,
  Palette,
  Security,
  Notifications,
  Hub,
  Storage,

  Videocam,


  Info,
  Warning,
  CheckCircle,
  ImportExport,
  Backup,
  Restore,
} from '@mui/icons-material'

interface SettingsData {
  profile: {
    displayName: string
    avatar: string
    status: string
    fourWordAddress: string
  }
  appearance: {
    theme: 'light' | 'dark' | 'auto'
    language: string
    fontSize: number
    compactMode: boolean
    animations: boolean
  }
  privacy: {
    showOnlineStatus: boolean
    allowDirectMessages: boolean
    shareTypingIndicators: boolean
    dataCollection: boolean
    crashReports: boolean
  }
  notifications: {
    desktopNotifications: boolean
    soundNotifications: boolean
    messageNotifications: boolean
    callNotifications: boolean
    emailDigest: boolean
  }
  network: {
    autoConnect: boolean
    preferredRegion: string
    maxPeers: number
    bandwidthLimit: number
    useRelay: boolean
  }
  storage: {
    cacheSize: number
    downloadPath: string
    autoCleanup: boolean
    backupEnabled: boolean
    syncSettings: boolean
  }
  media: {
    microphoneDevice: string
    cameraDevice: string
    speakerDevice: string
    microphoneGain: number
    cameraQuality: string
    echoCancellation: boolean
  }
}

const defaultSettings: SettingsData = {
  profile: {
    displayName: 'John Doe',
    avatar: '/avatar-default.jpg',
    status: 'Available',
    fourWordAddress: 'calm-river-mountain-dawn',
  },
  appearance: {
    theme: 'light',
    language: 'English',
    fontSize: 14,
    compactMode: false,
    animations: true,
  },
  privacy: {
    showOnlineStatus: true,
    allowDirectMessages: true,
    shareTypingIndicators: true,
    dataCollection: false,
    crashReports: true,
  },
  notifications: {
    desktopNotifications: true,
    soundNotifications: true,
    messageNotifications: true,
    callNotifications: true,
    emailDigest: false,
  },
  network: {
    autoConnect: true,
    preferredRegion: 'auto',
    maxPeers: 50,
    bandwidthLimit: 0,
    useRelay: true,
  },
  storage: {
    cacheSize: 500,
    downloadPath: '/Downloads',
    autoCleanup: true,
    backupEnabled: true,
    syncSettings: true,
  },
  media: {
    microphoneDevice: 'default',
    cameraDevice: 'default',
    speakerDevice: 'default',
    microphoneGain: 70,
    cameraQuality: 'HD',
    echoCancellation: true,
  },
}

const settingsCategories = [
  { id: 'profile', label: 'Profile', icon: <Person /> },
  { id: 'appearance', label: 'Appearance', icon: <Palette /> },
  { id: 'privacy', label: 'Privacy & Security', icon: <Security /> },
  { id: 'notifications', label: 'Notifications', icon: <Notifications /> },
  { id: 'network', label: 'Network', icon: <Hub /> },
  { id: 'storage', label: 'Storage', icon: <Storage /> },
  { id: 'media', label: 'Audio & Video', icon: <Videocam /> },
]

export default function SettingsInterface() {
  const [settings, setSettings] = useState<SettingsData>(defaultSettings)
  const [selectedCategory, setSelectedCategory] = useState('profile')
  const [importDialogOpen, setImportDialogOpen] = useState(false)
  const [exportDialogOpen, setExportDialogOpen] = useState(false)
  const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false)

  const updateSettings = <T extends keyof SettingsData>(
    category: T,
    key: keyof SettingsData[T],
    value: any
  ) => {
    setSettings(prev => ({
      ...prev,
      [category]: {
        ...prev[category],
        [key]: value,
      },
    }))
    setHasUnsavedChanges(true)
  }

  const handleSaveSettings = () => {
    console.log('Saving settings:', settings)
    setHasUnsavedChanges(false)
    // In real implementation, save to storage/network
  }

  const handleResetSettings = () => {
    setSettings(defaultSettings)
    setHasUnsavedChanges(true)
  }

  const handleExportSettings = () => {
    const dataStr = JSON.stringify(settings, null, 2)
    const dataBlob = new Blob([dataStr], { type: 'application/json' })
    const url = URL.createObjectURL(dataBlob)
    const link = document.createElement('a')
    link.href = url
    link.download = 'communitas-settings.json'
    link.click()
    setExportDialogOpen(false)
  }

  const handleImportSettings = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0]
    if (file) {
      const reader = new FileReader()
      reader.onload = (e) => {
        try {
          const importedSettings = JSON.parse(e.target?.result as string)
          setSettings(importedSettings)
          setHasUnsavedChanges(true)
          setImportDialogOpen(false)
        } catch (error) {
          console.error('Error importing settings:', error)
        }
      }
      reader.readAsText(file)
    }
  }

  const renderProfileSettings = () => (
    <Card>
      <CardContent>
        <Typography variant="h6" gutterBottom>
          Profile Information
        </Typography>
        
        <Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
          <TextField
            label="Display Name"
            value={settings.profile.displayName}
            onChange={(e) => updateSettings('profile', 'displayName', e.target.value)}
            fullWidth
          />
          
          <TextField
            label="Four-Word Address"
            value={settings.profile.fourWordAddress}
            disabled
            fullWidth
            helperText="This is your unique P2P address"
          />
          
          <FormControl fullWidth>
            <InputLabel>Status</InputLabel>
            <Select
              value={settings.profile.status}
              onChange={(e) => updateSettings('profile', 'status', e.target.value)}
            >
              <MenuItem value="Available">Available</MenuItem>
              <MenuItem value="Busy">Busy</MenuItem>
              <MenuItem value="Away">Away</MenuItem>
              <MenuItem value="Invisible">Invisible</MenuItem>
            </Select>
          </FormControl>
        </Box>
      </CardContent>
    </Card>
  )

  const renderAppearanceSettings = () => (
    <Card>
      <CardContent>
        <Typography variant="h6" gutterBottom>
          Appearance & Display
        </Typography>
        
        <Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
          <FormControl>
            <InputLabel>Theme</InputLabel>
            <Select
              value={settings.appearance.theme}
              onChange={(e) => updateSettings('appearance', 'theme', e.target.value)}
            >
              <MenuItem value="light">Light</MenuItem>
              <MenuItem value="dark">Dark</MenuItem>
              <MenuItem value="auto">Auto</MenuItem>
            </Select>
          </FormControl>
          
          <Box>
            <Typography gutterBottom>Font Size</Typography>
            <Slider
              value={settings.appearance.fontSize}
              onChange={(_e, value) => updateSettings('appearance', 'fontSize', value)}
              min={10}
              max={24}
              step={1}
              valueLabelDisplay="auto"
              marks={[
                { value: 12, label: 'Small' },
                { value: 16, label: 'Medium' },
                { value: 20, label: 'Large' },
              ]}
            />
          </Box>
          
          <FormControlLabel
            control={
              <Switch
                checked={settings.appearance.compactMode}
                onChange={(e) => updateSettings('appearance', 'compactMode', e.target.checked)}
              />
            }
            label="Compact Mode"
          />
          
          <FormControlLabel
            control={
              <Switch
                checked={settings.appearance.animations}
                onChange={(e) => updateSettings('appearance', 'animations', e.target.checked)}
              />
            }
            label="Enable Animations"
          />
        </Box>
      </CardContent>
    </Card>
  )

  const renderPrivacySettings = () => (
    <Card>
      <CardContent>
        <Typography variant="h6" gutterBottom>
          Privacy & Security
        </Typography>
        
        <List>
          <ListItem>
            <ListItemIcon><Person /></ListItemIcon>
            <ListItemText
              primary="Show Online Status"
              secondary="Let others see when you're online"
            />
            <Switch
              checked={settings.privacy.showOnlineStatus}
              onChange={(e) => updateSettings('privacy', 'showOnlineStatus', e.target.checked)}
            />
          </ListItem>
          
          <ListItem>
            <ListItemIcon><Security /></ListItemIcon>
            <ListItemText
              primary="Allow Direct Messages"
              secondary="Receive messages from anyone"
            />
            <Switch
              checked={settings.privacy.allowDirectMessages}
              onChange={(e) => updateSettings('privacy', 'allowDirectMessages', e.target.checked)}
            />
          </ListItem>
          
          <ListItem>
            <ListItemIcon><Info /></ListItemIcon>
            <ListItemText
              primary="Share Typing Indicators"
              secondary="Show when you're typing"
            />
            <Switch
              checked={settings.privacy.shareTypingIndicators}
              onChange={(e) => updateSettings('privacy', 'shareTypingIndicators', e.target.checked)}
            />
          </ListItem>
          
          <Divider />
          
          <ListItem>
            <ListItemIcon><Warning /></ListItemIcon>
            <ListItemText
              primary="Data Collection"
              secondary="Allow anonymous usage analytics"
            />
            <Switch
              checked={settings.privacy.dataCollection}
              onChange={(e) => updateSettings('privacy', 'dataCollection', e.target.checked)}
            />
          </ListItem>
          
          <ListItem>
            <ListItemIcon><CheckCircle /></ListItemIcon>
            <ListItemText
              primary="Crash Reports"
              secondary="Send crash reports to help improve Communitas"
            />
            <Switch
              checked={settings.privacy.crashReports}
              onChange={(e) => updateSettings('privacy', 'crashReports', e.target.checked)}
            />
          </ListItem>
        </List>
      </CardContent>
    </Card>
  )

  const renderNotificationSettings = () => (
    <Card>
      <CardContent>
        <Typography variant="h6" gutterBottom>
          Notifications
        </Typography>
        
        <List>
          <ListItem>
            <ListItemText primary="Desktop Notifications" />
            <Switch
              checked={settings.notifications.desktopNotifications}
              onChange={(e) => updateSettings('notifications', 'desktopNotifications', e.target.checked)}
            />
          </ListItem>
          
          <ListItem>
            <ListItemText primary="Sound Notifications" />
            <Switch
              checked={settings.notifications.soundNotifications}
              onChange={(e) => updateSettings('notifications', 'soundNotifications', e.target.checked)}
            />
          </ListItem>
          
          <ListItem>
            <ListItemText primary="Message Notifications" />
            <Switch
              checked={settings.notifications.messageNotifications}
              onChange={(e) => updateSettings('notifications', 'messageNotifications', e.target.checked)}
            />
          </ListItem>
          
          <ListItem>
            <ListItemText primary="Call Notifications" />
            <Switch
              checked={settings.notifications.callNotifications}
              onChange={(e) => updateSettings('notifications', 'callNotifications', e.target.checked)}
            />
          </ListItem>
        </List>
      </CardContent>
    </Card>
  )

  const renderMediaSettings = () => (
    <Card>
      <CardContent>
        <Typography variant="h6" gutterBottom>
          Audio & Video Settings
        </Typography>
        
        <Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
          <FormControl fullWidth>
            <InputLabel>Microphone</InputLabel>
            <Select
              value={settings.media.microphoneDevice}
              onChange={(e) => updateSettings('media', 'microphoneDevice', e.target.value)}
            >
              <MenuItem value="default">Default Microphone</MenuItem>
              <MenuItem value="built-in">Built-in Microphone</MenuItem>
            </Select>
          </FormControl>
          
          <Box>
            <Typography gutterBottom>Microphone Gain</Typography>
            <Slider
              value={settings.media.microphoneGain}
              onChange={(_e, value) => updateSettings('media', 'microphoneGain', value)}
              min={0}
              max={100}
              valueLabelDisplay="auto"
            />
          </Box>
          
          <FormControl fullWidth>
            <InputLabel>Camera</InputLabel>
            <Select
              value={settings.media.cameraDevice}
              onChange={(e) => updateSettings('media', 'cameraDevice', e.target.value)}
            >
              <MenuItem value="default">Default Camera</MenuItem>
              <MenuItem value="built-in">Built-in Camera</MenuItem>
            </Select>
          </FormControl>
          
          <FormControl fullWidth>
            <InputLabel>Camera Quality</InputLabel>
            <Select
              value={settings.media.cameraQuality}
              onChange={(e) => updateSettings('media', 'cameraQuality', e.target.value)}
            >
              <MenuItem value="HD">HD (720p)</MenuItem>
              <MenuItem value="FHD">Full HD (1080p)</MenuItem>
              <MenuItem value="4K">4K (2160p)</MenuItem>
            </Select>
          </FormControl>
          
          <FormControlLabel
            control={
              <Switch
                checked={settings.media.echoCancellation}
                onChange={(e) => updateSettings('media', 'echoCancellation', e.target.checked)}
              />
            }
            label="Echo Cancellation"
          />
        </Box>
      </CardContent>
    </Card>
  )

  const renderContent = () => {
    switch (selectedCategory) {
      case 'profile': return renderProfileSettings()
      case 'appearance': return renderAppearanceSettings()
      case 'privacy': return renderPrivacySettings()
      case 'notifications': return renderNotificationSettings()
      case 'media': return renderMediaSettings()
      default: return renderProfileSettings()
    }
  }

  return (
    <Box sx={{ display: 'flex', height: '100%' }}>
      {/* Settings categories sidebar */}
      <Paper sx={{ width: 280, mr: 2 }}>
        <Box sx={{ p: 2 }}>
          <Typography variant="h6" gutterBottom>
            Settings
          </Typography>
          
          <List>
            {settingsCategories.map((category) => (
              <ListItemButton
                key={category.id}
                selected={selectedCategory === category.id}
                onClick={() => setSelectedCategory(category.id)}
              >
                <ListItemIcon>{category.icon}</ListItemIcon>
                <ListItemText primary={category.label} />
              </ListItemButton>
            ))}
          </List>
          
          <Divider sx={{ my: 2 }} />
          
          <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
            <Button
              startIcon={<ImportExport />}
              onClick={() => setImportDialogOpen(true)}
              fullWidth
            >
              Import
            </Button>
            <Button
              startIcon={<Backup />}
              onClick={() => setExportDialogOpen(true)}
              fullWidth
            >
              Export
            </Button>
            <Button
              startIcon={<Restore />}
              onClick={handleResetSettings}
              color="warning"
              fullWidth
            >
              Reset
            </Button>
          </Box>
        </Box>
      </Paper>

      {/* Settings content */}
      <Box sx={{ flexGrow: 1 }}>
        <Box sx={{ mb: 2 }}>
          {hasUnsavedChanges && (
            <Alert severity="warning" sx={{ mb: 2 }}>
              You have unsaved changes. Don't forget to save your settings.
            </Alert>
          )}
        </Box>
        
        {renderContent()}
        
        <Box sx={{ mt: 3, display: 'flex', gap: 2 }}>
          <Button
            variant="contained"
            onClick={handleSaveSettings}
            disabled={!hasUnsavedChanges}
          >
            Save Changes
          </Button>
          <Button
            variant="outlined"
            onClick={() => setSettings(defaultSettings)}
          >
            Discard
          </Button>
        </Box>
      </Box>

      {/* Import dialog */}
      <Dialog open={importDialogOpen} onClose={() => setImportDialogOpen(false)}>
        <DialogTitle>Import Settings</DialogTitle>
        <DialogContent>
          <Typography variant="body2" sx={{ mb: 2 }}>
            Select a settings file to import your configuration.
          </Typography>
          <input
            type="file"
            accept=".json"
            onChange={handleImportSettings}
            style={{ width: '100%' }}
          />
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setImportDialogOpen(false)}>Cancel</Button>
        </DialogActions>
      </Dialog>

      {/* Export dialog */}
      <Dialog open={exportDialogOpen} onClose={() => setExportDialogOpen(false)}>
        <DialogTitle>Export Settings</DialogTitle>
        <DialogContent>
          <Typography variant="body2">
            Export your current settings to a file for backup or transfer.
          </Typography>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setExportDialogOpen(false)}>Cancel</Button>
          <Button variant="contained" onClick={handleExportSettings}>
            Export
          </Button>
        </DialogActions>
      </Dialog>
    </Box>
  )
}