forked from BETAIL-BOYS/TradeFlow-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
276 lines (252 loc) · 10.2 KB
/
Copy pathpage.tsx
File metadata and controls
276 lines (252 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"use client";
import React, { useState, useEffect } from "react";
import { Server } from "soroban-client";
import StickyHeader from "../../../components/StickyHeader";
import FractionalPurchaseModal, {
type Invoice,
} from "../../../components/FractionalPurchaseModal";
import DynamicRiskAssessmentChart from "../../../components/DynamicRiskAssessmentChart";
import RepayInvoiceButton from "../../../components/RepayInvoiceButton";
import { useTokenStore } from "../../../stores/tokenStore";
import { useTxWithToast } from "../../../hooks/useTxWithToast";
import { ArrowLeft, ExternalLink, Shield, TrendingUp } from "lucide-react";
import Link from "next/link";
import Icon from "../../../components/ui/Icon";
// Stellar testnet USDC issuer
const USDC_CODE = "USDC";
const USDC_ISSUER = "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5";
const server = new Server("https://soroban-testnet.stellar.org", {
allowHttp: true,
});
// Static fallback data used while on-chain data loads or if the contract is
// not yet deployed on testnet.
const INVOICE_DATA: Invoice = {
id: "INV-00123",
faceValue: 50000,
apy: 8.5,
dueDate: "2026-12-15",
currency: "USDC",
};
const MOCK_RISK_DATA = {
creditScore: 85,
paymentHistory: 92,
marketSectorRisk: 78,
collateralRatio: 75,
liquidityScore: 90,
debtToIncomeRatio: 68,
};
export default function InvoiceDetailPage() {
const { publicKey, isConnected } = useTokenStore();
const [showBuyModal, setShowBuyModal] = useState(false);
const [usdcBalance, setUsdcBalance] = useState("0");
const { executeTx } = useTxWithToast();
// Fetch live USDC balance from Stellar network whenever wallet connects
useEffect(() => {
if (!isConnected || !publicKey) {
setUsdcBalance("0");
return;
}
const fetchBalance = async () => {
try {
const accountResponse = await server
.accounts()
.accountId(publicKey)
.call();
const usdcEntry = accountResponse.balances.find(
(b: { asset_code?: string; asset_issuer?: string }) =>
b.asset_code === USDC_CODE && b.asset_issuer === USDC_ISSUER
);
setUsdcBalance(usdcEntry ? usdcEntry.balance : "0");
} catch {
setUsdcBalance("0");
}
};
fetchBalance();
}, [isConnected, publicKey]);
// Wrapping the Soroban call with executeTx ensures any Freighter error
// (user rejection, network failure, contract error) fires the correct toast.
const handleBuyFraction = async (
amountStroops: string,
invoiceId: string
): Promise<void> => {
await executeTx(async () => {
// TODO: Replace the lines below with your real Soroban client call, e.g.:
// await sorobanClient.buy_fraction({
// invoice_id: invoiceId,
// amount: BigInt(amountStroops),
// });
console.log("buy_fraction called:", { invoiceId, amountStroops });
await new Promise((r) => setTimeout(r, 1500));
});
};
return (
<div className="min-h-screen bg-tradeflow-dark text-white font-sans">
<StickyHeader
title="INV-123"
subtitle="Real World Asset token details and performance metrics"
actions={
<div className="flex items-center gap-3">
<Link
href="/marketplace"
className="flex items-center gap-2 px-4 py-2 bg-slate-700 hover:bg-slate-600 rounded-lg transition-colors"
>
<Icon icon={ArrowLeft} dense />
Back
</Link>
<button
onClick={() => setShowBuyModal(true)}
className="flex items-center gap-2 px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg transition-colors"
>
<Icon icon={ExternalLink} dense />
Trade
</button>
</div>
}
/>
<div className="px-4 lg:px-8 py-6">
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* Left Column */}
<div className="lg:col-span-2 space-y-6">
{/* Invoice Overview */}
<div className="bg-slate-800/50 rounded-xl border border-slate-700/50 p-6">
<h2 className="text-xl font-semibold mb-4">Invoice Overview</h2>
{loading && (
<p className="text-slate-400 text-sm animate-pulse">
Loading on-chain data…
</p>
)}
{error && (
<p className="text-red-400 text-sm bg-red-400/10 px-3 py-2 rounded-lg mb-4">
Could not load contract data: {error}
</p>
)}
<div className="grid grid-cols-2 gap-4">
<div>
<p className="text-slate-400 text-sm mb-1">Invoice ID</p>
<p className="font-mono text-blue-300">
{invoice?.id ?? "INV-00123"}
</p>
</div>
<div>
<p className="text-slate-400 text-sm mb-1">Token ID</p>
<p className="font-mono text-green-300">TKN-0x1234…5678</p>
</div>
<div>
<p className="text-slate-400 text-sm mb-1">Principal Amount</p>
<p className="text-xl font-bold">
{invoice
? `$${(Number(invoice.amount) / 10_000_000).toLocaleString()}`
: "$50,000"}
</p>
</div>
<div>
<p className="text-slate-400 text-sm mb-1">Recipient</p>
<p className="font-mono text-sm truncate text-slate-300">
{invoice?.recipient ?? "—"}
</p>
</div>
<div>
<p className="text-slate-400 text-sm mb-1">Maturity Date</p>
<p className="font-medium">Dec 15, 2026</p>
</div>
<div>
<p className="text-slate-400 text-sm mb-1">Status</p>
<span
className={`px-3 py-1 rounded-full text-sm font-medium ${
invoice?.status === "past_due"
? "bg-red-600/20 text-red-400"
: "bg-green-600/20 text-green-400"
}`}
>
{invoice?.status === "past_due" ? "Past Due" : invoice?.status ?? "Active"}
</span>
</div>
</div>
</div>
{/* Performance Chart */}
<div className="bg-slate-800/50 rounded-xl border border-slate-700/50 p-6">
<div className="flex items-center justify-between mb-4">
<h2 className="text-xl font-semibold">Performance</h2>
<div className="flex items-center gap-2 text-green-400">
<Icon icon={TrendingUp} />
<span className="font-medium">+12.5%</span>
</div>
</div>
<div className="h-64 bg-slate-700/30 rounded-lg flex items-center justify-center">
<p className="text-slate-400">Chart visualization would go here</p>
</div>
</div>
{/* Transaction History */}
<div className="bg-slate-800/50 rounded-xl border border-slate-700/50 p-6">
<h2 className="text-xl font-semibold mb-4">Transaction History</h2>
<div className="space-y-3">
{[
{ date: "2024-01-15", type: "Payment", amount: "$2,125", status: "Completed" },
{ date: "2024-01-01", type: "Payment", amount: "$2,125", status: "Completed" },
{ date: "2023-12-15", type: "Initial", amount: "$50,000", status: "Completed" },
].map((tx, index) => (
<div
key={index}
className="flex items-center justify-between p-3 bg-slate-700/30 rounded-lg"
>
<div>
<p className="font-medium">{tx.type}</p>
<p className="text-slate-400 text-sm">{tx.date}</p>
</div>
<div className="text-right">
<p className="font-medium">{tx.amount}</p>
<p className="text-green-400 text-sm">{tx.status}</p>
</div>
</div>
))}
</div>
</div>
</div>
{/* Sidebar */}
<div className="space-y-6">
<DynamicRiskAssessmentChart data={MOCK_RISK_DATA} />
{/* Quick Actions */}
<div className="bg-slate-800/50 rounded-xl border border-slate-700/50 p-6">
<h2 className="text-xl font-semibold mb-4">Quick Actions</h2>
<div className="space-y-3">
<button
onClick={() => setShowBuyModal(true)}
className="w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 rounded-lg transition-colors"
>
Buy Fraction
</button>
{/* Issue #194: Show Repay Loan CTA only to the original issuer */}
{isConnected && isIssuer && publicKey && (
<RepayInvoiceButton
invoiceId={invoice?.id ?? "INV-00123"}
callerPublicKey={publicKey}
totalDue={totalDue}
onSuccess={() => {
// Optionally refresh invoice state after repayment
}}
/>
)}
<button className="w-full px-4 py-2 bg-slate-700 hover:bg-slate-600 rounded-lg transition-colors">
View Documents
</button>
<button className="w-full px-4 py-2 bg-slate-700 hover:bg-slate-600 rounded-lg transition-colors">
Download Report
</button>
</div>
</div>
</div>
</div>
{showBuyModal && (
<FractionalPurchaseModal
invoice={INVOICE_DATA}
walletBalance={usdcBalance}
onClose={() => setShowBuyModal(false)}
onBuyFraction={handleBuyFraction}
/>
)}
</div>
</div>
);
}
// Inconsequential change for repo health
// Maintenance: minor update