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 | import { useState, useEffect } from 'react'
import { Loader, Building2, Settings } from 'lucide-react'
import api from '../../services/api'
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 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">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">Usage KB</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>
</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
const fmtCost = (v) => v == null ? '∞' : v >= 1000000 ? `${(v / 1000000).toFixed(2)}€` : `${(v / 10000).toFixed(2)}¢`
const fmt = (v) => v == null ? '∞' : v >= 1000000 ? `${(v / 1000000).toFixed(0)}M` : v >= 1000 ? `${(v / 1000).toFixed(0)}k` : String(v)
if (!lim) return <span className="text-gray-400">—</span>
return (
<div className="space-y-0.5">
<div>KB: {fmt(lim.maxKbItems)}</div>
<div>LLM: {fmtCost(lim.maxLlmCost)}</div>
<div>OCR: {fmtCost(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 text-sm text-gray-600">
{org.usage?.kbItems || 0} items
</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="7" className="text-center py-8 text-sm text-gray-500">
Aucune organisation trouvée
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
{selectedOrg && (
<PlanModal
org={selectedOrg}
onClose={() => setSelectedOrg(null)}
onPlanChange={handlePlanChange}
/>
)}
</div>
)
}
export default AdminOrganizations
|