/* eslint-disable react/no-unstable-nested-components */ import { useLocaleContext } from '@arcblock/ux/lib/Locale/context'; import type { Paginated, TPaymentIntentExpanded } from '@blocklet/payment-types'; import { Box, Button, CircularProgress, Stack, Typography } from '@mui/material'; import { useInfiniteScroll } from 'ahooks'; import TxLink from '../../components/blockchain/tx'; import Status from '../../components/status'; import api from '../../libs/api'; import { formatBNStr, formatToDate, getPaymentIntentStatusColor } from '../../libs/util'; const groupByDate = (items: TPaymentIntentExpanded[]) => { const grouped: { [key: string]: TPaymentIntentExpanded[] } = {}; items.forEach((item) => { const date = new Date(item.created_at).toLocaleDateString(); if (!grouped[date]) { grouped[date] = []; } grouped[date]?.push(item); }); return grouped; }; const fetchData = (params: Record = {}): Promise> => { const search = new URLSearchParams(); Object.keys(params).forEach((key) => { search.set(key, String(params[key])); }); return api.get(`/api/payment-intents?${search.toString()}`).then((res: any) => res.data); }; type Props = { customer_id: string; }; const pageSize = 10; export default function CustomerPaymentList({ customer_id }: Props) { const { t } = useLocaleContext(); const { data, loadMore, loadingMore, loading } = useInfiniteScroll>( (d) => { const page = d ? Math.ceil(d.list.length / pageSize) + 1 : 1; return fetchData({ page, pageSize, customer_id }); }, { reloadDeps: [customer_id], } ); if (loading || !data) { return ; } if (data && data.list.length === 0) { return ( {t('payment.customer.payment.empty')} ); } const hasMore = data && data.list.length < data.count; const grouped = groupByDate(data.list); return ( {Object.entries(grouped).map(([date, payments]) => ( {date} {payments.map((item: TPaymentIntentExpanded) => ( {formatToDate(item.created_at)} {formatBNStr(item.amount_received, item.paymentCurrency.decimal)}  {item.paymentCurrency.symbol} {item.description || '-'} {(item.payment_details?.arcblock?.tx_hash || item.payment_details?.ethereum?.tx_hash || item.payment_details?.base?.tx_hash) && ( )} ))} ))} {hasMore && ( )} {!hasMore && data.count > pageSize && ( {t('common.noMore', { resource: t('payment.customer.payments') })} )} ); }