Add client-side search and filtering on the payments page.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-03 11:21:37 -07:00
parent 8e7226b370
commit 47893a7838

View File

@ -6,6 +6,7 @@ import {
ChevronRight, ChevronRight,
Copy, Copy,
CreditCard, CreditCard,
ExternalLink,
Eye, Eye,
MoreHorizontal, MoreHorizontal,
RefreshCw, RefreshCw,
@ -16,7 +17,7 @@ import {
} from "lucide-react" } from "lucide-react"
import { Link } from "react-router-dom" import { Link } from "react-router-dom"
import { toast } from "sonner" import { toast } from "sonner"
import { getAllPayments, getPayments, paymentsFilterParams } from "../../api/payments.api" import { getAllPayments } from "../../api/payments.api"
import { AdminFiltersPanel } from "../../components/filters/AdminFiltersPanel" import { AdminFiltersPanel } from "../../components/filters/AdminFiltersPanel"
import { ExportCsvButton } from "../../components/export/ExportCsvButton" import { ExportCsvButton } from "../../components/export/ExportCsvButton"
import { ExportTruncationWarning } from "../../components/export/ExportTruncationWarning" import { ExportTruncationWarning } from "../../components/export/ExportTruncationWarning"
@ -81,10 +82,38 @@ type PaymentListFilters = {
provider: PaymentProvider | "" provider: PaymentProvider | ""
planCategory: PaymentPlanCategory | "" planCategory: PaymentPlanCategory | ""
currency: string currency: string
reference: string
} }
const TEXT_FILTER_DEBOUNCE_MS = 400 function paymentMatchesSearch(payment: Payment, query: string): boolean {
const q = query.trim().toLowerCase()
if (!q) return true
const haystack = [
String(payment.id),
String(payment.user_id),
String(payment.plan_id),
String(payment.subscription_id),
payment.session_id,
payment.transaction_id,
payment.nonce,
payment.amount,
payment.currency,
payment.payment_method,
payment.status,
payment.plan_name,
payment.plan_category,
formatPaymentPlanCategory(payment.plan_category),
payment.user_email,
payment.user_first_name,
payment.user_last_name,
paymentCustomerName(payment),
formatPaymentMethod(payment.payment_method),
formatPaymentStatus(payment.status),
formatPaymentAmount(payment),
]
.join(" ")
.toLowerCase()
return haystack.includes(q)
}
function copyText(value: string, label: string) { function copyText(value: string, label: string) {
if (!value) return if (!value) return
@ -105,38 +134,22 @@ const EMPTY_PAYMENT_STATS: PaymentAggregateStats = {
export function PaymentsPage() { export function PaymentsPage() {
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [statsLoading, setStatsLoading] = useState(true)
const [error, setError] = useState(false) const [error, setError] = useState(false)
const [payments, setPayments] = useState<Payment[]>([]) const [payments, setPayments] = useState<Payment[]>([])
const [totalCount, setTotalCount] = useState(0) const [page, setPage] = useState(1)
const [paymentStats, setPaymentStats] = useState<PaymentAggregateStats>(EMPTY_PAYMENT_STATS) const [pageSize, setPageSize] = useState(DEFAULT_TABLE_PAGE_SIZE)
const [offset, setOffset] = useState(0) const [searchQuery, setSearchQuery] = useState("")
const [pageSize, setPageSize] = useState(10)
const [statusFilter, setStatusFilter] = useState<PaymentStatus | "">("") const [statusFilter, setStatusFilter] = useState<PaymentStatus | "">("")
const [providerFilter, setProviderFilter] = useState<PaymentProvider | "">("") const [providerFilter, setProviderFilter] = useState<PaymentProvider | "">("")
const [planCategoryFilter, setPlanCategoryFilter] = useState<PaymentPlanCategory | "">("") const [planCategoryFilter, setPlanCategoryFilter] = useState<PaymentPlanCategory | "">("")
const [currencyFilter, setCurrencyFilter] = useState("") const [currencyFilter, setCurrencyFilter] = useState("")
const [referenceInput, setReferenceInput] = useState("")
const [referenceFilter, setReferenceFilter] = useState("")
const [selected, setSelected] = useState<Payment | null>(null) const [selected, setSelected] = useState<Payment | null>(null)
useEffect(() => {
const timer = window.setTimeout(() => {
setReferenceFilter(referenceInput.trim())
}, TEXT_FILTER_DEBOUNCE_MS)
return () => window.clearTimeout(timer)
}, [referenceInput])
useEffect(() => {
setOffset(0)
}, [referenceFilter])
const listFilters: PaymentListFilters = { const listFilters: PaymentListFilters = {
status: statusFilter, status: statusFilter,
provider: providerFilter, provider: providerFilter,
planCategory: planCategoryFilter, planCategory: planCategoryFilter,
currency: currencyFilter, currency: currencyFilter,
reference: referenceFilter,
} }
const exportParams = useMemo( const exportParams = useMemo(
@ -149,99 +162,87 @@ export function PaymentsPage() {
{ value: providerFilter }, { value: providerFilter },
{ value: planCategoryFilter }, { value: planCategoryFilter },
{ value: currencyFilter }, { value: currencyFilter },
{ value: referenceFilter },
]) ])
const hasActiveFilters = activeFilterCount > 0 const hasActiveFilters = activeFilterCount > 0 || Boolean(searchQuery.trim())
const fetchPayments = useCallback( const fetchPayments = useCallback(async () => {
async (nextOffset: number, limit: number, filters: PaymentListFilters) => { setLoading(true)
setLoading(true) setError(false)
setError(false)
try {
const res = await getPayments({
limit,
offset: nextOffset,
...paymentsFilterParams({
...(filters.status ? { status: filters.status } : {}),
...(filters.provider ? { provider: filters.provider } : {}),
...(filters.planCategory ? { plan_category: filters.planCategory } : {}),
...(filters.currency ? { currency: filters.currency } : {}),
...(filters.reference ? { reference: filters.reference } : {}),
}),
})
setPayments(res.data.payments)
setTotalCount(res.data.total_count)
} catch (e) {
console.error(e)
setError(true)
setPayments([])
setTotalCount(0)
notifyApiError(e, "Failed to load payments")
} finally {
setLoading(false)
}
},
[],
)
const fetchPaymentStats = useCallback(async (filters: PaymentListFilters) => {
setStatsLoading(true)
try { try {
const all = await getAllPayments({ const all = await getAllPayments({})
...(filters.status ? { status: filters.status } : {}), setPayments(all)
...(filters.provider ? { provider: filters.provider } : {}),
...(filters.planCategory ? { plan_category: filters.planCategory } : {}),
...(filters.currency ? { currency: filters.currency } : {}),
...(filters.reference ? { reference: filters.reference } : {}),
})
setPaymentStats(computePaymentAggregateStats(all))
} catch (e) { } catch (e) {
console.error(e) console.error(e)
setPaymentStats(EMPTY_PAYMENT_STATS) setError(true)
notifyApiError(e, "Failed to load payment summary") setPayments([])
notifyApiError(e, "Failed to load payments")
} finally { } finally {
setStatsLoading(false) setLoading(false)
} }
}, []) }, [])
useEffect(() => { useEffect(() => {
void fetchPayments(offset, pageSize, listFilters) void fetchPayments()
}, [ }, [fetchPayments])
offset,
pageSize,
statusFilter,
providerFilter,
planCategoryFilter,
currencyFilter,
referenceFilter,
fetchPayments,
])
useEffect(() => { useEffect(() => {
void fetchPaymentStats(listFilters) setPage(1)
}, [searchQuery, statusFilter, providerFilter, planCategoryFilter, currencyFilter, pageSize])
const filteredPayments = useMemo(() => {
return payments.filter((payment) => {
if (statusFilter && payment.status !== statusFilter) return false
if (
providerFilter &&
payment.payment_method?.toUpperCase() !== providerFilter.toUpperCase()
) {
return false
}
if (planCategoryFilter && payment.plan_category !== planCategoryFilter) return false
if (
currencyFilter &&
payment.currency?.toUpperCase() !== currencyFilter.toUpperCase()
) {
return false
}
return paymentMatchesSearch(payment, searchQuery)
})
}, [ }, [
payments,
statusFilter, statusFilter,
providerFilter, providerFilter,
planCategoryFilter, planCategoryFilter,
currencyFilter, currencyFilter,
referenceFilter, searchQuery,
fetchPaymentStats,
]) ])
const paymentStats: PaymentAggregateStats = useMemo(
() =>
filteredPayments.length
? computePaymentAggregateStats(filteredPayments)
: EMPTY_PAYMENT_STATS,
[filteredPayments],
)
const totalCount = filteredPayments.length
const pageCount = Math.max(1, Math.ceil(totalCount / pageSize))
const safePage = Math.min(page, pageCount)
const paginatedPayments = useMemo(
() => filteredPayments.slice((safePage - 1) * pageSize, safePage * pageSize),
[filteredPayments, safePage, pageSize],
)
const toggleStatus = (value: PaymentStatus) => { const toggleStatus = (value: PaymentStatus) => {
setStatusFilter((current) => (current === value ? "" : value)) setStatusFilter((current) => (current === value ? "" : value))
setOffset(0)
} }
const toggleProvider = (value: PaymentProvider) => { const toggleProvider = (value: PaymentProvider) => {
setProviderFilter((current) => (current === value ? "" : value)) setProviderFilter((current) => (current === value ? "" : value))
setOffset(0)
} }
const togglePlanCategory = (value: PaymentPlanCategory) => { const togglePlanCategory = (value: PaymentPlanCategory) => {
setPlanCategoryFilter((current) => (current === value ? "" : value)) setPlanCategoryFilter((current) => (current === value ? "" : value))
setOffset(0)
} }
const clearFilters = () => { const clearFilters = () => {
@ -249,22 +250,16 @@ export function PaymentsPage() {
setProviderFilter("") setProviderFilter("")
setPlanCategoryFilter("") setPlanCategoryFilter("")
setCurrencyFilter("") setCurrencyFilter("")
setReferenceInput("") setSearchQuery("")
setReferenceFilter("") setPage(1)
setOffset(0)
}
const refreshAll = () => {
void fetchPayments(offset, pageSize, listFilters)
void fetchPaymentStats(listFilters)
} }
const { successfulCount, totalRevenue, pendingCount } = paymentStats const { successfulCount, totalRevenue, pendingCount } = paymentStats
const pageStart = totalCount === 0 ? 0 : offset + 1 const pageStart = totalCount === 0 ? 0 : (safePage - 1) * pageSize + 1
const pageEnd = Math.min(offset + payments.length, totalCount) const pageEnd = Math.min(safePage * pageSize, totalCount)
const canPrev = offset > 0 const canPrev = safePage > 1
const canNext = offset + pageSize < totalCount const canNext = safePage < pageCount
return ( return (
<div className="mx-auto min-w-0 w-full max-w-7xl space-y-6 px-4 py-6 sm:px-6 lg:px-8"> <div className="mx-auto min-w-0 w-full max-w-7xl space-y-6 px-4 py-6 sm:px-6 lg:px-8">
@ -288,8 +283,8 @@ export function PaymentsPage() {
<Button <Button
variant="outline" variant="outline"
className="shrink-0 rounded-[6px]" className="shrink-0 rounded-[6px]"
disabled={loading || statsLoading} disabled={loading}
onClick={refreshAll} onClick={() => void fetchPayments()}
> >
<RefreshCw className={cn("mr-2 h-4 w-4", loading && "animate-spin")} /> <RefreshCw className={cn("mr-2 h-4 w-4", loading && "animate-spin")} />
Refresh Refresh
@ -306,7 +301,9 @@ export function PaymentsPage() {
</div> </div>
<div> <div>
<p className="text-xs font-medium text-grayScale-500">Total transactions</p> <p className="text-xs font-medium text-grayScale-500">Total transactions</p>
<p className="text-2xl font-bold text-grayScale-900">{totalCount}</p> <p className="text-2xl font-bold text-grayScale-900">
{loading ? "…" : totalCount.toLocaleString()}
</p>
</div> </div>
</CardContent> </CardContent>
</Card> </Card>
@ -319,7 +316,7 @@ export function PaymentsPage() {
<div> <div>
<p className="text-xs font-medium text-grayScale-500">Successful payments</p> <p className="text-xs font-medium text-grayScale-500">Successful payments</p>
<p className="text-2xl font-bold text-grayScale-900"> <p className="text-2xl font-bold text-grayScale-900">
{statsLoading ? "…" : successfulCount.toLocaleString()} {loading ? "…" : successfulCount.toLocaleString()}
</p> </p>
</div> </div>
</CardContent> </CardContent>
@ -333,10 +330,10 @@ export function PaymentsPage() {
<div> <div>
<p className="text-xs font-medium text-grayScale-500">Revenue</p> <p className="text-xs font-medium text-grayScale-500">Revenue</p>
<p className="text-2xl font-bold text-grayScale-900"> <p className="text-2xl font-bold text-grayScale-900">
{statsLoading ? "…" : `${totalRevenue.toLocaleString()} ETB`} {loading ? "…" : `${totalRevenue.toLocaleString()} ETB`}
</p> </p>
<p className="text-[11px] text-grayScale-400"> <p className="text-[11px] text-grayScale-400">
{statsLoading ? "Loading summary…" : `${pendingCount.toLocaleString()} pending`} {loading ? "Loading…" : `${pendingCount.toLocaleString()} pending`}
</p> </p>
</div> </div>
</CardContent> </CardContent>
@ -349,6 +346,30 @@ export function PaymentsPage() {
</CardHeader> </CardHeader>
<CardContent className="min-w-0 space-y-4 p-4 sm:p-6"> <CardContent className="min-w-0 space-y-4 p-4 sm:p-6">
<ExportTruncationWarning totalCount={totalCount} /> <ExportTruncationWarning totalCount={totalCount} />
<div className="relative max-w-md">
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-grayScale-400" />
<Input
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search customer, email, plan, transaction ID…"
disabled={loading}
className="h-10 rounded-[6px] border-grayScale-200 pl-9 pr-9 text-sm"
aria-label="Search payments"
/>
{searchQuery ? (
<button
type="button"
aria-label="Clear search"
disabled={loading}
onClick={() => setSearchQuery("")}
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-grayScale-400 hover:text-grayScale-600"
>
<X className="h-4 w-4" />
</button>
) : null}
</div>
<AdminFiltersPanel <AdminFiltersPanel
activeFilterCount={activeFilterCount} activeFilterCount={activeFilterCount}
onClearFilters={clearFilters} onClearFilters={clearFilters}
@ -395,65 +416,27 @@ export function PaymentsPage() {
/> />
))} ))}
</div> </div>
<div className="grid gap-3 sm:grid-cols-2"> <div className="space-y-1.5 sm:max-w-xs">
<div className="space-y-1.5"> <label
<label htmlFor="payments-currency-filter"
htmlFor="payments-currency-filter" className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400"
className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400" >
> Currency
Currency </label>
</label> <Select
<Select id="payments-currency-filter"
id="payments-currency-filter" value={currencyFilter}
value={currencyFilter} onChange={(e) => setCurrencyFilter(e.target.value)}
onChange={(e) => { disabled={loading}
setCurrencyFilter(e.target.value) className="h-9 rounded-[6px] text-sm"
setOffset(0) >
}} <option value="">All currencies</option>
disabled={loading} {SUBSCRIPTION_CURRENCIES.map((currency) => (
className="h-9 rounded-[6px] text-sm" <option key={currency} value={currency}>
> {currency}
<option value="">All currencies</option> </option>
{SUBSCRIPTION_CURRENCIES.map((currency) => ( ))}
<option key={currency} value={currency}> </Select>
{currency}
</option>
))}
</Select>
</div>
<div className="space-y-1.5">
<label
htmlFor="payments-reference-filter"
className="text-[11px] font-bold uppercase tracking-wider text-grayScale-400"
>
Reference
</label>
<div className="relative">
<Search className="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-grayScale-400" />
<Input
id="payments-reference-filter"
value={referenceInput}
onChange={(e) => setReferenceInput(e.target.value)}
placeholder="Session, nonce, or transaction ID"
disabled={loading}
className="h-9 rounded-[6px] border-grayScale-200 pl-8 pr-8 text-sm"
/>
{referenceInput ? (
<button
type="button"
aria-label="Clear reference filter"
disabled={loading}
onClick={() => setReferenceInput("")}
className="absolute right-2 top-1/2 -translate-y-1/2 text-grayScale-400 hover:text-grayScale-600"
>
<X className="h-3.5 w-3.5" />
</button>
) : null}
</div>
<p className="text-[10px] text-grayScale-400">
Partial match on session ID, nonce, or transaction ID
</p>
</div>
</div> </div>
</AdminFiltersPanel> </AdminFiltersPanel>
@ -469,12 +452,12 @@ export function PaymentsPage() {
variant="outline" variant="outline"
size="sm" size="sm"
className="rounded-[6px]" className="rounded-[6px]"
onClick={refreshAll} onClick={() => void fetchPayments()}
> >
Try again Try again
</Button> </Button>
</div> </div>
) : payments.length === 0 ? ( ) : paginatedPayments.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-3 rounded-[8px] border border-dashed border-grayScale-200 py-16 text-center"> <div className="flex flex-col items-center justify-center gap-3 rounded-[8px] border border-dashed border-grayScale-200 py-16 text-center">
<CreditCard className="h-10 w-10 text-grayScale-300" /> <CreditCard className="h-10 w-10 text-grayScale-300" />
<p className="text-sm font-medium text-grayScale-700"> <p className="text-sm font-medium text-grayScale-700">
@ -482,7 +465,7 @@ export function PaymentsPage() {
</p> </p>
<p className="max-w-sm text-xs text-grayScale-500"> <p className="max-w-sm text-xs text-grayScale-500">
{hasActiveFilters {hasActiveFilters
? "Try different filters or clear them to see more results." ? "Try a different search or clear filters to see more results."
: "Transactions will appear here once customers complete checkout."} : "Transactions will appear here once customers complete checkout."}
</p> </p>
{hasActiveFilters ? ( {hasActiveFilters ? (
@ -514,7 +497,7 @@ export function PaymentsPage() {
</tr> </tr>
</thead> </thead>
<tbody className="divide-y divide-grayScale-50"> <tbody className="divide-y divide-grayScale-50">
{payments.map((payment) => ( {paginatedPayments.map((payment) => (
<tr key={payment.id} className="group transition-colors hover:bg-grayScale-50/60"> <tr key={payment.id} className="group transition-colors hover:bg-grayScale-50/60">
<td className="whitespace-nowrap px-3 py-2.5 sm:px-4"> <td className="whitespace-nowrap px-3 py-2.5 sm:px-4">
<p className="font-semibold text-grayScale-900">#{payment.id}</p> <p className="font-semibold text-grayScale-900">#{payment.id}</p>
@ -616,10 +599,7 @@ export function PaymentsPage() {
<select <select
value={pageSize} value={pageSize}
disabled={loading} disabled={loading}
onChange={(e) => { onChange={(e) => setPageSize(Number(e.target.value))}
setPageSize(Number(e.target.value))
setOffset(0)
}}
className="h-8 appearance-none rounded-md border bg-white pl-2 pr-7 text-sm font-medium text-grayScale-600 focus:outline-none" className="h-8 appearance-none rounded-md border bg-white pl-2 pr-7 text-sm font-medium text-grayScale-600 focus:outline-none"
> >
{TABLE_PAGE_SIZE_OPTIONS.map((size) => ( {TABLE_PAGE_SIZE_OPTIONS.map((size) => (
@ -639,7 +619,7 @@ export function PaymentsPage() {
size="sm" size="sm"
className="rounded-[6px]" className="rounded-[6px]"
disabled={!canPrev || loading} disabled={!canPrev || loading}
onClick={() => setOffset((o) => Math.max(0, o - pageSize))} onClick={() => setPage((p) => Math.max(1, p - 1))}
> >
<ChevronLeft className="mr-1 h-4 w-4" /> <ChevronLeft className="mr-1 h-4 w-4" />
Previous Previous
@ -650,7 +630,7 @@ export function PaymentsPage() {
size="sm" size="sm"
className="rounded-[6px]" className="rounded-[6px]"
disabled={!canNext || loading} disabled={!canNext || loading}
onClick={() => setOffset((o) => o + pageSize)} onClick={() => setPage((p) => p + 1)}
> >
Next Next
<ChevronRight className="ml-1 h-4 w-4" /> <ChevronRight className="ml-1 h-4 w-4" />