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 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 | 2x 2x 1x 2x 2x 2x 2x 2x 5x 5x 5x 5x 2x 2x 1x 5x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 5x 2x 1x 19x 19x 19x 19x 19x 19x 8x 8x 7x 1x 19x 6x 19x 2x 19x 6x 13x 36x 2x 36x 36x 24x 2x 1x 2x | import { useState, useEffect } from 'react'
import { Loader, Building2, Settings, Users, X } from 'lucide-react'
import api from '../../services/api'
import QuotaBar from '../../components/QuotaBar'
import { formatUsageValue } from '../../utils/usage'
function MembersModal({ org, onClose }) {
useEffect(() => {
const handleKeyDown = (event) => {
Eif (event.key === 'Escape') onClose()
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [onClose])
const members = org.members || []
return (
<div
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
onClick={onClose}
role="presentation"
>
<div
className="bg-white rounded-xl w-full max-w-xl max-h-[80vh] flex flex-col"
onClick={(event) => event.stopPropagation()}
role="dialog"
aria-modal="true"
aria-labelledby={`members-title-${org._id}`}
>
<div className="flex items-start justify-between gap-3 p-5 border-b border-gray-200">
<div>
<h3 id={`members-title-${org._id}`} className="text-lg font-semibold text-gray-900">
Utilisateurs — {org.name}
</h3>
<p className="text-sm text-gray-500 mt-0.5">
{members.length} {members.length > 1 ? 'utilisateurs rattachés' : 'utilisateur rattaché'}
</p>
</div>
<button
type="button"
onClick={onClose}
className="p-1.5 text-gray-400 hover:text-gray-700 hover:bg-gray-100 rounded-lg transition-colors"
aria-label="Fermer"
>
<X className="w-5 h-5" />
</button>
</div>
{members.length === 0 ? (
<p className="text-sm text-gray-500 text-center py-8">Aucun utilisateur rattaché</p>
) : (
<ul className="divide-y divide-gray-100 overflow-y-auto">
{members.map((member) => (
<li key={String(member.userId)} className="flex items-center justify-between gap-3 px-5 py-3">
<div className="min-w-0">
<p className="text-sm font-medium text-gray-900 truncate">{member.name}</p>
<p className="text-xs text-gray-500 truncate">{member.email}</p>
</div>
<div className="flex items-center gap-3 shrink-0">
<span className={`inline-flex px-2 py-0.5 rounded-full text-xs font-medium ${
member.role === 'owner' ? 'bg-indigo-100 text-indigo-800' : 'bg-gray-100 text-gray-700'
}`}>
{member.role === 'owner' ? 'Propriétaire' : 'Membre'}
</span>
<span className="text-xs text-gray-500 w-24 text-right">
{member.joinedAt ? new Date(member.joinedAt).toLocaleDateString() : '—'}
</span>
</div>
</li>
))}
</ul>
)}
</div>
</div>
)
}
function PlanModal({ org, onClose, onPlanChange }) {
const [loading, setLoading] = useState(false)
const isCustom = org.plan?.code === 'specific_limit'
const [limits, setLimits] = useState({
maxKbItems: '',
maxLlmCost: '',
maxOcrCost: '',
})
useEffect(() => {
if (isCustom) {
const div = (v) => v != null ? Math.round(v / 10000) : ''
setLimits({
maxKbItems: org.plan?.customLimits?.maxKbItems ?? '',
maxLlmCost: div(org.plan?.customLimits?.maxLlmCost),
maxOcrCost: div(org.plan?.customLimits?.maxOcrCost),
})
}
}, [org])
const handleSetCustom = async () => {
setLoading(true)
try {
await api.put(`/admin/organizations/${org._id}/plan`, {
planCode: 'specific_limit',
customLimits: {
maxKbItems: limits.maxKbItems ? Number(limits.maxKbItems) : null,
maxLlmCost: limits.maxLlmCost ? Math.round(Number(limits.maxLlmCost) * 10000) : null,
maxOcrCost: limits.maxOcrCost ? Math.round(Number(limits.maxOcrCost) * 10000) : null,
},
})
onPlanChange(org._id, 'specific_limit')
onClose()
} catch (err) {
console.error(err)
} finally {
setLoading(false)
}
}
const handleSetPlan = async (planCode) => {
setLoading(true)
try {
await api.put(`/admin/organizations/${org._id}/plan`, { planCode })
onPlanChange(org._id, planCode)
onClose()
} catch (err) {
console.error(err)
} finally {
setLoading(false)
}
}
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50" onClick={onClose}>
<div className="bg-white rounded-xl p-6 w-full max-w-lg mx-4" onClick={e => e.stopPropagation()}>
<h3 className="text-lg font-semibold text-gray-900 mb-1">
Changer le plan — {org.name}
</h3>
<p className="text-sm text-gray-500 mb-6">
Plan actuel : <span className="font-medium text-gray-700">{org.plan?.name || 'Gratuit'}</span>
</p>
{isCustom ? (
<div className="space-y-4">
<p className="text-sm font-medium text-gray-700">Redéfinir les limites :</p>
<div className="space-y-3">
<div>
<label className="block text-sm text-gray-600 mb-1">Éléments KB</label>
<input
type="number"
value={limits.maxKbItems}
onChange={e => setLimits({ ...limits, maxKbItems: e.target.value })}
placeholder="Illimité"
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm"
/>
</div>
<div>
<label className="block text-sm text-gray-600 mb-1">Coût LLM (centimes / mois)</label>
<input
type="number"
value={limits.maxLlmCost}
onChange={e => setLimits({ ...limits, maxLlmCost: e.target.value })}
placeholder="Illimité"
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm"
/>
</div>
<div>
<label className="block text-sm text-gray-600 mb-1">Coût OCR (centimes / mois)</label>
<input
type="number"
value={limits.maxOcrCost}
onChange={e => setLimits({ ...limits, maxOcrCost: e.target.value })}
placeholder="Illimité"
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm"
/>
</div>
</div>
<div className="flex gap-3 pt-2">
<button
onClick={handleSetCustom}
disabled={loading}
className="flex-1 px-4 py-2.5 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium disabled:opacity-50"
>
{loading ? 'Enregistrement...' : 'Enregistrer les limites'}
</button>
<button
onClick={() => handleSetPlan('free')}
disabled={loading}
className="px-4 py-2.5 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium disabled:opacity-50"
>
Repasser en gratuit
</button>
<button onClick={onClose} className="px-4 py-2.5 text-gray-500 hover:text-gray-700 text-sm">
Annuler
</button>
</div>
</div>
) : (
<div className="space-y-4">
<p className="text-sm text-gray-600">Définir des limites personnalisées :</p>
<div className="space-y-3">
<div>
<label className="block text-sm text-gray-600 mb-1">Éléments KB</label>
<input
type="number"
value={limits.maxKbItems}
onChange={e => setLimits({ ...limits, maxKbItems: e.target.value })}
placeholder="Illimité"
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm"
/>
</div>
<div>
<label className="block text-sm text-gray-600 mb-1">Coût LLM (centimes / mois)</label>
<input
type="number"
value={limits.maxLlmCost}
onChange={e => setLimits({ ...limits, maxLlmCost: e.target.value })}
placeholder="Illimité"
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm"
/>
</div>
<div>
<label className="block text-sm text-gray-600 mb-1">Coût OCR (centimes / mois)</label>
<input
type="number"
value={limits.maxOcrCost}
onChange={e => setLimits({ ...limits, maxOcrCost: e.target.value })}
placeholder="Illimité"
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm"
/>
</div>
</div>
<div className="flex gap-3 pt-2">
<button
onClick={handleSetCustom}
disabled={loading}
className="flex-1 px-4 py-2.5 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium disabled:opacity-50"
>
{loading ? 'Enregistrement...' : 'Passer en Sur mesure'}
</button>
<button onClick={onClose} className="px-4 py-2.5 text-gray-500 hover:text-gray-700 text-sm">
Annuler
</button>
</div>
</div>
)}
</div>
</div>
)
}
function AdminOrganizations() {
const [orgs, setOrgs] = useState([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const [selectedOrg, setSelectedOrg] = useState(null)
const [membersOrg, setMembersOrg] = useState(null)
const fetchOrgs = async () => {
try {
const res = await api.get('/admin/organizations')
setOrgs(res.data.data || [])
} catch (err) {
setError(err.response?.data?.message || err.message)
}
}
useEffect(() => {
fetchOrgs().finally(() => setLoading(false))
}, [])
const handlePlanChange = () => {
fetchOrgs()
}
if (loading) {
return (
<div className="flex items-center justify-center min-h-[40vh]">
<Loader className="w-8 h-8 animate-spin text-indigo-600" />
</div>
)
}
return (
<div>
<h1 className="text-2xl font-bold text-gray-900 mb-6">Organisations</h1>
{error && (
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">{error}</div>
)}
<div className="bg-white rounded-xl border border-gray-200 overflow-hidden">
<div className="overflow-x-auto">
<table className="w-full">
<thead>
<tr className="bg-gray-50 border-b">
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Organisation</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Utilisateurs</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Plan</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Limites</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Statut</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Consommation</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Créée le</th>
<th className="text-left px-4 py-3 text-sm font-medium text-gray-600">Actions</th>
</tr>
</thead>
<tbody>
{orgs.map((org) => (
<tr key={org._id} className="border-b last:border-0 hover:bg-gray-50">
<td className="px-4 py-3">
<div className="flex items-center gap-2">
<Building2 className="w-4 h-4 text-gray-400" />
<span className="text-sm font-medium text-gray-900">{org.name}</span>
</div>
{org.owner?.email && (
<p className="text-xs text-gray-500 mt-0.5 ml-6">{org.owner.email}</p>
)}
</td>
<td className="px-4 py-3">
<button
type="button"
onClick={() => setMembersOrg(org)}
className="inline-flex items-center gap-1.5 px-2.5 py-1 text-sm rounded-lg border border-gray-200 text-gray-700 hover:bg-indigo-50 hover:border-indigo-200 hover:text-indigo-700 transition-colors"
aria-label={`Voir les utilisateurs de ${org.name}`}
title="Voir les utilisateurs rattachés"
>
<Users className="w-4 h-4 text-gray-400" />
{org.membersCount ?? 0}
</button>
</td>
<td className="px-4 py-3">
<span className="text-sm text-gray-700">{org.plan?.name || 'Gratuit'}</span>
</td>
<td className="px-4 py-3 text-xs text-gray-600">
{(() => {
const lim = org.plan?.customLimits || org.plan?.limits
if (!lim) return <span className="text-gray-400">—</span>
return (
<div className="space-y-0.5">
<div>KB: {formatUsageValue(lim.maxKbItems)}</div>
<div>LLM: {formatUsageValue(lim.maxLlmCost, 'cost', lim.maxLlmCost)}</div>
<div>OCR: {formatUsageValue(lim.maxOcrCost, 'cost', lim.maxOcrCost)}</div>
</div>
)
})()}
</td>
<td className="px-4 py-3">
<span className={`inline-flex px-2 py-0.5 rounded-full text-xs font-medium ${
org.plan?.status === 'active' ? 'bg-green-100 text-green-800'
: org.plan?.status === 'past_due' ? 'bg-red-100 text-red-800'
: 'bg-gray-100 text-gray-800'
}`}>
{org.plan?.status || 'active'}
</span>
</td>
<td className="px-4 py-3">
{org.usage ? (
<div
className="space-y-1"
title={
org.usage.periodStart && org.usage.periodEnd
? `Période : ${new Date(org.usage.periodStart).toLocaleDateString()} → ${new Date(org.usage.periodEnd).toLocaleDateString()}`
: undefined
}
>
<QuotaBar
variant="compact"
label="KB"
used={org.usage.kbItems.used}
limit={org.usage.kbItems.limit}
/>
<QuotaBar
variant="compact"
label="LLM"
used={org.usage.llmCost.used}
limit={org.usage.llmCost.limit}
type="cost"
/>
<QuotaBar
variant="compact"
label="OCR"
used={org.usage.ocrCost.used}
limit={org.usage.ocrCost.limit}
type="cost"
/>
</div>
) : (
<span className="text-sm text-gray-400">—</span>
)}
</td>
<td className="px-4 py-3 text-sm text-gray-500">
{new Date(org.createdAt).toLocaleDateString()}
</td>
<td className="px-4 py-3">
<button
onClick={() => setSelectedOrg(org)}
className="flex items-center gap-1 px-3 py-1.5 text-sm text-indigo-600 hover:bg-indigo-50 rounded-lg transition-colors"
>
<Settings className="w-4 h-4" />
Changer
</button>
</td>
</tr>
))}
{orgs.length === 0 && (
<tr>
<td colSpan="8" className="text-center py-8 text-sm text-gray-500">
Aucune organisation trouvée
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
{membersOrg && (
<MembersModal
org={membersOrg}
onClose={() => setMembersOrg(null)}
/>
)}
{selectedOrg && (
<PlanModal
org={selectedOrg}
onClose={() => setSelectedOrg(null)}
onPlanChange={handlePlanChange}
/>
)}
</div>
)
}
export default AdminOrganizations
|