forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculationSummary.tsx
More file actions
167 lines (155 loc) · 5.94 KB
/
Copy pathCalculationSummary.tsx
File metadata and controls
167 lines (155 loc) · 5.94 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
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { Receipt, CheckCircle } from 'lucide-react';
import type { Participant } from './SplitCalculator';
interface CalculationSummaryProps {
participants: Participant[];
subtotal: number;
currency: string;
rounding: 'none' | 'up' | 'down' | 'nearest';
}
const CURRENCY_SYMBOLS: Record<string, string> = {
USD: '$',
EUR: '€',
GBP: '£',
JPY: '¥',
XLM: 'XLM',
};
export function CalculationSummary({
participants,
subtotal,
currency,
rounding,
}: CalculationSummaryProps) {
const { t } = useTranslation();
const currencySymbol = CURRENCY_SYMBOLS[currency] || '$';
const roundedParticipants = useMemo(() => {
if (rounding === 'none') return participants;
return participants.map(p => ({
...p,
amount: applyRounding(p.amount, rounding),
}));
}, [participants, rounding]);
const applyRounding = (amount: number, method: string) => {
switch (method) {
case 'up':
return Math.ceil(amount * 100) / 100;
case 'down':
return Math.floor(amount * 100) / 100;
case 'nearest':
return Math.round(amount * 100) / 100;
default:
return amount;
}
};
const totalWithRounding = roundedParticipants.reduce((sum, p) => sum + p.amount, 0);
const roundingDifference = totalWithRounding - subtotal;
return (
<div className="mt-6 bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
<div className="flex items-center gap-3 mb-4">
<Receipt size={24} className="text-[var(--color-primary)]" aria-hidden="true" />
<h2 className="text-xl font-bold text-gray-900 dark:text-gray-100">
{t('calculator.summary')}
</h2>
</div>
{/* Summary Table */}
<div className="overflow-x-auto">
<table
className="w-full"
role="table"
aria-label={t('calculator.splitSummary')}
>
<thead>
<tr className="border-b border-gray-200 dark:border-gray-700">
<th className="text-left py-3 px-4 text-sm font-semibold text-gray-700 dark:text-gray-300">
{t('calculator.participant')}
</th>
<th className="text-right py-3 px-4 text-sm font-semibold text-gray-700 dark:text-gray-300">
{t('calculator.amount')}
</th>
{rounding !== 'none' && (
<th className="text-right py-3 px-4 text-sm font-semibold text-gray-700 dark:text-gray-300">
{t('calculator.rounded')}
</th>
)}
</tr>
</thead>
<tbody>
{roundedParticipants.map((participant) => (
<tr
key={participant.id}
className="border-b border-gray-100 dark:border-gray-700"
>
<td className="py-3 px-4">
<div className="flex items-center gap-3">
<div className="w-8 h-8 rounded-full bg-[var(--color-primary)]/20 text-[var(--color-primary)] flex items-center justify-center text-sm font-bold">
{participant.name.charAt(0).toUpperCase()}
</div>
<span className="font-medium text-gray-900 dark:text-gray-100">
{participant.name}
</span>
</div>
</td>
<td className="py-3 px-4 text-right text-gray-700 dark:text-gray-300">
{currencySymbol}{participant.amount.toFixed(2)}
</td>
{rounding !== 'none' && (
<td className="py-3 px-4 text-right">
<span className="text-gray-500 dark:text-gray-400 line-through">
{currencySymbol}{participant.amount.toFixed(2)}
</span>
</td>
)}
</tr>
))}
</tbody>
<tfoot>
<tr className="bg-gray-50 dark:bg-gray-700/50">
<td className="py-3 px-4 font-bold text-gray-900 dark:text-gray-100">
{t('calculator.total')}
</td>
<td className="py-3 px-4 text-right font-bold text-gray-900 dark:text-gray-100">
{currencySymbol}{subtotal.toFixed(2)}
</td>
{rounding !== 'none' && (
<td className="py-3 px-4 text-right font-bold text-[var(--color-primary)]">
{currencySymbol}{totalWithRounding.toFixed(2)}
</td>
)}
</tr>
</tfoot>
</table>
</div>
{/* Rounding Info */}
{rounding !== 'none' && Math.abs(roundingDifference) > 0.01 && (
<div className="mt-4 p-3 bg-amber-50 dark:bg-amber-900/20 rounded-lg">
<p className="text-sm text-amber-700 dark:text-amber-400">
{t('calculator.roundingNote')}: {roundingDifference > 0 ? '+' : '-'}
{currencySymbol}{Math.abs(roundingDifference).toFixed(2)}
</p>
</div>
)}
{/* Confirmation */}
<div className="mt-4 flex items-center gap-2 text-green-600 dark:text-green-400">
<CheckCircle size={20} aria-hidden="true" />
<span className="text-sm font-medium">
{t('calculator.calculationComplete')}
</span>
</div>
{/* Export Actions */}
<div className="mt-6 flex flex-wrap gap-3">
<button
onClick={() => {
const text = roundedParticipants
.map(p => `${p.name}: ${currencySymbol}${p.amount.toFixed(2)}`)
.join('\n');
navigator.clipboard.writeText(text);
}}
className="px-4 py-2 text-sm font-medium text-[var(--color-primary)] border border-[var(--color-primary)] rounded-lg hover:bg-[var(--color-primary)]/10 transition-colors"
>
{t('calculator.copyToClipboard')}
</button>
</div>
</div>
);
}