From 9f0c0d2215d74415eb5335d08114cc49bc296427 Mon Sep 17 00:00:00 2001 From: vrajasekhar1 Date: Fri, 28 Nov 2025 13:00:42 +0530 Subject: [PATCH 1/6] Refactor transaction details page by creating components and utility functions --- src/app/history/[id]/page.tsx | 160 +++++--------------------- src/components/AuditLogPreview.tsx | 54 +++++++++ src/components/CommentEditor.tsx | 52 +++++++++ src/components/TxDetailsBreakdown.tsx | 62 ++++++++++ src/components/TxHeaderCard.tsx | 73 ++++++++++++ src/lib.ts | 34 ++++++ 6 files changed, 306 insertions(+), 129 deletions(-) create mode 100644 src/components/AuditLogPreview.tsx create mode 100644 src/components/CommentEditor.tsx create mode 100644 src/components/TxDetailsBreakdown.tsx create mode 100644 src/components/TxHeaderCard.tsx diff --git a/src/app/history/[id]/page.tsx b/src/app/history/[id]/page.tsx index fe48f30..77c2d76 100644 --- a/src/app/history/[id]/page.tsx +++ b/src/app/history/[id]/page.tsx @@ -2,9 +2,13 @@ import React, { useState, useEffect, useRef, useContext } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Modal from '@/components/Modal'; +import TxHeaderCard from '@/components/TxHeaderCard'; +import TxDetailsBreakdown from '@/components/TxDetailsBreakdown'; +import AuditLogPreview from '@/components/AuditLogPreview'; +import CommentEditor from '@/components/CommentEditor'; import { AppContext } from '@/context/AppContextProvider'; import { fetchSingleUserOrder, updateOrderStatus, proposeDispute, acceptDispute } from '@/services/orderApi'; -import { mapOrdersToTxItems, TxItem, TxStatus, statusClasses, statusLabel, mapCommentsToFrontend, mapCommentToFrontend } from '@/lib'; +import { mapOrdersToTxItems, TxItem, TxStatus, statusClasses, statusLabel, mapCommentsToFrontend, mapCommentToFrontend, fmt, deriveBreakdown } from '@/lib'; import { addComment } from '@/services/commentApi'; import { payWithPi } from '@/config/payment'; import { toast } from 'react-toastify'; @@ -77,38 +81,6 @@ export default function TxDetailsPage() { } }, [tx?.status]); - // Helpers to display the popup breakdown like on the mock - const fmt = (n: number) => { - if (!Number.isFinite(n)) return ''; - let s = n.toFixed(5); - if (s.includes('.')) { - s = s.replace(/0+$/g, '').replace(/\.$/, ''); - } - return s; - }; - - // Given total (top figure), derive base and fees using the same policy as the homepage - const deriveBreakdown = (total: number) => { - // Known constants - const network = 0.02; - // Find base such that total ≈ base + stake + network + escrow - // stake = max(0.1*base, 1) - // escrow = max(0.01*base, 0.1) - let lo = 0, hi = Math.max(total, 1); - for (let i = 0; i < 50; i++) { - const mid = (lo + hi) / 2; - const stake = Math.max(0.1 * mid, 1); - const escrow = Math.max(0.01 * mid, 0.1); - const t = mid + stake + network + escrow; - if (t > total) hi = mid; else lo = mid; - } - const base = lo; - const completionStake = Math.max(0.1 * base, 1); - const escrowFee = Math.max(0.01 * base, 0.1); - const recomputedTotal = base + completionStake + network + escrowFee; - return { base, completionStake, networkFees: network, escrowFee, total: recomputedTotal }; - }; - useEffect(() => { const loadOrder = async () => { // await a2uTrigger() @@ -280,34 +252,18 @@ export default function TxDetailsPage() { return (
-
- -

- EscrowPi Transaction -

- -
+ router.push('/history')} + onRefresh={() => { + if (!isRefreshing) { + setIsRefreshing(true); + setRefreshTick((t) => t + 1); + } + }} + />
@@ -634,83 +590,29 @@ export default function TxDetailsPage() { )}
-
- - EscrowPi Transaction Details - - - - -
- {(() => { const b = deriveBreakdown(tx.amount); const refundNote = tx.myRole === 'payer' ? '(refunded to you at end)' : '(refunded to the payer at end)'; const getLabel = tx.myRole === 'payee' ? 'You get:' : 'Payee gets:'; return ( -
-
{getLabel}{fmt(b.base)} pi
-
Transaction completion stake:{fmt(b.completionStake)} pi
-
{refundNote}
-
Pi Network gas fees:{fmt(b.networkFees)} pi
-
EscrowPi fee:{fmt(b.escrowFee)} pi
-
Total:{fmt(b.total)} pi
-
- ); })()} -
-
+ {/* Audit Log (outside details) */}
-
-
Audit Log
-
setShowComments(true)} - > - {comments.length === 0 ? ( -
No comments yet.
- ) : ( -
- {comments.map((c, idx) => ( -
-
- {c.author} - {new Date(c.ts).toLocaleString()} -
-
{c.text}
-
- ))} -
- )} -
-
+ setShowComments(true)} + /> {/* Add New Comment (outside details) - remains visible, but disabled for terminal states */} {(() => { // Disputed is not terminal for UC9 const isTerminal = tx.status === 'cancelled' || tx.status === 'declined' || tx.status === 'released'; return ( -
-
Add New Comment
-