import { useCallback, useEffect, useMemo, useState } from "react" import { Calendar, CreditCard, Package, Pencil, Plus, RefreshCw, Search, Tag, Trash2, } from "lucide-react" import { toast } from "sonner" import { getSubscriptionPlans } from "../../api/subscription-plans.api" import { Badge } from "../../components/ui/badge" import { Button } from "../../components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "../../components/ui/card" import { Input } from "../../components/ui/input" import { SpinnerIcon } from "../../components/ui/spinner-icon" import { cn } from "../../lib/utils" import { formatPlanCategory, formatPlanCreatedAt, formatPlanDuration, formatPlanPrice, } from "../../lib/subscriptionPlans" import type { SubscriptionPlan } from "../../types/subscription.types" import { CreateSubscriptionPlanDialog } from "./components/CreateSubscriptionPlanDialog" import { DeleteSubscriptionPlanDialog } from "./components/DeleteSubscriptionPlanDialog" import { EditSubscriptionPlanDialog } from "./components/EditSubscriptionPlanDialog" export function SubscriptionPlansTab() { const [loading, setLoading] = useState(true) const [error, setError] = useState(false) const [plans, setPlans] = useState([]) const [query, setQuery] = useState("") const [statusFilter, setStatusFilter] = useState<"all" | "active" | "inactive">("all") const [createOpen, setCreateOpen] = useState(false) const [planToEdit, setPlanToEdit] = useState(null) const [planToDelete, setPlanToDelete] = useState(null) const load = useCallback(async () => { setLoading(true) setError(false) try { const res = await getSubscriptionPlans() setPlans(res.data) } catch (e) { console.error(e) setError(true) setPlans([]) toast.error("Failed to load subscription packages") } finally { setLoading(false) } }, []) useEffect(() => { void load() }, [load]) const filtered = useMemo(() => { const q = query.trim().toLowerCase() return [...plans] .filter((plan) => { if (statusFilter === "active" && !plan.is_active) return false if (statusFilter === "inactive" && plan.is_active) return false if (!q) return true const haystack = [plan.name, plan.description, plan.category, plan.currency] .join(" ") .toLowerCase() return haystack.includes(q) }) .sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) }, [plans, query, statusFilter]) const activeCount = plans.filter((p) => p.is_active).length const handleCreated = (plan: SubscriptionPlan) => { setPlans((prev) => { const without = prev.filter((p) => p.id !== plan.id) return [plan, ...without] }) } const handleUpdated = (plan: SubscriptionPlan) => { setPlans((prev) => prev.map((p) => (p.id === plan.id ? plan : p))) } const handleDeleted = (id: number) => { setPlans((prev) => prev.filter((p) => p.id !== id)) } return (

Billing & catalog

Subscription packages

Manage learner subscription plans from{" "} GET /subscription-plans . Create, edit, or remove packages for the learner checkout flow.

Total packages

{plans.length}

Active

{activeCount}

Inactive

{plans.length - activeCount}

All packages
setQuery(e.target.value)} />
{( [ { id: "all", label: "All" }, { id: "active", label: "Active" }, { id: "inactive", label: "Inactive" }, ] as const ).map((tab) => ( ))}
{loading ? (

Loading packages…

) : error ? (

Could not load packages

) : filtered.length === 0 ? (

{plans.length === 0 ? "No subscription packages yet" : "No packages match your filters"}

{plans.length === 0 ? "Create your first package to offer paid access in the learner app." : "Try a different search or status filter."}

{plans.length === 0 ? ( ) : null}
) : (
{filtered.map((plan) => ( ))}
Package Category Duration Price Status Created Actions

{plan.name}

{plan.description}

{formatPlanCategory(plan.category)} {formatPlanDuration(plan)} {formatPlanPrice(plan)} {plan.is_active ? "Active" : "Inactive"} {formatPlanCreatedAt(plan.created_at)}
)}
{ if (!open) setPlanToEdit(null) }} onUpdated={handleUpdated} /> { if (!open) setPlanToDelete(null) }} onDeleted={handleDeleted} />
) }