-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathFreezeButton.tsx
More file actions
91 lines (84 loc) · 2.46 KB
/
Copy pathFreezeButton.tsx
File metadata and controls
91 lines (84 loc) · 2.46 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
import { useState, useCallback } from 'react';
import { Snowflake } from 'lucide-react';
import './FreezeButton.css';
interface FreezeButtonProps {
lineId: string;
lineName: string;
frozen: boolean;
onFreeze: (lineId: string) => void;
onUnfreeze: (lineId: string) => void;
className?: string;
}
export function FreezeButton({
lineId,
lineName,
frozen,
onFreeze,
onUnfreeze,
className = '',
}: FreezeButtonProps) {
const [confirming, setConfirming] = useState(false);
const label = frozen ? 'Unfreeze' : 'Freeze';
const confirmLabel = frozen
? `Unfreeze ${lineName}?`
: `Freeze ${lineName}?`;
const confirmDescription = frozen
? 'This credit line will be reactivated. Draws and payments will resume.'
: 'Draws will be blocked. Interest will continue to accrue. You can unfreeze at any time.';
const handleConfirm = useCallback(() => {
setConfirming(false);
if (frozen) {
onUnfreeze(lineId);
} else {
onFreeze(lineId);
}
}, [frozen, lineId, onFreeze, onUnfreeze]);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Escape') {
setConfirming(false);
}
},
[],
);
if (confirming) {
return (
<div
className="freeze-confirm"
role="dialog"
aria-label={`Confirm ${label.toLowerCase()} for ${lineName}`}
onKeyDown={handleKeyDown}
>
<p className="freeze-confirm__title">{confirmLabel}</p>
<p className="freeze-confirm__desc">{confirmDescription}</p>
<div className="freeze-confirm__actions">
<button
onClick={() => setConfirming(false)}
className="freeze-btn freeze-btn--cancel"
aria-label={`Cancel ${label.toLowerCase()}`}
>
Cancel
</button>
<button
onClick={handleConfirm}
className={`freeze-btn freeze-btn--confirm ${frozen ? 'freeze-btn--unfreeze' : 'freeze-btn--freeze'}`}
aria-label={`Confirm ${label.toLowerCase()}`}
>
{label}
</button>
</div>
</div>
);
}
return (
<button
onClick={() => setConfirming(true)}
className={`freeze-btn freeze-btn--trigger ${frozen ? 'freeze-btn--unfreeze' : 'freeze-btn--freeze'} ${className}`}
aria-label={`${label} ${lineName}`}
aria-haspopup="dialog"
>
<Snowflake className="freeze-btn__icon" aria-hidden="true" />
{label}
</button>
);
}