Spaces:
Paused

File size: 6,998 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import { useUsers, useWorkspaces } from '@nexus/shared';
import styles from './WorkspaceSettings.module.css';

interface WorkspaceSettingsProps {
  workspaceId?: string;
}

export const WorkspaceSettings: React.FC<WorkspaceSettingsProps> = ({ workspaceId }) => {
  const { users, loading: usersLoading, addUser } = useUsers(workspaceId);
  const { workspaces } = useWorkspaces();
  
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [formData, setFormData] = useState({ email: '', fullName: '', role: 'customer_user', password: 'TemporaryPassword123!' });
  const [formError, setFormError] = useState<string | null>(null);
  const [isSubmitting, setIsSubmitting] = useState(false);

  const currentWorkspace = workspaces.find(w => w.id === workspaceId) || workspaces[0];

  const handleAddMember = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsSubmitting(true);
    setFormError(null);
    try {
      await addUser({
        email: formData.email,
        full_name: formData.fullName,
        role: formData.role,
        password: formData.password
      });
      setIsModalOpen(false);
      setFormData({ email: '', fullName: '', role: 'customer_user', password: 'TemporaryPassword123!' });
    } catch (err: any) {
      setFormError(err.message);
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <div className={styles.container}>
      <header className={styles.header}>
        <h1 className={styles.workspaceName}>{currentWorkspace?.name || 'Workspace Settings'}</h1>
        <p className={styles.description}>
          Manage your organization's identity, team, and security settings. 
          Use this panel to control access and oversee your dedicated support environment.
        </p>
      </header>

      <section className={styles.teamSection}>
        <div className={styles.sectionHeader}>
          <h2 className={styles.sectionTitle}>Workspace Team</h2>
          <button className={styles.addBtn} onClick={() => setIsModalOpen(true)}>
            <span>+</span> Add Member
          </button>
        </div>

        <div className={styles.tableCard}>
          <table className={styles.table}>
            <thead>
              <tr>
                <th className={styles.th}>Member</th>
                <th className={styles.th}>Role</th>
                <th className={styles.th}>Status</th>
                <th className={styles.th}></th>
              </tr>
            </thead>
            <tbody>
              {usersLoading && users.length === 0 ? (
                 <tr><td colSpan={4} className={styles.td}>Synchronizing team data...</td></tr>
              ) : users.length === 0 ? (
                 <tr><td colSpan={4} className={styles.td}>No members found in this workspace.</td></tr>
              ) : (
                users.map(user => (
                  <tr key={user.id}>
                    <td className={styles.td}>
                      <div className={styles.memberCell}>
                        <div className={styles.avatar}>{user.full_name?.slice(0,1).toUpperCase() || user.email.slice(0,1).toUpperCase()}</div>
                        <div className={styles.memberInfo}>
                          <span className={styles.memberName}>{user.full_name}</span>
                          <span className={styles.memberEmail}>{user.email}</span>
                        </div>
                      </div>
                    </td>
                    <td className={styles.td}>
                      <span className={`${styles.roleBadge} ${user.role.includes('admin') ? styles.roleAdmin : ''}`}>
                        {user.role.replace('_', ' ')}
                      </span>
                    </td>
                    <td className={styles.td}>
                      <div className={styles.statusCell}>
                        <span className={styles.statusDot} />
                        Active Member
                      </div>
                    </td>
                    <td className={styles.td} style={{ textAlign: 'right' }}>
                      <button className={styles.actionBtn} style={{ background: 'none', border: 'none', cursor: 'pointer', color: '#94a3b8' }}>⋯</button>
                    </td>
                  </tr>
                ))
              )}
            </tbody>
          </table>
        </div>
      </section>

      {/* Add Member Modal */}
      {isModalOpen && (
        <div className={styles.modalOverlay} onClick={() => setIsModalOpen(false)}>
          <div className={styles.modal} onClick={e => e.stopPropagation()}>
            <h2 className={styles.modalTitle}>Add Team Member</h2>
            <p className={styles.modalSub}>Grant workspace access to a new team member.</p>
            
            <form onSubmit={handleAddMember}>
              <div className={styles.formGroup}>
                <label className={styles.label}>Full Name</label>
                <input 
                  className={styles.input} 
                  placeholder="John Doe" 
                  required
                  value={formData.fullName}
                  onChange={e => setFormData({...formData, fullName: e.target.value})}
                />
              </div>
              <div className={styles.formGroup}>
                <label className={styles.label}>Email Address</label>
                <input 
                  className={styles.input} 
                  type="email" 
                  placeholder="john@organization.com" 
                  required
                  value={formData.email}
                  onChange={e => setFormData({...formData, email: e.target.value})}
                />
              </div>
              <div className={styles.formGroup}>
                <label className={styles.label}>Workspace Role</label>
                <select 
                  className={styles.select}
                  value={formData.role}
                  onChange={e => setFormData({...formData, role: e.target.value})}
                >
                  <option value="customer_user">Customer User</option>
                  <option value="customer_admin">Customer Admin</option>
                  <option value="3sc_agent">SC Agent (Staff)</option>
                  <option value="3sc_lead">SC Lead (Staff)</option>
                  <option value="3sc_admin">Co-Admin (Staff)</option>
                </select>
              </div>

              {formError && <div className={styles.error}>{formError}</div>}

              <div className={styles.modalActions}>
                <button type="button" className={styles.cancelBtn} onClick={() => setIsModalOpen(false)}>Cancel</button>
                <button type="submit" className={styles.submitBtn} disabled={isSubmitting}>
                  {isSubmitting ? 'Creating...' : 'Create Member'}
                </button>
              </div>
            </form>
          </div>
        </div>
      )}
    </div>
  );
};