'use client'; import { useMemo } from 'react'; import type { ColumnDef } from '@tanstack/react-table'; import { Edit, RotateCcw } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { PERMISSIONS } from '@/constants/permissions'; import { usePermissions } from '@/hooks/usePermissions'; import type { Plan } from '@/types'; function formatDate(value?: string | null) { if (!value) return '-'; return new Intl.DateTimeFormat('en-IN', { day: '2-digit', month: 'short', year: 'numeric', }).format(new Date(value)); } function formatPrice(value: string, cycle: string) { const amount = Number(value); const price = Number.isFinite(amount) ? amount.toFixed(2) : value; return `${price} / ${cycle}`; } interface UsePlanColumnsParams { onEdit: (plan: Plan) => void; onToggleStatus: (plan: Plan) => void; pendingPlanId?: number; } export function usePlanColumns({ onEdit, onToggleStatus, pendingPlanId, }: UsePlanColumnsParams): ColumnDef[] { const { hasPermission } = usePermissions(); const canEdit = hasPermission(PERMISSIONS.PLAN.UPDATE); const canDelete = hasPermission(PERMISSIONS.PLAN.DELETE); return useMemo(() => { const columns: ColumnDef[] = [ { accessorKey: 'name', header: 'Plan', cell: ({ row }) => (

{row.original.name}

{row.original.slug}

), }, { accessorKey: 'price', header: 'Price', cell: ({ row }) => formatPrice(row.original.price, row.original.billing_cycle), }, { accessorKey: 'trial_days', header: 'Trial', cell: ({ row }) => `${row.original.trial_days} days`, }, { id: 'limits', header: 'Limits', enableSorting: false, cell: ({ row }) => (

{row.original.max_projects} projects

{row.original.max_users} users

), }, { accessorKey: 'created_at', header: 'Created', cell: ({ row }) => formatDate(row.original.created_at), }, { accessorKey: 'is_active', header: 'Status', cell: ({ row }) => (
{row.original.is_active ? 'Active' : 'Inactive'} {row.original.is_custom ? Custom : null}
), }, ]; if (!canEdit && !canDelete) return columns; columns.push({ id: 'actions', header: () =>
Actions
, enableSorting: false, cell: ({ row }) => { const plan = row.original; return (
{canEdit ? ( ) : null} {canDelete ? ( ) : null}
); }, }); return columns; }, [canDelete, canEdit, onEdit, onToggleStatus, pendingPlanId]); }