import React, { useState } from 'react'; import { api, useWorkspace, useUsers } from '@nexus/shared'; import { UserPlus, Shield, ShieldCheck, X, Mail, Lock, Users, RefreshCcw, ChevronDown } from 'lucide-react'; const COLORS = { brand: "var(--primary)", border: "var(--border-subtle)", bg: "var(--bg-card)", text: "var(--text-main)", muted: "var(--text-muted)", success: "var(--success)", error: "var(--error)", }; const TEAM_OPTIONS = ["ENGINEERING", "FINANCE", "SUCCESS", "ACCOUNT", "SECURITY", "COMPLIANCE", "GENERAL", "OTHER"]; export const WorkspaceView: React.FC<{ user: any }> = ({ user }) => { const { workspace, loading: workspaceLoading, refresh: refreshWorkspace } = useWorkspace(); const { users: members, loading: usersLoading, refresh: refreshUsers, addUser, loaded: usersLoaded } = useUsers(); // Branding State const [brandingLogo, setBrandingLogo] = useState(workspace?.branding?.logo_url || ''); const [brandingColor, setBrandingColor] = useState(workspace?.branding?.colors?.primary || '#7c3aed'); const [brandingSubmitting, setBrandingSubmitting] = useState(false); // Synchronize state when workspace loads React.useEffect(() => { if (workspace?.branding) { setBrandingLogo(workspace.branding.logo_url || ''); setBrandingColor(workspace.branding.colors?.primary || '#7c3aed'); } }, [workspace]); // Provisioning State const [modalType, setModalType] = useState<'create' | 'edit' | null>(null); const [editingUser, setEditingUser] = useState(null); const [form, setForm] = useState({ email: '', full_name: '', password: '', role: 'customer_user', team: 'GENERAL' }); const [submitting, setSubmitting] = useState(false); const isAdmin = user.role.includes('admin'); const isInitialLoading = (workspaceLoading && !workspace) || (usersLoading && !usersLoaded); const handleUpdateBranding = async () => { setBrandingSubmitting(true); try { await api.patch('/workspace/', { workspace_id: workspace?.id, branding: { ...workspace?.branding, logo_url: brandingLogo, colors: { ...workspace?.branding?.colors, primary: brandingColor } } }); sessionStorage.removeItem('nexus_active_ws_v1'); await refreshWorkspace(workspace?.id); } catch (err) { console.error("Branding update failed:", err); } finally { setBrandingSubmitting(false); } }; const handleProvision = async (e: React.FormEvent) => { e.preventDefault(); setSubmitting(true); try { if (modalType === 'edit' && editingUser) { await api.patch(`/users/${editingUser.id}/`, { full_name: form.full_name, role: form.role, team: form.team }); } else { await addUser(form); } setForm({ email: '', full_name: '', password: '', role: 'customer_user', team: 'GENERAL' }); setModalType(null); setEditingUser(null); refreshUsers(); } catch (err) { console.error("Provisioning failed:", err); } finally { setSubmitting(false); } }; const openEdit = (m: any) => { setEditingUser(m); setForm({ email: m.email, full_name: m.full_name, password: '', // Hidden in edit role: m.role, team: m.team || 'GENERAL' }); setModalType('edit'); }; const removeMember = async (memberId: string) => { if (!window.confirm("Are you sure you want to revoke this member's access? They will be marked as deactivated in this workspace.")) return; try { await api.delete(`/users/${memberId}/`); refreshUsers(); } catch (err) { console.error("Failed to remove member:", err); } }; if (isInitialLoading) return (
INITIALIZING WORKSPACE...
); return (
{/* Main Header */}
WORKSPACE OVERVIEW

{workspace?.name?.toUpperCase()}

Manage your organization's team members, roles, and access permissions from this central dashboard.

{/* Identity & Branding Section (Admin Only) */} {isAdmin && (

Identity & Branding

setBrandingLogo(e.target.value)} className="input-focus" placeholder="https://example.com/logo.png" style={{ flex: 1, padding: "18px 24px", borderRadius: 16, border: `1.5px solid var(--border-subtle)`, fontSize: 16, outline: "none", background: "var(--bg-dark)", color: COLORS.text, boxSizing: "border-box", fontWeight: 600 }} />
{brandingLogo ? ( Logo ) : ( )}
{ if (e.target.value && e.target.value !== workspace?.name) { try { await api.patch('/workspace/', { name: e.target.value }); sessionStorage.removeItem('nexus_active_ws_v1'); refreshWorkspace(workspace?.id); } catch (err) { console.error(err); } } }} className="input-focus" placeholder="e.g. ACME CORP" style={{ width: "100%", padding: "18px 24px", borderRadius: 16, border: `1.5px solid var(--border-subtle)`, fontSize: 16, outline: "none", background: "var(--bg-dark)", color: COLORS.text, boxSizing: "border-box", fontWeight: 700, fontFamily: 'var(--font-heading)' }} />
{ setBrandingColor(e.target.value); document.documentElement.style.setProperty('--primary', e.target.value); document.documentElement.style.setProperty('--banner-gradient', `linear-gradient(135deg, ${e.target.value} 0%, ${e.target.value}dd 100%)`); }} style={{ width: 42, height: 42, border: 'none', borderRadius: 10, cursor: 'pointer', background: 'none' }} /> { setBrandingColor(e.target.value); if (e.target.value.startsWith('#') && e.target.value.length === 7) { document.documentElement.style.setProperty('--primary', e.target.value); } }} style={{ background: 'none', border: 'none', color: COLORS.text, fontSize: 16, fontWeight: 700, outline: 'none', width: 100, textTransform: 'uppercase', fontFamily: 'var(--font-mono)' }} />
Identity changes sync across all member portals and analytical reports in real-time.
)}
{/* Member Directory Section */}

Workspace Members

{isAdmin ? ( ) : (
ADMIN RESTRICTION ACTIVE
)}
{members.map(member => ( ))}
MEMBER INFO ACCESS ROLE STATUS ACTIONS
{member.full_name[0].toUpperCase()}
{member.full_name}
{member.email}
{member.role.replace('_', ' ').toUpperCase()}
{member.is_active ? "ACTIVE" : "DEACTIVATED"}
{isAdmin && ( )} {isAdmin && member.id !== user.id && member.is_active && ( )}
{modalType && (
MEMBER REGISTRATION

{modalType === 'edit' ? 'Update Member Profile' : 'Invite New Member'}

setForm({...form, full_name: e.target.value})} placeholder="e.g. John Doe" className="input-focus" style={{ width: "100%", padding: "16px 20px", borderRadius: 14, border: `1.5px solid var(--border-subtle)`, fontSize: 15, outline: "none", background: "var(--bg-dark)", color: COLORS.text, boxSizing: "border-box", fontWeight: 600 }} />
setForm({...form, email: e.target.value})} placeholder="user@example.com" className="input-focus" style={{ width: "100%", padding: "16px 20px 16px 48px", borderRadius: 14, border: `1.5px solid var(--border-subtle)`, fontSize: 15, outline: "none", background: "var(--bg-dark)", color: modalType === 'edit' ? COLORS.muted : COLORS.text, boxSizing: "border-box", fontWeight: 600 }} />
{modalType === 'create' && (
setForm({...form, password: e.target.value})} placeholder="••••••••••••" className="input-focus" style={{ width: "100%", padding: "16px 20px 16px 48px", borderRadius: 14, border: `1.5px solid var(--border-subtle)`, fontSize: 15, outline: "none", background: "var(--bg-dark)", color: COLORS.text, boxSizing: "border-box", fontWeight: 600 }} />
)}
)}
); };