Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { useState, useRef, useEffect } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'
import { ChevronDown, Building2, Plus } from 'lucide-react'
import { useAuth } from '../contexts/AuthContext'
import { useOrganizationStore } from '../store/organizationStore'
export default function OrgSwitcher() {
const [isOpen, setIsOpen] = useState(false)
const [showCreateModal, setShowCreateModal] = useState(false)
const [newOrgName, setNewOrgName] = useState('')
const [isCreating, setIsCreating] = useState(false)
const dropdownRef = useRef(null)
const modalRef = useRef(null)
const navigate = useNavigate()
const location = useLocation()
const { user } = useAuth()
const {
organizations,
currentOrganization,
isLoading,
switchOrganization,
createOrganization
} = useOrganizationStore()
const canActuallyCreate = user && ['admin', 'tester'].includes(user.role)
const noOrgs = !isLoading && organizations.length === 0
useEffect(() => {
function handleClickOutside(event) {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
setIsOpen(false)
}
if (modalRef.current && !modalRef.current.contains(event.target)) {
setShowCreateModal(false)
setNewOrgName('')
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [])
const handleSwitchOrg = (org) => {
const pathParts = location.pathname.split('/').filter(Boolean)
const urlOrgId = pathParts[0]
const isOrgId = /^[0-9a-f]{24}$/.test(urlOrgId)
const hasOrgInUrl = urlOrgId && isOrgId
switchOrganization(org.id)
if (hasOrgInUrl) {
pathParts[0] = org.id
navigate('/' + pathParts.join('/'), { replace: true })
} else {
navigate(`/${org.id}`, { replace: true })
}
setIsOpen(false)
}
const handleCreateOrg = async () => {
if (!newOrgName.trim()) return
setIsCreating(true)
const newOrg = await createOrganization(newOrgName.trim())
if (newOrg?.id) {
navigate(`/${newOrg.id}`, { replace: true })
}
setIsCreating(false)
setShowCreateModal(false)
setNewOrgName('')
setIsOpen(false)
}
const openCreateModal = () => setShowCreateModal(true)
const closeCreateModal = () => {
setShowCreateModal(false)
setNewOrgName('')
}
return (
<>
<div className="relative" ref={dropdownRef}>
{noOrgs ? (
<button
onClick={openCreateModal}
className="flex items-center w-full px-4 py-2 text-sm text-white bg-indigo-600 hover:bg-indigo-700 rounded-lg transition-colors"
>
<Plus className="w-4 h-4 mr-2" />
<span className="flex-1">Créer une organisation</span>
</button>
) : currentOrganization ? (
<>
<button
onClick={() => setIsOpen(!isOpen)}
className="flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 rounded-lg transition-colors"
>
<Building2 className="w-4 h-4 mr-2 text-gray-400" />
<span className="flex-1 text-left truncate">{currentOrganization.name}</span>
<ChevronDown className={`w-4 h-4 text-gray-400 transition-transform ${isOpen ? 'rotate-180' : ''}`} />
</button>
{isOpen && (
<div className="absolute bottom-full left-0 mb-1 w-64 bg-white rounded-lg shadow-lg border border-gray-200 overflow-hidden">
<div className="py-1">
{organizations.map((org) => (
<button
key={org.id}
onClick={() => handleSwitchOrg(org)}
className={`flex items-center w-full px-4 py-2 text-sm text-left hover:bg-gray-50 transition-colors ${
org.id === currentOrganization.id ? 'bg-indigo-50 text-indigo-600' : 'text-gray-700'
}`}
>
<Building2 className="w-4 h-4 mr-2 text-gray-400" />
<span className="flex-1 truncate">{org.name}</span>
<span className="text-xs text-gray-400 capitalize">{org.role}</span>
{org.id === currentOrganization.id && (
<span className="ml-2 text-indigo-600">✓</span>
)}
</button>
))}
</div>
<div className="border-t border-gray-100 py-1">
<button
onClick={openCreateModal}
className="flex items-center w-full px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors"
>
<Plus className="w-4 h-4 mr-2 text-gray-400" />
Nouvelle organisation
</button>
</div>
</div>
)}
</>
) : null}
</div>
{showCreateModal && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50" onClick={(e) => e.target === e.currentTarget && closeCreateModal()}>
<div ref={modalRef} className="bg-white text-gray-900 rounded-lg shadow-xl w-80 p-6">
{canActuallyCreate ? (
<>
<h3 className="text-lg font-semibold text-gray-900 mb-4">Nouvelle organisation</h3>
<input
type="text"
value={newOrgName}
onChange={(e) => setNewOrgName(e.target.value)}
placeholder="Nom de l'organisation"
className="w-full px-3 py-2 border border-gray-300 rounded-lg mb-4 focus:outline-none focus:ring-2 focus:ring-indigo-500"
autoFocus
onKeyDown={(e) => e.key === 'Enter' && handleCreateOrg()}
/>
<div className="flex justify-end gap-3">
<button
onClick={closeCreateModal}
className="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 transition-colors"
disabled={isCreating}
>
Annuler
</button>
<button
onClick={handleCreateOrg}
disabled={!newOrgName.trim() || isCreating}
className="px-4 py-2 text-sm bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors disabled:opacity-50"
>
{isCreating ? 'Création...' : 'Créer'}
</button>
</div>
</>
) : (
<>
<h3 className="text-lg font-semibold text-gray-900 mb-2">Accès restreint</h3>
<p className="text-sm text-gray-600 mb-4">
La création d'organisation est réservée aux testers durant la bêta fermée.
Vous pouvez contacter Data Players pour devenir tester, ou demander à un tester de vous inviter sur son organisation.
</p>
<button
onClick={closeCreateModal}
className="w-full px-4 py-2 text-sm bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition-colors"
>
Compris
</button>
</>
)}
</div>
</div>
)}
</>
)
}
|