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
import React, { useState, useEffect } from 'react'
import {
  Box,
  Grid,
  Card,
  CardContent,
  Typography,
  Button,
  Chip,
  LinearProgress,
  Avatar,
  List,
  ListItem,
  ListItemAvatar,
  ListItemText,
  Divider,
  IconButton
} from '@mui/material'
import {
  Business as BusinessIcon,
  Group as GroupIcon,
  Work as WorkIcon,
  PersonAdd as PersonAddIcon,
  Settings as SettingsIcon,
  Add as AddIcon,
  MoreVert as MoreVertIcon
} from '@mui/icons-material'

import { OrganizationHierarchy } from '../../types/organization'
import { organizationService } from '../../services/organization/OrganizationService'
import CreateOrganizationDialog from './CreateOrganizationDialog'
import CreateGroupDialog from './CreateGroupDialog'
import CreateProjectDialog from './CreateProjectDialog'
import InviteMemberDialog from './InviteMemberDialog'

interface OrganizationDashboardProps {
  currentUserId?: string
}

const OrganizationDashboard: React.FC<OrganizationDashboardProps> = ({
  currentUserId = 'user_owner_123'
}) => {
  const [hierarchies, setHierarchies] = useState<OrganizationHierarchy[]>([])
  const [selectedOrgId, setSelectedOrgId] = useState<string>('')
  const [loading, setLoading] = useState(true)
  
  // Dialog states
  const [createOrgOpen, setCreateOrgOpen] = useState(false)
  const [createGroupOpen, setCreateGroupOpen] = useState(false)
  const [createProjectOpen, setCreateProjectOpen] = useState(false)
  const [inviteMemberOpen, setInviteMemberOpen] = useState(false)
  const [inviteTarget, setInviteTarget] = useState<{ type: 'organization' | 'group' | 'project', id: string } | null>(null)

  useEffect(() => {
    loadUserOrganizations()
  }, [currentUserId])

  const loadUserOrganizations = async () => {
    try {
      setLoading(true)
      const userOrgs = await organizationService.getUserOrganizations(currentUserId)
      
      const hierarchyPromises = userOrgs.map(org => 
        organizationService.getOrganizationHierarchy(org.id)
      )
      
      const hierarchyResults = await Promise.all(hierarchyPromises)
      const validHierarchies = hierarchyResults.filter(h => h !== null) as OrganizationHierarchy[]
      
      setHierarchies(validHierarchies)
      if (validHierarchies.length > 0 && !selectedOrgId) {
        setSelectedOrgId(validHierarchies[0].organization.id)
      }
    } catch (error) {
      console.error('Error loading organizations:', error)
    } finally {
      setLoading(false)
    }
  }

  const selectedHierarchy = hierarchies.find(h => h.organization.id === selectedOrgId)

  const handleCreateOrganization = async (data: any) => {
    try {
      await organizationService.createOrganization(data, currentUserId)
      await loadUserOrganizations()
      setCreateOrgOpen(false)
    } catch (error) {
      console.error('Error creating organization:', error)
    }
  }

  const handleCreateGroup = async (data: any) => {
    try {
      await organizationService.createGroup({
        ...data,
        organization_id: selectedOrgId
      }, currentUserId)
      await loadUserOrganizations()
      setCreateGroupOpen(false)
    } catch (error) {
      console.error('Error creating group:', error)
    }
  }

  const handleCreateProject = async (data: any) => {
    try {
      await organizationService.createProject({
        ...data,
        organization_id: selectedOrgId
      }, currentUserId)
      await loadUserOrganizations()
      setCreateProjectOpen(false)
    } catch (error) {
      console.error('Error creating project:', error)
    }
  }

  const handleInviteMember = async (data: any) => {
    try {
      if (inviteTarget) {
        await organizationService.inviteMember({
          entity_type: inviteTarget.type,
          entity_id: inviteTarget.id,
          invitee_address: data.address,
          role: data.role,
          message: data.message
        }, currentUserId)
        await loadUserOrganizations()
      }
      setInviteMemberOpen(false)
      setInviteTarget(null)
    } catch (error) {
      console.error('Error inviting member:', error)
    }
  }

  const getRoleColor = (role: string) => {
    switch (role) {
      case 'Owner': return 'error'
      case 'Admin': return 'warning'
      case 'Member': return 'primary'
      case 'Viewer': return 'info'
      case 'Guest': return 'default'
      default: return 'default'
    }
  }

  const formatStorageUsage = (used: number, total: number) => {
    const percentage = (used / total) * 100
    return { used: used.toFixed(1), total: total.toFixed(1), percentage }
  }

  if (loading) {
    return (
      <Box sx={{ p: 3 }}>
        <Typography>Loading organizations...</Typography>
        <LinearProgress sx={{ mt: 2 }} />
      </Box>
    )
  }

  if (hierarchies.length === 0) {
    return (
      <Box sx={{ p: 3, textAlign: 'center' }}>
        <BusinessIcon sx={{ fontSize: 64, color: 'text.secondary', mb: 2 }} />
        <Typography variant="h5" gutterBottom>No Organizations</Typography>
        <Typography color="text.secondary" paragraph>
          Create your first organization to start collaborating with your team.
        </Typography>
        <Button
          variant="contained"
          startIcon={<AddIcon />}
          onClick={() => setCreateOrgOpen(true)}
          size="large"
        >
          Create Organization
        </Button>
      </Box>
    )
  }

  return (
    <Box sx={{ p: 3 }}>
      {/* Header */}
      <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 3 }}>
        <Typography variant="h4" component="h1">
          Organization Dashboard
        </Typography>
        <Button
          variant="contained"
          startIcon={<AddIcon />}
          onClick={() => setCreateOrgOpen(true)}
        >
          Create Organization
        </Button>
      </Box>

      {/* Organization Selector */}
      <Grid container spacing={2} sx={{ mb: 3 }}>
        {hierarchies.map((hierarchy) => (
          <Grid item xs={12} sm={6} md={4} key={hierarchy.organization.id}>
            <Card 
              sx={{ 
                cursor: 'pointer',
                border: selectedOrgId === hierarchy.organization.id ? 2 : 1,
                borderColor: selectedOrgId === hierarchy.organization.id ? 'primary.main' : 'divider'
              }}
              onClick={() => setSelectedOrgId(hierarchy.organization.id)}
            >
              <CardContent>
                <Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
                  <BusinessIcon sx={{ mr: 1, color: 'primary.main' }} />
                  <Typography variant="h6" noWrap>
                    {hierarchy.organization.name}
                  </Typography>
                </Box>
                <Typography color="text.secondary" variant="body2" sx={{ mb: 2 }}>
                  {hierarchy.organization.description || 'No description'}
                </Typography>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                  <Typography variant="caption">
                    {hierarchy.total_members} members
                  </Typography>
                  <Typography variant="caption">
                    {formatStorageUsage(
                      hierarchy.total_storage_used_gb, 
                      hierarchy.organization.storage_quota.allocated_gb
                    ).percentage.toFixed(0)}% storage
                  </Typography>
                </Box>
              </CardContent>
            </Card>
          </Grid>
        ))}
      </Grid>

      {selectedHierarchy && (
        <Grid container spacing={3}>
          {/* Left Column - Organization Overview */}
          <Grid item xs={12} lg={4}>
            <Card sx={{ mb: 3 }}>
              <CardContent>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}>
                  <Box>
                    <Typography variant="h6">{selectedHierarchy.organization.name}</Typography>
                    <Typography color="text.secondary">
                      {selectedHierarchy.organization.description}
                    </Typography>
                  </Box>
                  <IconButton size="small">
                    <SettingsIcon />
                  </IconButton>
                </Box>
                
                <Divider sx={{ my: 2 }} />
                
                {/* Storage Usage */}
                <Typography variant="subtitle2" gutterBottom>Storage Usage</Typography>
                <Box sx={{ mb: 2 }}>
                  {(() => {
                    const storage = formatStorageUsage(
                      selectedHierarchy.total_storage_used_gb,
                      selectedHierarchy.organization.storage_quota.allocated_gb
                    )
                    return (
                      <>
                        <LinearProgress 
                          variant="determinate" 
                          value={storage.percentage} 
                          sx={{ mb: 1 }}
                        />
                        <Typography variant="caption">
                          {storage.used} GB of {storage.total} GB used
                        </Typography>
                      </>
                    )
                  })()}
                </Box>

                {/* Quick Stats */}
                <Grid container spacing={2}>
                  <Grid item xs={6}>
                    <Box sx={{ textAlign: 'center' }}>
                      <Typography variant="h4" color="primary">
                        {selectedHierarchy.groups.length}
                      </Typography>
                      <Typography variant="caption">Groups</Typography>
                    </Box>
                  </Grid>
                  <Grid item xs={6}>
                    <Box sx={{ textAlign: 'center' }}>
                      <Typography variant="h4" color="primary">
                        {selectedHierarchy.projects.length}
                      </Typography>
                      <Typography variant="caption">Projects</Typography>
                    </Box>
                  </Grid>
                </Grid>
              </CardContent>
            </Card>

            {/* Members List */}
            <Card>
              <CardContent>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
                  <Typography variant="h6">Members ({selectedHierarchy.organization.members.length})</Typography>
                  <Button
                    size="small"
                    startIcon={<PersonAddIcon />}
                    onClick={() => {
                      setInviteTarget({ type: 'organization', id: selectedHierarchy.organization.id })
                      setInviteMemberOpen(true)
                    }}
                  >
                    Invite
                  </Button>
                </Box>
                <List dense>
                  {selectedHierarchy.organization.members.map((member) => (
                    <ListItem key={member.user_id} sx={{ px: 0 }}>
                      <ListItemAvatar>
                        <Avatar sx={{ width: 32, height: 32 }}>
                          {member.display_name.charAt(0)}
                        </Avatar>
                      </ListItemAvatar>
                      <ListItemText
                        primary={member.display_name}
                        secondary={member.four_word_address}
                      />
                      <Chip 
                        label={member.role} 
                        size="small" 
                        color={getRoleColor(member.role) as any}
                      />
                    </ListItem>
                  ))}
                </List>
              </CardContent>
            </Card>
          </Grid>

          {/* Right Column - Groups and Projects */}
          <Grid item xs={12} lg={8}>
            {/* Groups Section */}
            <Card sx={{ mb: 3 }}>
              <CardContent>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
                  <Typography variant="h6">Groups ({selectedHierarchy.groups.length})</Typography>
                  <Button
                    variant="outlined"
                    size="small"
                    startIcon={<AddIcon />}
                    onClick={() => setCreateGroupOpen(true)}
                  >
                    New Group
                  </Button>
                </Box>
                
                {selectedHierarchy.groups.length === 0 ? (
                  <Typography color="text.secondary" sx={{ textAlign: 'center', py: 4 }}>
                    No groups yet. Create your first group to start team discussions.
                  </Typography>
                ) : (
                  <Grid container spacing={2}>
                    {selectedHierarchy.groups.map((group) => (
                      <Grid item xs={12} sm={6} key={group.id}>
                        <Card variant="outlined" sx={{ height: '100%' }}>
                          <CardContent>
                            <Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
                              <GroupIcon sx={{ mr: 1, color: 'info.main' }} />
                              <Typography variant="subtitle1" noWrap>
                                {group.name}
                              </Typography>
                              <IconButton size="small" sx={{ ml: 'auto' }}>
                                <MoreVertIcon />
                              </IconButton>
                            </Box>
                            <Typography color="text.secondary" variant="body2" sx={{ mb: 2 }}>
                              {group.description || 'No description'}
                            </Typography>
                            <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                              <Typography variant="caption">
                                {group.members.length} members
                              </Typography>
                              <Button
                                size="small"
                                onClick={() => {
                                  setInviteTarget({ type: 'group', id: group.id })
                                  setInviteMemberOpen(true)
                                }}
                              >
                                Invite
                              </Button>
                            </Box>
                          </CardContent>
                        </Card>
                      </Grid>
                    ))}
                  </Grid>
                )}
              </CardContent>
            </Card>

            {/* Projects Section */}
            <Card>
              <CardContent>
                <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
                  <Typography variant="h6">Projects ({selectedHierarchy.projects.length})</Typography>
                  <Button
                    variant="outlined"
                    size="small"
                    startIcon={<AddIcon />}
                    onClick={() => setCreateProjectOpen(true)}
                  >
                    New Project
                  </Button>
                </Box>
                
                {selectedHierarchy.projects.length === 0 ? (
                  <Typography color="text.secondary" sx={{ textAlign: 'center', py: 4 }}>
                    No projects yet. Create your first project to start collaborating.
                  </Typography>
                ) : (
                  <Grid container spacing={2}>
                    {selectedHierarchy.projects.map((project) => (
                      <Grid item xs={12} sm={6} key={project.id}>
                        <Card variant="outlined" sx={{ height: '100%' }}>
                          <CardContent>
                            <Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
                              <WorkIcon sx={{ mr: 1, color: 'success.main' }} />
                              <Typography variant="subtitle1" noWrap>
                                {project.name}
                              </Typography>
                              <Chip 
                                label={project.priority} 
                                size="small" 
                                color={
                                  project.priority === 'critical' ? 'error' :
                                  project.priority === 'high' ? 'warning' :
                                  project.priority === 'medium' ? 'info' : 'default'
                                }
                                sx={{ ml: 1 }}
                              />
                              <IconButton size="small" sx={{ ml: 'auto' }}>
                                <MoreVertIcon />
                              </IconButton>
                            </Box>
                            <Typography color="text.secondary" variant="body2" sx={{ mb: 2 }}>
                              {project.description || 'No description'}
                            </Typography>
                            
                            {/* Project Storage */}
                            <Box sx={{ mb: 2 }}>
                              {(() => {
                                const storage = formatStorageUsage(
                                  project.storage_quota.used_gb,
                                  project.storage_quota.allocated_gb
                                )
                                return (
                                  <>
                                    <LinearProgress 
                                      variant="determinate" 
                                      value={storage.percentage}
                                      sx={{ mb: 0.5 }}
                                    />
                                    <Typography variant="caption">
                                      {storage.used} GB of {storage.total} GB
                                    </Typography>
                                  </>
                                )
                              })()}
                            </Box>

                            <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                              <Typography variant="caption">
                                {project.members.length} members
                              </Typography>
                              <Button
                                size="small"
                                onClick={() => {
                                  setInviteTarget({ type: 'project', id: project.id })
                                  setInviteMemberOpen(true)
                                }}
                              >
                                Invite
                              </Button>
                            </Box>
                          </CardContent>
                        </Card>
                      </Grid>
                    ))}
                  </Grid>
                )}
              </CardContent>
            </Card>
          </Grid>
        </Grid>
      )}

      {/* Dialogs */}
      <CreateOrganizationDialog
        open={createOrgOpen}
        onClose={() => setCreateOrgOpen(false)}
        onSubmit={handleCreateOrganization}
      />

      <CreateGroupDialog
        open={createGroupOpen}
        onClose={() => setCreateGroupOpen(false)}
        onSubmit={handleCreateGroup}
      />

      <CreateProjectDialog
        open={createProjectOpen}
        onClose={() => setCreateProjectOpen(false)}
        onSubmit={handleCreateProject}
      />

      <InviteMemberDialog
        open={inviteMemberOpen}
        onClose={() => {
          setInviteMemberOpen(false)
          setInviteTarget(null)
        }}
        onSubmit={handleInviteMember}
        entityType={inviteTarget?.type}
      />
    </Box>
  )
}

export default OrganizationDashboard