'use client'; import { Display } from '@/components/display'; import { ProTable, ProTableActions } from '@/components/pro-table'; import useGlobalStore from '@/config/use-global'; import { createUserSubscribe, deleteUserSubscribe, getUserSubscribe, updateUserSubscribe, } from '@/services/admin/user'; import { formatDate } from '@/utils/common'; import { Button } from '@workspace/ui/components/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@workspace/ui/components/dropdown-menu'; import { ConfirmButton } from '@workspace/ui/custom-components/confirm-button'; import { useTranslations } from 'next-intl'; import Link from 'next/link'; import { useRef, useState } from 'react'; import { toast } from 'sonner'; import { SubscriptionDetail } from './subscription-detail'; import { SubscriptionForm } from './subscription-form'; export default function UserSubscription({ userId }: { userId: number }) { const t = useTranslations('user'); const [loading, setLoading] = useState(false); const ref = useRef(null); const { getUserSubscribeUrls } = useGlobalStore(); return ( > action={ref} header={{ title: t('subscriptionList'), toolbar: ( { setLoading(true); await createUserSubscribe({ user_id: Number(userId), ...values, }); toast.success(t('createSuccess')); ref.current?.refresh(); setLoading(false); return true; }} /> ), }} columns={[ { accessorKey: 'id', header: 'ID', }, { accessorKey: 'name', header: t('subscriptionName'), cell: ({ row }) => row.original.subscribe.name, }, { accessorKey: 'upload', header: t('upload'), cell: ({ row }) => , }, { accessorKey: 'download', header: t('download'), cell: ({ row }) => , }, { accessorKey: 'traffic', header: t('totalTraffic'), cell: ({ row }) => , }, { accessorKey: 'speed_limit', header: t('speedLimit'), cell: ({ row }) => { const speed = row.original?.subscribe?.speed_limit; return ; }, }, { accessorKey: 'device_limit', header: t('deviceLimit'), cell: ({ row }) => { const limit = row.original?.subscribe?.device_limit; return ; }, }, { accessorKey: 'reset_time', header: t('resetTime'), cell: ({ row }) => { return ; }, }, { accessorKey: 'expire_time', header: t('expireTime'), cell: ({ row }) => row.getValue('expire_time') ? formatDate(row.getValue('expire_time')) : t('permanent'), }, { accessorKey: 'created_at', header: t('createdAt'), cell: ({ row }) => formatDate(row.getValue('created_at')), }, ]} request={async (pagination) => { const { data } = await getUserSubscribe({ user_id: userId, ...pagination, }); return { list: data.data?.list || [], total: data.data?.total || 0, }; }} actions={{ render: (row) => { return [ { setLoading(true); await updateUserSubscribe({ user_id: Number(userId), user_subscribe_id: row.id, ...values, }); toast.success(t('updateSuccess')); ref.current?.refresh(); setLoading(false); return true; }} />, , {t('delete')}} title={t('confirmDelete')} description={t('deleteSubscriptionDescription')} onConfirm={async () => { await deleteUserSubscribe({ user_subscribe_id: row.id }); toast.success(t('deleteSuccess')); ref.current?.refresh(); }} cancelText={t('cancel')} confirmText={t('confirm')} />, , ]; }, }} /> ); } function RowMoreActions({ userId, subId }: { userId: number; subId: number }) { const triggerRef = useRef(null); const t = useTranslations('user'); return (
{t('subscriptionLogs')} {t('resetLogs')} {t('trafficStats')} {t('trafficDetails')} { e.preventDefault(); triggerRef.current?.click(); }} > {t('onlineDevices')} } userId={userId} subscriptionId={subId} />
); }