340 lines
18 KiB
TypeScript
340 lines
18 KiB
TypeScript
import React from 'react';
|
|
import { Shield, RefreshCw, Download, CheckCircle } from 'lucide-react';
|
|
|
|
interface CustomerDrawerProps {
|
|
customerId: string | null;
|
|
customerData: any | null;
|
|
loading: boolean;
|
|
statusToggling: boolean;
|
|
activeTab: 'overview' | 'subscriptions' | 'invoices';
|
|
onTabChange: (tab: 'overview' | 'subscriptions' | 'invoices') => void;
|
|
onToggleStatus: () => void;
|
|
onClose: () => void;
|
|
onDownloadInvoice?: (paymentId: string) => void;
|
|
}
|
|
|
|
export const CustomerDrawer: React.FC<CustomerDrawerProps> = ({
|
|
customerId,
|
|
customerData,
|
|
loading,
|
|
statusToggling,
|
|
activeTab,
|
|
onTabChange,
|
|
onToggleStatus,
|
|
onClose,
|
|
onDownloadInvoice,
|
|
}) => {
|
|
if (!customerId) return null;
|
|
|
|
const formatCurrency = (amount: number, currency: string = 'INR') => {
|
|
const sym = currency === 'USD' ? '$' : currency === 'EUR' ? '€' : '₹';
|
|
return `${sym}${Number(amount || 0).toFixed(2)}`;
|
|
};
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 bg-slate-950/60 backdrop-blur-xs flex justify-end animate-in fade-in duration-200">
|
|
<div className="w-full max-w-xl bg-white h-full shadow-2xl flex flex-col justify-between overflow-hidden animate-in slide-in-from-right duration-200">
|
|
{/* Drawer Top Bar */}
|
|
<div className="p-4 sm:p-5 border-b border-slate-200 bg-slate-50 flex items-center justify-between shrink-0 gap-3">
|
|
<div className="flex items-center gap-3 min-w-0">
|
|
<div className="h-10 w-10 sm:h-11 sm:w-11 rounded-2xl bg-indigo-600 text-white font-black text-sm sm:text-base flex items-center justify-center shadow-md shrink-0">
|
|
{customerData?.display_name ? customerData.display_name[0].toUpperCase() : 'U'}
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
<h3 className="text-sm sm:text-base font-black text-slate-800 truncate">
|
|
{customerData?.display_name || 'Customer Account'}
|
|
</h3>
|
|
{customerData && (
|
|
<span
|
|
className={`text-[10px] font-black uppercase px-2 py-0.5 rounded-full border shrink-0 ${
|
|
customerData.is_active
|
|
? 'bg-emerald-50 text-emerald-700 border-emerald-200'
|
|
: 'bg-rose-50 text-rose-700 border-rose-200'
|
|
}`}
|
|
>
|
|
{customerData.is_active ? 'Active' : 'Suspended'}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<p className="text-xs text-slate-400 font-semibold truncate">{customerData?.email || 'Loading...'}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="h-8 w-8 rounded-full bg-slate-200/80 hover:bg-slate-300 text-slate-600 flex items-center justify-center text-sm font-black transition-colors cursor-pointer shrink-0"
|
|
aria-label="Close customer drawer"
|
|
>
|
|
✕
|
|
</button>
|
|
</div>
|
|
|
|
{/* Drawer Body */}
|
|
<div className="flex-1 overflow-y-auto p-4 sm:p-6 space-y-5 sm:space-y-6">
|
|
{loading ? (
|
|
<div className="flex flex-col items-center justify-center py-20 space-y-3">
|
|
<RefreshCw className="h-8 w-8 animate-spin text-indigo-600" />
|
|
<p className="text-xs font-bold text-slate-400">Loading customer profile & entitlements...</p>
|
|
</div>
|
|
) : customerData ? (
|
|
<>
|
|
{/* Account Status Control */}
|
|
<div className="bg-slate-50 border border-slate-200 rounded-2xl p-4 flex flex-col sm:flex-row sm:items-center justify-between gap-3 shadow-2xs">
|
|
<div className="space-y-0.5 min-w-0">
|
|
<div className="flex items-center gap-1.5">
|
|
<Shield className="h-3.5 w-3.5 text-indigo-600 shrink-0" />
|
|
<span className="text-xs font-bold text-slate-800">Account Access Control</span>
|
|
</div>
|
|
<p className="text-[11px] text-slate-500 font-medium">
|
|
{customerData.is_active
|
|
? 'Account is active. Customer has login access to Web and Mobile.'
|
|
: 'Account is suspended. Customer is blocked from logging in.'}
|
|
</p>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={onToggleStatus}
|
|
disabled={statusToggling}
|
|
className={`self-start sm:self-auto shrink-0 px-3.5 py-1.5 rounded-xl text-xs font-black transition-all flex items-center gap-1.5 cursor-pointer shadow-xs disabled:opacity-50 ${
|
|
customerData.is_active
|
|
? 'bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200'
|
|
: 'bg-emerald-600 hover:bg-emerald-500 text-white shadow-emerald-600/20'
|
|
}`}
|
|
>
|
|
{statusToggling ? (
|
|
<RefreshCw className="h-3 w-3 animate-spin" />
|
|
) : customerData.is_active ? (
|
|
'Suspend Account'
|
|
) : (
|
|
'Re-Activate Account'
|
|
)}
|
|
</button>
|
|
</div>
|
|
|
|
{/* Drawer Sub-Tabs */}
|
|
<div className="flex border-b border-slate-200 gap-1.5 pb-1 overflow-x-auto select-none">
|
|
{[
|
|
{ id: 'overview' as const, label: 'Overview' },
|
|
{
|
|
id: 'subscriptions' as const,
|
|
label: `Plans & Modules (${customerData.subscriptions?.length || 0})`,
|
|
},
|
|
{
|
|
id: 'invoices' as const,
|
|
label: `Invoices (${customerData.transactions?.length || 0})`,
|
|
},
|
|
].map((tab) => (
|
|
<button
|
|
key={tab.id}
|
|
type="button"
|
|
onClick={() => onTabChange(tab.id)}
|
|
className={`px-3 py-2 text-xs font-bold rounded-xl transition-all cursor-pointer shrink-0 whitespace-nowrap ${
|
|
activeTab === tab.id
|
|
? 'bg-indigo-600 text-white shadow-xs'
|
|
: 'text-slate-500 hover:text-slate-800 hover:bg-slate-100'
|
|
}`}
|
|
>
|
|
{tab.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{/* TAB 1: OVERVIEW */}
|
|
{activeTab === 'overview' && (
|
|
<div className="space-y-4">
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div className="bg-slate-50 rounded-2xl p-3 border border-slate-200/60 min-w-0">
|
|
<span className="text-[10px] font-bold text-slate-400 uppercase tracking-wider block">Total Spent</span>
|
|
<p className="text-lg sm:text-xl font-black text-slate-800 mt-0.5 truncate">
|
|
{customerData.total_spent_formatted ||
|
|
formatCurrency(
|
|
customerData.total_spent ?? customerData.total_spent_inr ?? 0,
|
|
customerData.currency || 'INR'
|
|
)}
|
|
</p>
|
|
</div>
|
|
<div className="bg-slate-50 rounded-2xl p-3 border border-slate-200/60 min-w-0">
|
|
<span className="text-[10px] font-bold text-slate-400 uppercase tracking-wider block">Total Orders</span>
|
|
<p className="text-lg sm:text-xl font-black text-slate-800 mt-0.5 truncate">{customerData.total_orders || 0}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="bg-white border border-slate-200 rounded-2xl p-4 space-y-3">
|
|
<h4 className="text-xs font-bold text-slate-700 uppercase tracking-wider border-b border-slate-100 pb-2">
|
|
Contact & Account Information
|
|
</h4>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-3 text-xs">
|
|
<div className="min-w-0">
|
|
<span className="text-slate-400 font-semibold block text-[10px]">Email Address</span>
|
|
<span className="font-bold text-slate-700 break-all">{customerData.email}</span>
|
|
</div>
|
|
<div className="min-w-0">
|
|
<span className="text-slate-400 font-semibold block text-[10px]">Phone Number</span>
|
|
<span className="font-bold text-slate-700 break-words">{customerData.phone_number || 'Not provided'}</span>
|
|
</div>
|
|
<div className="min-w-0">
|
|
<span className="text-slate-400 font-semibold block text-[10px]">Country</span>
|
|
<span className="font-bold text-slate-700">{customerData.country || 'India'}</span>
|
|
</div>
|
|
<div className="min-w-0">
|
|
<span className="text-slate-400 font-semibold block text-[10px]">Assigned Roles</span>
|
|
<span className="font-bold text-indigo-600 capitalize">
|
|
{customerData.roles?.join(', ') || 'user'}
|
|
</span>
|
|
</div>
|
|
<div className="min-w-0">
|
|
<span className="text-slate-400 font-semibold block text-[10px]">Member Since</span>
|
|
<span className="font-bold text-slate-700">
|
|
{customerData.created_at ? customerData.created_at.split('T')[0] : 'N/A'}
|
|
</span>
|
|
</div>
|
|
{customerData.external_ref && (
|
|
<div className="min-w-0">
|
|
<span className="text-slate-400 font-semibold block text-[10px]">Gateway Ref</span>
|
|
<span className="font-bold font-mono text-[10px] text-slate-600 break-all">{customerData.external_ref}</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* TAB 2: SUBSCRIPTIONS */}
|
|
{activeTab === 'subscriptions' && (
|
|
<div className="space-y-3">
|
|
{(!customerData.subscriptions || customerData.subscriptions.length === 0) ? (
|
|
<p className="text-xs text-slate-400 italic py-6 text-center">No active or past plan subscriptions found.</p>
|
|
) : (
|
|
customerData.subscriptions.map((sub: any, idx: number) => {
|
|
const planName = sub.plan?.name || sub.plan_name || sub.plan?.code || sub.plan_code || 'Subscribed Plan';
|
|
const planCode = sub.plan?.code || sub.plan_code;
|
|
const modules = sub.plan?.modules || [];
|
|
|
|
return (
|
|
<div key={idx} className="bg-slate-50 border border-slate-200 rounded-xl p-4 text-xs space-y-2">
|
|
<div className="flex justify-between items-start gap-2">
|
|
<div>
|
|
<h5 className="font-bold text-slate-800 text-sm">{planName}</h5>
|
|
{planCode && (
|
|
<span className="text-[9px] font-mono font-bold text-indigo-600 bg-indigo-50 border border-indigo-200 px-1.5 py-0.5 rounded uppercase mt-0.5 inline-block">
|
|
{planCode}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<span
|
|
className={`px-2 py-0.5 rounded text-[10px] font-bold ${
|
|
sub.status === 'active'
|
|
? 'bg-emerald-50 text-emerald-700 border border-emerald-200'
|
|
: 'bg-slate-200 text-slate-600'
|
|
}`}
|
|
>
|
|
{sub.status}
|
|
</span>
|
|
</div>
|
|
|
|
{modules.length > 0 && (
|
|
<div className="flex flex-wrap gap-1 pt-1">
|
|
{modules.map((m: any, mIdx: number) => (
|
|
<span
|
|
key={mIdx}
|
|
className="bg-white border border-slate-200 text-slate-600 text-[9px] font-semibold px-2 py-0.5 rounded-md flex items-center gap-1 shadow-2xs"
|
|
>
|
|
<CheckCircle className="h-2.5 w-2.5 text-emerald-500" />
|
|
{m.name || m.code}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
<div className="pt-2 border-t border-slate-200/60 flex justify-between text-[10px] text-slate-400">
|
|
<span>
|
|
Valid: {sub.started_at ? sub.started_at.split('T')[0] : 'N/A'} →{' '}
|
|
{sub.expires_at ? sub.expires_at.split('T')[0] : 'Lifetime'}
|
|
</span>
|
|
{sub.remaining_days !== null && sub.remaining_days !== undefined && (
|
|
<span className={sub.is_expired ? 'text-rose-500 font-bold' : 'text-emerald-600 font-bold'}>
|
|
{sub.is_expired ? 'Expired' : `${sub.remaining_days} days left`}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* TAB 3: INVOICES */}
|
|
{activeTab === 'invoices' && (
|
|
<div className="space-y-3">
|
|
{(!customerData.transactions || customerData.transactions.length === 0) ? (
|
|
<p className="text-xs text-slate-400 italic py-6 text-center">No payment transactions found.</p>
|
|
) : (
|
|
customerData.transactions.map((tx: any, idx: number) => {
|
|
const invNumber = tx.invoice?.invoice_number || (tx.gateway_transaction_id ? `Tx: ${tx.gateway_transaction_id}` : tx.id);
|
|
const paymentId = tx.gateway_transaction_id || tx.id;
|
|
const amount = tx.invoice?.total_amount !== undefined ? tx.invoice.total_amount : tx.amount;
|
|
const curr = tx.currency || tx.invoice?.currency || 'INR';
|
|
|
|
return (
|
|
<div
|
|
key={idx}
|
|
className="flex flex-col sm:flex-row sm:items-center justify-between gap-2.5 bg-slate-50 border border-slate-200 rounded-xl p-3.5 text-xs shadow-2xs"
|
|
>
|
|
<div className="space-y-0.5 min-w-0">
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
<span className="font-mono font-bold text-slate-800 break-all">{invNumber}</span>
|
|
{tx.invoice?.invoice_number && (
|
|
<span className="text-[9px] bg-indigo-50 border border-indigo-200 text-indigo-700 font-bold px-1.5 py-0.2 rounded shrink-0">
|
|
Invoice
|
|
</span>
|
|
)}
|
|
</div>
|
|
<p className="text-[10px] text-slate-400 break-all">
|
|
{tx.created_at?.split('T')[0] || '—'}
|
|
{tx.gateway_transaction_id && ` • ${tx.gateway_transaction_id}`}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between sm:justify-end gap-3 shrink-0 pt-1 sm:pt-0 border-t sm:border-t-0 border-slate-200/60">
|
|
<div className="text-left sm:text-right">
|
|
<p className="font-bold text-slate-800">{formatCurrency(amount, curr)}</p>
|
|
<span
|
|
className={`text-[10px] font-bold ${
|
|
tx.status === 'success' || tx.status === 'captured'
|
|
? 'text-emerald-600'
|
|
: 'text-rose-600'
|
|
}`}
|
|
>
|
|
{tx.status}
|
|
</span>
|
|
</div>
|
|
|
|
{onDownloadInvoice && paymentId && (
|
|
<button
|
|
type="button"
|
|
onClick={() => onDownloadInvoice(paymentId)}
|
|
className="h-7 w-7 flex items-center justify-center bg-indigo-50 hover:bg-indigo-100 border border-indigo-200 text-indigo-700 rounded-lg cursor-pointer transition-colors shadow-xs"
|
|
title="Download Invoice PDF"
|
|
>
|
|
<Download className="h-3.5 w-3.5" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
</div>
|
|
)}
|
|
</>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|