forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-subscriptions-dialog.tsx
More file actions
470 lines (447 loc) · 15.2 KB
/
Copy pathuser-subscriptions-dialog.tsx
File metadata and controls
470 lines (447 loc) · 15.2 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Ban, Plus, RotateCcw, Trash2 } from 'lucide-react'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { ConfirmDialog } from '@/components/confirm-dialog'
import {
DataTableRowActionMenu,
StaticDataTable,
} from '@/components/data-table'
import {
sideDrawerContentClassName,
sideDrawerFormClassName,
sideDrawerHeaderClassName,
} from '@/components/drawer-layout'
import { StatusBadge } from '@/components/status-badge'
import { TableId } from '@/components/table-id'
import { Button } from '@/components/ui/button'
import {
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuShortcut,
} from '@/components/ui/dropdown-menu'
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import {
Sheet,
SheetContent,
SheetHeader,
SheetTitle,
SheetDescription,
} from '@/components/ui/sheet'
import { Switch } from '@/components/ui/switch'
import { formatQuota } from '@/lib/format'
import {
getAdminPlans,
getUserSubscriptions,
createUserSubscription,
invalidateUserSubscription,
deleteUserSubscription,
resetUserSubscriptionsByPlan,
} from '../../api'
import { formatTimestamp } from '../../lib'
import type { PlanRecord, UserSubscriptionRecord } from '../../types'
interface Props {
open: boolean
onOpenChange: (open: boolean) => void
user: { id: number; username?: string } | null
onSuccess?: () => void
}
function SubscriptionStatusBadge(props: {
sub: UserSubscriptionRecord['subscription']
t: (key: string) => string
}) {
// eslint-disable-next-line react-hooks/purity
const now = Date.now() / 1000
const isExpired = (props.sub.end_time || 0) > 0 && props.sub.end_time < now
const isActive = props.sub.status === 'active' && !isExpired
if (isActive) {
return (
<StatusBadge
label={props.t('Active')}
variant='success'
copyable={false}
/>
)
}
if (props.sub.status === 'cancelled') {
return (
<StatusBadge
label={props.t('Invalidated')}
variant='neutral'
copyable={false}
/>
)
}
return (
<StatusBadge
label={props.t('Expired')}
variant='neutral'
copyable={false}
/>
)
}
export function UserSubscriptionsDialog(props: Props) {
const { t } = useTranslation()
const [loading, setLoading] = useState(false)
const [creating, setCreating] = useState(false)
const [plans, setPlans] = useState<PlanRecord[]>([])
const [subs, setSubs] = useState<UserSubscriptionRecord[]>([])
const [selectedPlanId, setSelectedPlanId] = useState<string>('')
const [resetting, setResetting] = useState(false)
const [advanceResetTime, setAdvanceResetTime] = useState(true)
const [resetAction, setResetAction] = useState<{
planId: number
planTitle: string
} | null>(null)
const [confirmAction, setConfirmAction] = useState<{
type: 'invalidate' | 'delete'
subId: number
} | null>(null)
const planTitleMap = useMemo(() => {
const map = new Map<number, string>()
plans.forEach((p) => {
if (p.plan.id) map.set(p.plan.id, p.plan.title || `#${p.plan.id}`)
})
return map
}, [plans])
const loadData = useCallback(async () => {
if (!props.user?.id) return
setLoading(true)
try {
const [plansRes, subsRes] = await Promise.all([
getAdminPlans(),
getUserSubscriptions(props.user.id),
])
if (plansRes.success) setPlans(plansRes.data || [])
if (subsRes.success) setSubs(subsRes.data || [])
} catch {
toast.error(t('Loading failed'))
} finally {
setLoading(false)
}
}, [props.user?.id, t])
useEffect(() => {
if (props.open && props.user?.id) {
setSelectedPlanId('')
loadData()
}
}, [props.open, props.user?.id, loadData])
const handleCreate = async () => {
if (!props.user?.id || !selectedPlanId) {
toast.error(t('Please select a subscription plan'))
return
}
setCreating(true)
try {
const res = await createUserSubscription(props.user.id, {
plan_id: Number(selectedPlanId),
})
if (res.success) {
toast.success(res.data?.message || t('Added successfully'))
setSelectedPlanId('')
await loadData()
props.onSuccess?.()
}
} catch {
toast.error(t('Request failed'))
} finally {
setCreating(false)
}
}
const handleConfirmAction = async () => {
if (!confirmAction) return
try {
if (confirmAction.type === 'invalidate') {
const res = await invalidateUserSubscription(confirmAction.subId)
if (res.success) {
toast.success(res.data?.message || t('Has been invalidated'))
await loadData()
props.onSuccess?.()
}
} else {
const res = await deleteUserSubscription(confirmAction.subId)
if (res.success) {
toast.success(t('Deleted'))
await loadData()
props.onSuccess?.()
}
}
} catch {
toast.error(t('Operation failed'))
} finally {
setConfirmAction(null)
}
}
const handleResetConfirm = async () => {
if (!props.user?.id || !resetAction) return
setResetting(true)
try {
const res = await resetUserSubscriptionsByPlan(props.user.id, {
plan_id: resetAction.planId,
advance_reset_time: advanceResetTime,
})
if (res.success) {
toast.success(
t('Reset {{count}} active subscriptions', {
count: res.data?.reset_count || 0,
})
)
await loadData()
props.onSuccess?.()
}
} catch {
toast.error(t('Operation failed'))
} finally {
setResetting(false)
setResetAction(null)
}
}
return (
<>
<Sheet open={props.open} onOpenChange={props.onOpenChange}>
<SheetContent className={sideDrawerContentClassName('sm:max-w-2xl')}>
<SheetHeader className={sideDrawerHeaderClassName()}>
<SheetTitle>{t('User Subscription Management')}</SheetTitle>
<SheetDescription>
{props.user?.username || '-'} (ID: {props.user?.id || '-'})
</SheetDescription>
</SheetHeader>
<div className={sideDrawerFormClassName()}>
<div className='flex gap-2'>
<Select
items={plans.map((p) => ({
value: String(p.plan.id),
label: (
<>
{p.plan.title}($
{Number(p.plan.price_amount || 0).toFixed(2)})
</>
),
}))}
value={selectedPlanId}
onValueChange={(v) => v !== null && setSelectedPlanId(v)}
>
<SelectTrigger className='flex-1'>
<SelectValue placeholder={t('Select subscription plan')} />
</SelectTrigger>
<SelectContent alignItemWithTrigger={false}>
<SelectGroup>
{plans.map((p) => (
<SelectItem key={p.plan.id} value={String(p.plan.id)}>
{p.plan.title} ($
{Number(p.plan.price_amount || 0).toFixed(2)})
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<Button
onClick={handleCreate}
disabled={creating || !selectedPlanId}
>
<Plus className='mr-1 h-4 w-4' />
{t('Add subscription')}
</Button>
</div>
<StaticDataTable
data={loading ? [] : subs}
getRowKey={(record) => record.subscription.id}
emptyClassName={loading ? 'py-8' : 'text-muted-foreground py-8'}
emptyContent={
loading ? t('Loading...') : t('No subscription records')
}
columns={[
{
id: 'id',
header: t('ID'),
cell: (record) => <TableId value={record.subscription.id} />,
},
{
id: 'plan',
header: t('Plan'),
cell: (record) => {
const sub = record.subscription
return (
<div>
<div className='font-medium'>
{planTitleMap.get(sub.plan_id) || `#${sub.plan_id}`}
</div>
<div className='text-muted-foreground text-sm'>
{t('Source')}: {sub.source || '-'}
</div>
</div>
)
},
},
{
id: 'status',
header: t('Status'),
cell: (record) => (
<SubscriptionStatusBadge sub={record.subscription} t={t} />
),
},
{
id: 'validity',
header: t('Validity'),
cell: (record) => {
const sub = record.subscription
return (
<div className='text-sm'>
<div>
{t('Start')}: {formatTimestamp(sub.start_time)}
</div>
<div>
{t('End')}: {formatTimestamp(sub.end_time)}
</div>
</div>
)
},
},
{
id: 'quota',
header: t('Total Quota'),
cell: (record) => {
const sub = record.subscription
const total = Number(sub.amount_total || 0)
const used = Number(sub.amount_used || 0)
return total > 0
? `${formatQuota(used)}/${formatQuota(total)}`
: t('Unlimited')
},
},
{
id: 'actions',
header: t('Actions'),
className: 'text-right',
cellClassName: 'text-right',
cell: (record) => {
const sub = record.subscription
const now = Date.now() / 1000
const isExpired =
(sub.end_time || 0) > 0 && sub.end_time < now
const isActive = sub.status === 'active' && !isExpired
return (
<DataTableRowActionMenu ariaLabel={t('Actions')}>
<DropdownMenuItem
disabled={!isActive}
onClick={() => {
setAdvanceResetTime(true)
setResetAction({
planId: sub.plan_id,
planTitle:
planTitleMap.get(sub.plan_id) ||
`#${sub.plan_id}`,
})
}}
>
{t('Reset quota')}
<DropdownMenuShortcut>
<RotateCcw size={16} />
</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem
disabled={!isActive}
onClick={() =>
setConfirmAction({
type: 'invalidate',
subId: sub.id,
})
}
>
{t('Invalidate')}
<DropdownMenuShortcut>
<Ban size={16} />
</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
variant='destructive'
onClick={() =>
setConfirmAction({
type: 'delete',
subId: sub.id,
})
}
>
{t('Delete')}
<DropdownMenuShortcut>
<Trash2 size={16} />
</DropdownMenuShortcut>
</DropdownMenuItem>
</DataTableRowActionMenu>
)
},
},
]}
/>
</div>
</SheetContent>
</Sheet>
{confirmAction && (
<ConfirmDialog
open
onOpenChange={(v) => !v && setConfirmAction(null)}
title={
confirmAction.type === 'invalidate'
? t('Confirm invalidate')
: t('Confirm delete')
}
desc={
confirmAction.type === 'invalidate'
? t(
'After invalidating, this subscription will be immediately deactivated. Historical records are not affected. Continue?'
)
: t(
'Deleting will permanently remove this subscription record (including benefit details). Continue?'
)
}
handleConfirm={handleConfirmAction}
destructive={confirmAction.type === 'delete'}
/>
)}
{resetAction && (
<ConfirmDialog
open
onOpenChange={(v) => !v && setResetAction(null)}
title={t('Reset subscription quota')}
desc={t('Reset active {{plan}} subscriptions for this user?', {
plan: resetAction.planTitle,
})}
confirmText={t('Reset quota')}
handleConfirm={handleResetConfirm}
isLoading={resetting}
>
<label className='flex items-center justify-between gap-3 rounded-md border px-3 py-2 text-sm'>
<span>{t('Advance next reset time')}</span>
<Switch
checked={advanceResetTime}
onCheckedChange={(checked) => setAdvanceResetTime(!!checked)}
aria-label={t('Advance next reset time')}
/>
</label>
</ConfirmDialog>
)}
</>
)
}