-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPartyProgress.tsx
212 lines (183 loc) · 7.3 KB
/
PartyProgress.tsx
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
import { useVirtualizer } from '@tanstack/react-virtual';
import { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { PartyEvent, usePartyEvents } from '@/api/party/events';
import { usePartyCodes, usePartyProgress } from '@/api/progress';
import { Tooltip } from '@/components/helpers/Tooltip';
import { ProgressCell } from './ProgressCell';
export const PartyProgress: FC<{ party_id: string }> = ({ party_id }) => {
const { data: orderedCodes } = usePartyCodes(party_id);
const { triedCodes, percentages } = usePartyProgress(party_id);
const codes = orderedCodes;
return (
<>
<PartyProgressList
party_id={party_id}
orderedCodes={orderedCodes}
triedCodes={triedCodes}
percentages={percentages}
codes={codes}
/>
</>
);
};
export const PartyProgressList: FC<{
party_id: string;
orderedCodes: string[];
triedCodes: Map<string, PartyEvent[]>;
percentages: Map<string, number>;
codes: string[];
}> = ({ party_id, orderedCodes, triedCodes, percentages, codes }) => {
const parentRef = useRef<HTMLDivElement>(null);
// Constants for item sizing
const ITEM_WIDTH = 64;
const ITEM_HEIGHT = 24;
const ITEM_GAP = 2;
const CELL_SIZE = ITEM_WIDTH + ITEM_GAP;
// State for container dimensions
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 });
// Calculate columns based on container width - memoize to prevent recalculation
const columnCount = useMemo(
() => Math.max(1, Math.floor(containerSize.width / CELL_SIZE)),
[containerSize.width, CELL_SIZE]
);
// Calculate the total number of rows needed - memoize to prevent recalculation
const rowCount = useMemo(
() => Math.ceil(codes.length / columnCount),
[codes.length, columnCount]
);
// Resize observer callback
const observerCallback = useCallback(() => {
if (parentRef.current) {
const width = parentRef.current.clientWidth;
const height = parentRef.current.clientHeight;
setContainerSize({ width, height });
}
}, []);
// Set up resize observer
useEffect(() => {
if (!parentRef.current) return;
observerCallback();
// Use ResizeObserver for more efficient resize detection
const resizeObserver = new ResizeObserver(observerCallback);
resizeObserver.observe(parentRef.current);
return () => {
if (parentRef.current) {
resizeObserver.unobserve(parentRef.current);
}
resizeObserver.disconnect();
};
}, [observerCallback]);
// Create a virtualizer for rows
const rowVirtualizer = useVirtualizer({
count: rowCount,
getScrollElement: () => parentRef.current,
estimateSize: () => ITEM_HEIGHT + ITEM_GAP,
overscan: 5,
});
// Create a virtualizer for columns
const columnVirtualizer = useVirtualizer({
count: columnCount,
getScrollElement: () => parentRef.current,
horizontal: true,
estimateSize: () => ITEM_WIDTH + ITEM_GAP, // Make sure this uses the updated ITEM_WIDTH
overscan: 5,
});
// Get cell index in the full list
const getCellIndex = useCallback(
(rowIndex: number, columnIndex: number) => {
return rowIndex * columnCount + columnIndex;
},
[columnCount]
);
const visibleCodes: Map<string, string[]> = new Map();
usePartyEvents(party_id, (event) => {
return event.data.type === 'PartyCursorUpdate';
}).events.forEach((event) => {
if (event.data.type === 'PartyCursorUpdate') {
const cursor = parseInt(event.data.cursor, 10);
const { size } = event.data;
const codes = orderedCodes.slice(cursor, cursor + size);
visibleCodes.set(event.user_id, codes);
}
});
// Memoize virtual cells to prevent unnecessary re-renders
const virtualCells = useMemo(() => {
return rowVirtualizer.getVirtualItems().flatMap(
(virtualRow) =>
columnVirtualizer
.getVirtualItems()
.map((virtualColumn) => {
const cellIndex = getCellIndex(virtualRow.index, virtualColumn.index);
// Skip rendering if cell index is out of bounds
if (cellIndex >= codes.length) return null;
const code = codes[cellIndex];
return {
key: `${virtualRow.index}:${virtualColumn.index}`,
rowStart: virtualRow.start,
columnStart: virtualColumn.start,
code,
cellIndex,
};
})
.filter(Boolean) // Filter out null entries
);
}, [
rowVirtualizer.getVirtualItems(),
columnVirtualizer.getVirtualItems(),
codes,
getCellIndex,
]);
return (
<div className="card w-full flex flex-col gap-2 text" style={{ gridColumn: '1 / -1' }}>
<div className="flex items-center justify-between">
<h3>Raid Progress</h3>
<Tooltip>
<p>Here you see the progress of the raid.</p>
<br />
<p>More info about your progress will be added in the future.</p>
</Tooltip>
</div>
<p className="text-secondary">
These are all the codes you have entered and you have left to explore.
</p>
<div
ref={parentRef}
className="max-h-[80vh] overflow-auto w-full"
style={{
height: '900px',
width: '100%',
}}
>
<div
style={{
height: `${rowVirtualizer.getTotalSize()}px`,
width: `${columnVirtualizer.getTotalSize()}px`,
position: 'relative',
}}
>
{virtualCells.map(
(cell) =>
cell && (
<div
key={cell.key}
className="absolute bg-secondary rounded-sm"
style={{
top: cell.rowStart,
left: cell.columnStart,
width: `${ITEM_WIDTH}px`, // This will now use the increased width
height: `${ITEM_HEIGHT}px`,
}}
>
<ProgressCell
visibleCodes={visibleCodes}
code={cell.code}
triedCodes={triedCodes}
/>
</div>
)
)}
</div>
</div>
</div>
);
};