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 | import { useState, useEffect } from 'react'
import { useParams, Link } from 'react-router-dom'
import { Loader, ExternalLink, CreditCard, AlertTriangle, CheckCircle, ArrowRight } from 'lucide-react'
import { getOrganizationId } from '../../utils/organization'
import { CONTACT_URL } from '../../constants/contact'
import api from '../../services/api'
import UsageBar from '../../frontoffice/components/UsageBar'
function Subscription() {
const { organizationId } = useParams()
const [data, setData] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const orgId = organizationId || getOrganizationId()
useEffect(() => {
if (!orgId) {
setLoading(false)
return
}
const fetchData = async () => {
try {
const [subRes, usageRes] = await Promise.all([
api.get(`/organizations/${orgId}/subscription`),
api.get(`/organizations/${orgId}/usage`),
])
setData({
subscription: subRes.data.data,
usage: usageRes.data.data,
})
} catch (err) {
setError(err.response?.data?.message || err.message)
} finally {
setLoading(false)
}
}
fetchData()
}, [orgId])
const handlePortal = async () => {
try {
const res = await api.post(`/organizations/${orgId}/subscriptions/portal`)
if (res.data.data?.url) {
window.location.href = res.data.data.url
}
} catch (err) {
setError(err.response?.data?.message || 'Error opening portal')
}
}
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>
)
}
const plan = data?.subscription?.currentPlan
const bestPlan = data?.subscription?.bestActivePlan
const orgPlan = data?.subscription?.organizationPlan
const usage = data?.usage
const trackedServices = data?.usage?.trackedServices || {}
const stripeSubs = data?.subscription?.activePlans || []
const statusBadge = {
active: { label: 'Actif', class: 'bg-green-100 text-green-800' },
past_due: { label: 'Paiement en retard', class: 'bg-red-100 text-red-800' },
canceled: { label: 'Annulé', class: 'bg-gray-100 text-gray-800' },
}
const statusInfo = statusBadge[orgPlan?.status] || statusBadge.active
return (
<div>
<h1 className="text-2xl font-bold text-gray-900 mb-6">Abonnement</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="grid lg:grid-cols-2 gap-6 mb-8">
<div className="bg-white rounded-xl border border-gray-200 p-6">
<h2 className="text-lg font-semibold text-gray-900 mb-4">Plan actuel</h2>
<div className="flex items-center justify-between mb-4">
<div>
<span className="text-2xl font-bold text-gray-900">{plan?.name || 'Gratuit'}</span>
{plan?.displayPrice?.amount > 0 && (
<span className="text-gray-500 ml-2">{plan.displayPrice.amount} € / {plan.displayPrice.interval}</span>
)}
</div>
<span className={`px-3 py-1 rounded-full text-sm font-medium ${statusInfo.class}`}>
{statusInfo.label}
</span>
</div>
<div className="space-y-2 text-sm text-gray-600 mb-6">
{(() => {
const lim = plan?.limits || {}
const fmtCost = (v) => v == null ? 'Illimité' : v >= 1000000 ? `${(v / 1000000).toFixed(2)}€` : `${(v / 10000).toFixed(2)}¢`
const fmt = (v) => v == null ? 'Illimité' : v >= 1000000 ? `${(v / 1000000).toFixed(0)}M` : v >= 1000 ? `${(v / 1000).toFixed(0)}k` : String(v)
return (
<>
<p>KB : {fmt(lim.maxKbItems)} éléments</p>
<p>LLM : {fmtCost(lim.maxLlmCost)} / mois</p>
<p>OCR : {fmtCost(lim.maxOcrCost)} / mois</p>
</>
)
})()}
</div>
{orgPlan?.planCode === 'specific_limit' ? (
<div className="bg-indigo-50 border border-indigo-200 rounded-lg p-4 text-sm text-indigo-700">
<p className="mb-3">Plan configuré par un administrateur.</p>
<a
href={CONTACT_URL}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"
>
Contacter un administrateur
</a>
</div>
) : orgPlan?.stripeCustomerId ? (
<div className="flex gap-3">
<button
onClick={handlePortal}
className="flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"
>
<ExternalLink className="w-4 h-4" />
Gérer l'abonnement
</button>
<Link
to={`/${orgId}/pricing`}
className="flex items-center gap-2 px-4 py-2 border border-indigo-300 text-indigo-700 rounded-lg hover:bg-indigo-50 text-sm font-medium"
>
<ArrowRight className="w-4 h-4" />
Changer d'abonnement
</Link>
</div>
) : (
<div>
<Link
to={`/${orgId}/pricing`}
className="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"
>
<ArrowRight className="w-4 h-4" />
Choisir un abonnement
</Link>
</div>
)}
</div>
<div className="bg-white rounded-xl border border-gray-200 p-6">
<h2 className="text-lg font-semibold text-gray-900 mb-1">Consommation</h2>
{bestPlan && (
<p className="text-xs text-gray-500 mb-3">
Basée sur le meilleur plan actif : <span className="font-medium">{bestPlan.name}</span>
</p>
)}
{usage?.period && (
<p className="text-sm text-gray-500 mb-4">
Période du {new Date(usage.period.start).toLocaleDateString()} au {new Date(usage.period.end).toLocaleDateString()}
{' '}({usage.period.daysRemaining} jours restants)
</p>
)}
{usage?.usage ? (
<div>
<UsageBar
label="Éléments KB"
used={usage.usage.kbItems.used}
limit={usage.usage.kbItems.limit}
unit=""
/>
{trackedServices.llm !== false && (
<UsageBar
label="Coût LLM"
used={usage.usage.llmCost.used}
limit={usage.usage.llmCost.limit}
unit=""
type="cost"
/>
)}
{trackedServices.llm === false && (
<p className="text-xs text-gray-500 mt-3 mb-3">
Clé OpenRouter de l'organisation — pas de limitation ni tracking côté Nooloom.
</p>
)}
{trackedServices.ocr !== false && (
<UsageBar
label="Coût OCR"
used={usage.usage.ocrCost.used}
limit={usage.usage.ocrCost.limit}
unit=""
type="cost"
/>
)}
{trackedServices.ocr === false && (
<p className="text-xs text-gray-500 mt-3 mb-3">
Clé Marker de l'organisation — pas de limitation ni tracking côté Nooloom.
</p>
)}
</div>
) : (
<p className="text-sm text-gray-500">Aucune donnée de consommation disponible.</p>
)}
</div>
</div>
{stripeSubs.length > 0 && (
<div className="bg-white rounded-xl border border-gray-200 p-6 mb-6">
<h2 className="text-lg font-semibold text-gray-900 mb-4">Abonnements actifs</h2>
<div className="space-y-4">
{stripeSubs.map((s, i) => {
const ending = s.cancelAtPeriodEnd
const isBest = bestPlan && s.planCode === bestPlan.code
const fmt = (v) => v == null ? 'Illimité' : v >= 1000000 ? `${(v / 1000000).toFixed(0)}M` : v >= 1000 ? `${(v / 1000).toFixed(0)}k` : String(v)
const fmtCost = (v) => v == null ? 'Illimité' : v >= 1000000 ? `${(v / 1000000).toFixed(2)}€` : `${(v / 10000).toFixed(2)}¢`
return (
<div key={i} className={`rounded-lg border p-4 ${isBest ? 'border-indigo-300 bg-indigo-50' : ''}`}>
<div className="flex items-center justify-between mb-2">
<div>
<span className="font-medium">{s.planName}</span>
{s.price && <span className="text-gray-500 ml-2">{s.price}€/mois</span>}
{isBest && <span className="ml-2 text-xs text-indigo-600 font-medium">(meilleur plan actif)</span>}
</div>
<div className="text-xs">
{ending ? (
<span className="text-orange-600">Prend fin le {new Date(s.currentPeriodEnd).toLocaleDateString()}</span>
) : (
<span className="text-green-600">Renouvellement le {new Date(s.currentPeriodEnd).toLocaleDateString()}</span>
)}
</div>
</div>
{s.limits && (
<div className="text-xs text-gray-500 space-x-3 mt-1">
<span>KB : {fmt(s.limits.maxKbItems)}</span>
<span>LLM : {fmtCost(s.limits.maxLlmCost)} / mois</span>
<span>OCR : {fmtCost(s.limits.maxOcrCost)} / mois</span>
</div>
)}
</div>
)
})}
</div>
</div>
)}
</div>
)
}
export default Subscription
|