Spaces:
Paused

File size: 10,873 Bytes
0e37978
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235f8ef
 
5eb4857
4f6d973
f3a0c24
5eb4857
 
 
 
 
 
 
 
f3a0c24
5eb4857
235f8ef
 
 
5eb4857
235f8ef
 
 
 
 
 
 
5eb4857
235f8ef
 
 
5eb4857
 
 
 
 
 
 
0e37978
5eb4857
 
 
 
 
 
235f8ef
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
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
import React, { useState } from 'react';
import { api } from '@nexus/shared';

const COLORS = {
  brand: "#1E40AF",
  gray50: "#F9FAFB",
  gray100: "#F3F4F6",
  gray200: "#E5E7EB",
  gray400: "#9CA3AF",
  gray600: "#4B5563",
  gray700: "#374151",
  gray800: "#1F2937",
  gray900: "#111827",
  success: "#059669",
  error: "#DC2626",
};

import { useWorkspace, useUsers } from '@nexus/shared';

export const WorkspaceView: React.FC<{ user: any }> = ({ user }) => {
  const { workspace, loading: workspaceLoading } = useWorkspace();
  const { users: members, loading: usersLoading, refresh: refreshUsers, addUser, loaded: usersLoaded } = useUsers();
  
  // New User Form
  const [showAddForm, setShowAddForm] = useState(false);
  const [form, setForm] = useState({ email: '', full_name: '', password: '', role: 'customer_user' });
  const [submitting, setSubmitting] = useState(false);

  const isAdmin = user.role.includes('admin');

  const isInitialLoading = (workspaceLoading && !workspace) || (usersLoading && !usersLoaded);

  const handleCreateUser = async (e: React.FormEvent) => {
    e.preventDefault();
    setSubmitting(true);
    try {
      await addUser(form);
      setForm({ email: '', full_name: '', password: '', role: 'customer_user' });
      // Brief delay before closing for better feedback
      setTimeout(() => {
        setShowAddForm(false);
        setSubmitting(false);
      }, 500); 
    } catch (err) {
      console.error("Failed to create user:", err);
      alert("Error creating user. Check email uniqueness and permissions.");
      setSubmitting(false);
    }
  };

  const removeMember = async (memberId: string) => {
    if (!window.confirm("Are you sure you want to remove this member? This action is definitive.")) return;
    try {
      await api.delete(`/users/${memberId}/`);
      refreshUsers();
    } catch (err) {
      console.error("Failed to remove member:", err);
      alert("Error removing member. Check your administrative permissions.");
    }
  };

  if (isInitialLoading) return <div style={{ padding: 40, color: COLORS.gray400 }}>Loading workspace intelligence...</div>;

  return (
    <div style={{ padding: "40px", maxWidth: 1200, margin: "0 auto" }}>
      {/* Workspace Header */}
      <div style={{ marginBottom: 48 }}>
        <h1 style={{ fontSize: 32, fontWeight: 800, color: COLORS.gray900, marginBottom: 8 }}>{workspace?.name}</h1>
        <p style={{ color: COLORS.gray600, fontSize: 16 }}>Manage your organization's identity, team, and security settings.</p>
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "1fr 340px", gap: 40, alignItems: "start" }}>
        
        {/* Team Section */}
        <div>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 24 }}>
            <h2 style={{ fontSize: 20, fontWeight: 700, color: COLORS.gray900, margin: 0 }}>Workspace Team</h2>
            {isAdmin ? (
               <button 
                onClick={() => setShowAddForm(true)}
                style={{ padding: "10px 20px", background: COLORS.brand, color: "#fff", border: "none", borderRadius: 10, fontSize: 14, fontWeight: 600, cursor: "pointer" }}
              >
                + Add Member
              </button>
            ) : (
                <button 
                disabled
                style={{ padding: "10px 20px", background: COLORS.gray200, color: COLORS.gray400, border: "none", borderRadius: 10, fontSize: 14, fontWeight: 600, cursor: "not-allowed" }}
              >
                Contact Admin to Add Users
              </button>
            )}
          </div>

          <div style={{ background: "#fff", border: `1px solid ${COLORS.gray200}`, borderRadius: 16, overflow: "hidden" }}>
            <table style={{ width: "100%", borderCollapse: "collapse", textAlign: "left" }}>
              <thead>
                <tr style={{ background: COLORS.gray50, borderBottom: `1px solid ${COLORS.gray200}` }}>
                  <th style={{ padding: "16px 24px", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase" }}>Member</th>
                  <th style={{ padding: "16px 24px", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase" }}>Role</th>
                  <th style={{ padding: "16px 24px", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase" }}>Status</th>
                  <th style={{ padding: "16px 24px", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase" }}></th>
                </tr>
              </thead>
              <tbody>
                {members.map(member => (
                  <tr key={member.id} style={{ borderBottom: `1px solid ${COLORS.gray100}` }}>
                    <td style={{ padding: "20px 24px" }}>
                      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
                        <div style={{ width: 36, height: 36, borderRadius: "50%", background: COLORS.gray100, display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 700, fontSize: 14, color: COLORS.brand }}>
                          {member.full_name[0].toUpperCase()}
                        </div>
                        <div>
                          <div style={{ fontSize: 14, fontWeight: 600, color: COLORS.gray900 }}>{member.full_name}</div>
                          <div style={{ fontSize: 12, color: COLORS.gray400 }}>{member.email}</div>
                        </div>
                      </div>
                    </td>
                    <td style={{ padding: "20px 24px" }}>
                       <span style={{ fontSize: 12, fontWeight: 600, color: COLORS.gray600, background: COLORS.gray100, padding: "4px 10px", borderRadius: 6 }}>
                        {member.role.replace('_', ' ').toUpperCase()}
                       </span>
                    </td>
                    <td style={{ padding: "20px 24px" }}>
                       <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
                          <div style={{ width: 6, height: 6, borderRadius: "50%", background: COLORS.success }} />
                          <span style={{ fontSize: 13, color: COLORS.success }}>Active Member</span>
                       </div>
                    </td>
                    <td style={{ padding: "20px 24px", textAlign: "right" }}>
                       {isAdmin && member.id !== user.id && (
                          <button 
                            onClick={() => removeMember(member.id)}
                            style={{ background: "none", border: "none", color: COLORS.error, fontSize: 13, fontWeight: 500, cursor: "pointer" }}
                          >
                            Remove Member
                          </button>
                       )}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>

        {/* Sidebar Creation Form (Fixed right) */}
        {showAddForm && (
          <div style={{ background: "#fff", padding: 24, borderRadius: 20, border: `1px solid ${COLORS.gray200}`, boxShadow: "0 10px 40px rgba(0,0,0,0.05)" }}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 20 }}>
               <h3 style={{ fontSize: 16, fontWeight: 700, margin: 0 }}>Add New Member</h3>
               <button onClick={() => setShowAddForm(false)} style={{ background: "none", border: "none", fontSize: 20, cursor: "pointer", color: COLORS.gray400 }}>×</button>
            </div>
            
            <form onSubmit={handleCreateUser} style={{ display: "flex", flexDirection: "column", gap: 16 }}>
               <div>
                  <label style={{ display: "block", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase", marginBottom: 6 }}>Full Name</label>
                  <input 
                    required 
                    value={form.full_name} 
                    onChange={e => setForm({...form, full_name: e.target.value})} 
                    placeholder="Enter name"
                    style={{ width: "100%", padding: "12px", borderRadius: 10, border: `1px solid ${COLORS.gray200}`, fontSize: 14, outline: "none" }}
                  />
               </div>
               <div>
                  <label style={{ display: "block", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase", marginBottom: 6 }}>Email</label>
                  <input 
                    required 
                    type="email" 
                    value={form.email} 
                    onChange={e => setForm({...form, email: e.target.value})} 
                    placeholder="name@company.com"
                    style={{ width: "100%", padding: "12px", borderRadius: 10, border: `1px solid ${COLORS.gray200}`, fontSize: 14, outline: "none" }}
                  />
               </div>
               <div>
                  <label style={{ display: "block", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase", marginBottom: 6 }}>Password</label>
                  <input 
                    required 
                    type="password" 
                    value={form.password} 
                    onChange={e => setForm({...form, password: e.target.value})} 
                    placeholder="Temporary password"
                    style={{ width: "100%", padding: "12px", borderRadius: 10, border: `1px solid ${COLORS.gray200}`, fontSize: 14, outline: "none" }}
                  />
               </div>
               <div>
                  <label style={{ display: "block", fontSize: 12, fontWeight: 700, color: COLORS.gray400, textTransform: "uppercase", marginBottom: 6 }}>Role</label>
                  <select 
                    value={form.role} 
                    onChange={e => setForm({...form, role: e.target.value})} 
                    style={{ width: "100%", padding: "12px", borderRadius: 10, border: `1px solid ${COLORS.gray200}`, fontSize: 14, outline: "none" }}
                  >
                    <option value="customer_user">Customer User</option>
                    <option value="customer_admin">Customer Admin</option>
                  </select>
               </div>
               <button 
                type="submit" 
                disabled={submitting}
                style={{ width: "100%", padding: "14px", background: COLORS.brand, color: "#fff", border: "none", borderRadius: 10, fontWeight: 700, marginTop: 10, cursor: "pointer" }}
               >
                 {submitting ? "Sending Invite..." : "Create User Access"}
               </button>
            </form>
          </div>
        )}
      </div>
    </div>
  );
};