forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickRepayTrigger.tsx
More file actions
64 lines (58 loc) · 1.93 KB
/
Copy pathQuickRepayTrigger.tsx
File metadata and controls
64 lines (58 loc) · 1.93 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
import React, { useState, useRef } from 'react';
import { useWallet } from '../context/WalletContext';
import { MOCK_CREDIT_LINES } from '../data/mockData';
import { QuickRepayModal } from './QuickRepayModal';
export const QuickRepayTrigger = () => {
const [isOpen, setIsOpen] = useState(false);
const triggerRef = useRef<HTMLButtonElement>(null);
const { balances } = useWallet();
const walletBalance = balances?.reduce((acc, b) => acc + Number(b.balance), 0) || 0;
const hasOpenLine = MOCK_CREDIT_LINES.some(cl => cl.status !== 'Closed');
if (!hasOpenLine) return null;
const linesWithBalance = MOCK_CREDIT_LINES.filter(cl => cl.utilized > 0);
const mostRecentLine = linesWithBalance.sort((a, b) =>
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
)[0];
if (!mostRecentLine) return null;
return (
<>
<button
ref={triggerRef}
type="button"
className="header-nav-link"
style={{
height: 44,
padding: '0 1rem',
display: 'flex',
alignItems: 'center',
background: 'rgba(63,185,80,0.1)',
color: '#3fb950',
borderRadius: 6,
fontWeight: 600,
border: '1px solid rgba(63,185,80,0.3)',
cursor: 'pointer',
transition: 'all 0.2s',
}}
onClick={() => setIsOpen(true)}
onMouseEnter={(e) => {
e.currentTarget.style.background = 'rgba(63,185,80,0.2)';
}}
onMouseLeave={(e) => {
e.currentTarget.style.background = 'rgba(63,185,80,0.1)';
}}
>
Quick Repay
</button>
{isOpen && (
<QuickRepayModal
creditLine={mostRecentLine}
walletBalance={walletBalance}
initialAmount={mostRecentLine.utilized.toString()}
onClose={() => setIsOpen(false)}
onSuccess={() => setIsOpen(false)}
triggerRef={triggerRef}
/>
)}
</>
);
};