Skip to content

Commit e0d1bed

Browse files
committed
chore: wip
1 parent 2c1c543 commit e0d1bed

File tree

1 file changed

+74
-61
lines changed

1 file changed

+74
-61
lines changed

src/features/transactions/pages/group-page.tsx

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ type AccountFoo = {
88
status: 'from' | 'to' | 'middle' | 'outside'
99
}
1010

11-
type TransactionTrProps = {
11+
type TransactionFooDrawing = {
12+
from: number
13+
to: number
14+
direction: 'leftToRight' | 'rightToLeft' | 'toSelf'
15+
}
16+
17+
type TransactionTableRowProps = {
1218
transaction: Transaction
1319
cellHeight?: number
1420
lineWidth?: number
@@ -19,43 +25,21 @@ type TransactionTrProps = {
1925
indentLevel?: number
2026
verticalBars?: number[]
2127
}
22-
function TransactionTr({
28+
function TransactionTableRow({
2329
transaction,
2430
accounts,
2531
hasParent = false,
2632
hasNextSibbling = false,
2733
hasChildren = false,
2834
indentLevel = 0,
2935
verticalBars,
30-
}: TransactionTrProps) {
31-
const accountsWithStatus = useMemo(() => {
32-
let a = false
33-
return accounts.map<AccountFoo>((account) => {
34-
if (transaction.sender === account) {
35-
a = !a
36-
return {
37-
account: account,
38-
status: 'from',
39-
}
40-
}
41-
if (transaction.receiver === account) {
42-
a = !a
43-
return {
44-
account: account,
45-
status: 'to',
46-
}
47-
}
48-
return {
49-
account: account,
50-
status: a ? 'middle' : 'outside',
51-
}
52-
})
53-
}, [accounts, transaction.receiver, transaction.sender])
36+
}: TransactionTableRowProps) {
37+
const foo = useMemo(() => calcTransactionFoo(transaction, accounts), [accounts, transaction])
5438

5539
return (
5640
<>
5741
<tr>
58-
<td className={cn('p-0 relative')}>
42+
<td className={cn('p-0 relative -z-10')}>
5943
{verticalBars &&
6044
verticalBars.length &&
6145
verticalBars
@@ -76,32 +60,27 @@ function TransactionTr({
7660
)}
7761
</div>
7862
</td>
79-
{accountsWithStatus.map((account, index) => (
80-
<td key={index} className={cn('p-0 relative')}>
81-
{/* TODO: fix the calc, it doesn't look good */}
82-
<div className={cn('h-10 border-l-2 border-muted border-dashed absolute left-[calc(50%-1px)] -z-10')}></div>
83-
{account.status === 'from' && (
84-
<div className={cn('h-10 absolute left-[50%] translate-x-[-50%] translate-y-[-25%] z-10')}>
85-
<SvgCircle />
86-
</div>
87-
)}
88-
{account.status === 'from' && <div className={cn('border-primary w-[50%] border-b-2 translate-x-[100%]')}></div>}
89-
90-
<div className={cn('flex justify-center items-center relative')}>
91-
{account.status === 'to' && (
92-
<div>
93-
<SvgCircle />
63+
{accounts.map((account, index) => {
64+
if (index < foo.from || index > foo.to) return <td key={index}></td>
65+
if (index === foo.from)
66+
return (
67+
<td key={index} colSpan={foo.to - foo.from + 1}>
68+
<div className={cn('flex items-center justify-center bg-red-200')}>
69+
<SvgCircle width={20} height={20}></SvgCircle>
70+
<div
71+
className={cn('border-primary border-b-2')}
72+
style={{ width: `calc(${(100 - 100 / (foo.to - foo.from + 1)).toFixed(2)}% - 20px)` }}
73+
></div>
74+
<SvgCircle width={20} height={20}></SvgCircle>
9475
</div>
95-
)}
96-
{account.status === 'middle' && <div className={cn('border-primary w-full border-b-2')}></div>}
97-
</div>
98-
</td>
99-
))}
76+
</td>
77+
)
78+
else return null
79+
})}
10080
</tr>
101-
10281
{hasChildren &&
10382
transaction.transactions?.map((childTransaction, index, arr) => (
104-
<TransactionTr
83+
<TransactionTableRow
10584
transaction={childTransaction}
10685
hasChildren={childTransaction.transactions && childTransaction.transactions.length > 0}
10786
hasParent={true}
@@ -168,7 +147,7 @@ export function GroupPage() {
168147
],
169148
}
170149
const accounts = extractSendersAndReceivers(group)
171-
150+
const allTransactionCounts = 15
172151
return (
173152
<table className={cn('w-full')}>
174153
<tr>
@@ -177,21 +156,43 @@ export function GroupPage() {
177156
<th key={index}>{account}</th>
178157
))}
179158
</tr>
180-
{group.transactions?.map((transaction, index, arr) => (
181-
<TransactionTr
182-
transaction={transaction}
183-
hasChildren={transaction.transactions && transaction.transactions.length > 0}
184-
hasParent={false}
185-
hasNextSibbling={index < arr.length - 1}
186-
accounts={accounts}
187-
/>
188-
))}
159+
<tbody style={{ height: `${allTransactionCounts * 40}px` }}>
160+
<tr>
161+
<td></td>
162+
<td rowSpan={allTransactionCounts} colSpan={accounts.length} className={cn('p-0')}>
163+
<div
164+
className={cn('grid h-full')}
165+
style={{
166+
gridTemplateColumns: `repeat(${accounts.length}, minmax(0, 1fr))`,
167+
height: `${allTransactionCounts * 40}px`,
168+
}}
169+
>
170+
{accounts.map((account, index) => (
171+
<div key={index} className={cn('flex justify-center')}>
172+
<div className={cn('border-muted border-l-2 h-full border-dashed')}></div>
173+
</div>
174+
))}
175+
</div>
176+
</td>
177+
</tr>
178+
</tbody>
179+
<tbody className="relative" style={{ top: `-${15 * 40}px` }}>
180+
{group.transactions.map((transaction, index, arr) => (
181+
<TransactionTableRow
182+
transaction={transaction}
183+
hasChildren={transaction.transactions && transaction.transactions.length > 0}
184+
hasParent={false}
185+
hasNextSibbling={index < arr.length - 1}
186+
accounts={accounts}
187+
/>
188+
))}
189+
</tbody>
189190
</table>
190191
)
191192
}
192193

193194
export type Group = {
194-
transactions?: Transaction[]
195+
transactions: Transaction[]
195196
}
196197

197198
export type Transaction = {
@@ -204,7 +205,7 @@ export type Transaction = {
204205
function extractSendersAndReceivers(group: Group): string[] {
205206
let sendersAndReceivers: string[] = []
206207

207-
function extract(transactionArr: Transaction[] | undefined) {
208+
function extract(transactionArr: Transaction[]) {
208209
if (transactionArr) {
209210
transactionArr.forEach((transaction) => {
210211
sendersAndReceivers.push(transaction.sender)
@@ -223,3 +224,15 @@ function extractSendersAndReceivers(group: Group): string[] {
223224

224225
return sendersAndReceivers
225226
}
227+
228+
function calcTransactionFoo(transaction: Transaction, accounts: string[]): TransactionFooDrawing {
229+
const fromAccount = accounts.findIndex((a) => transaction.sender === a)
230+
const toAccount = accounts.findIndex((a) => transaction.receiver === a)
231+
const direction = fromAccount < toAccount ? 'leftToRight' : fromAccount > toAccount ? 'rightToLeft' : 'toSelf'
232+
233+
return {
234+
from: Math.min(fromAccount, toAccount),
235+
to: Math.max(fromAccount, toAccount),
236+
direction: direction,
237+
}
238+
}

0 commit comments

Comments
 (0)