File size: 7,917 Bytes
e7abd9e |
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
import React, { useMemo, useEffect, useCallback } from "react";
import { Box, Typography } from "@mui/material";
import { useSearchParams } from "react-router-dom";
import { TABLE_DEFAULTS } from "./constants/defaults";
import { useLeaderboard } from "./context/LeaderboardContext";
import { useLeaderboardProcessing } from "./hooks/useLeaderboardData";
import { useLeaderboardData } from "./hooks/useLeaderboardData";
import LeaderboardTable from "./components/Table/Table";
import SearchBar, { SearchBarSkeleton } from "./components/Filters/SearchBar";
import PerformanceMonitor from "./components/PerformanceMonitor";
const Leaderboard = () => {
const { state, actions } = useLeaderboard();
const [searchParams, setSearchParams] = useSearchParams();
const {
data,
isLoading: dataLoading,
error: dataError,
} = useLeaderboardData();
const {
table,
filteredData,
error: processingError,
} = useLeaderboardProcessing();
// Memoize filtered data
const memoizedFilteredData = useMemo(() => filteredData, [filteredData]);
const memoizedTable = useMemo(() => table, [table]);
// Memoize table options
const hasTableOptionsChanges = useMemo(() => {
return (
state.display.rowSize !== TABLE_DEFAULTS.ROW_SIZE ||
JSON.stringify(state.display.scoreDisplay) !==
JSON.stringify(TABLE_DEFAULTS.SCORE_DISPLAY) ||
state.display.averageMode !== TABLE_DEFAULTS.AVERAGE_MODE ||
state.display.rankingMode !== TABLE_DEFAULTS.RANKING_MODE
);
}, [state.display]);
const hasColumnFilterChanges = useMemo(() => {
return (
JSON.stringify([...state.display.visibleColumns].sort()) !==
JSON.stringify([...TABLE_DEFAULTS.COLUMNS.DEFAULT_VISIBLE].sort())
);
}, [state.display.visibleColumns]);
// Memoize callbacks
const onToggleFilters = useCallback(() => {
actions.toggleFiltersExpanded();
}, [actions]);
const onColumnVisibilityChange = useCallback(
(newVisibility) => {
actions.setDisplayOption(
"visibleColumns",
Object.keys(newVisibility).filter((key) => newVisibility[key])
);
},
[actions]
);
const onRowSizeChange = useCallback(
(size) => {
actions.setDisplayOption("rowSize", size);
},
[actions]
);
const onScoreDisplayChange = useCallback(
(display) => {
actions.setDisplayOption("scoreDisplay", display);
},
[actions]
);
const onAverageModeChange = useCallback(
(mode) => {
actions.setDisplayOption("averageMode", mode);
},
[actions]
);
const onRankingModeChange = useCallback(
(mode) => {
actions.setDisplayOption("rankingMode", mode);
},
[actions]
);
const onPrecisionsChange = useCallback(
(precisions) => {
actions.setFilter("precisions", precisions);
},
[actions]
);
const onTypesChange = useCallback(
(types) => {
actions.setFilter("types", types);
},
[actions]
);
const onParamsRangeChange = useCallback(
(range) => {
actions.setFilter("paramsRange", range);
},
[actions]
);
const onBooleanFiltersChange = useCallback(
(filters) => {
actions.setFilter("booleanFilters", filters);
},
[actions]
);
const onReset = useCallback(() => {
actions.resetFilters();
}, [actions]);
// Memoize loading states
const loadingStates = useMemo(() => {
const isInitialLoading = dataLoading || !data;
const isProcessingData = !memoizedTable || !memoizedFilteredData;
const isApplyingFilters = state.models.length > 0 && !memoizedFilteredData;
const hasValidFilterCounts =
state.countsReady &&
state.filterCounts &&
state.filterCounts.normal &&
state.filterCounts.officialOnly;
return {
isInitialLoading,
isProcessingData,
isApplyingFilters,
showSearchSkeleton: isInitialLoading || !hasValidFilterCounts,
showFiltersSkeleton: isInitialLoading || !hasValidFilterCounts,
showTableSkeleton:
isInitialLoading ||
isProcessingData ||
isApplyingFilters ||
!hasValidFilterCounts,
};
}, [
dataLoading,
data,
memoizedTable,
memoizedFilteredData,
state.models.length,
state.filterCounts,
state.countsReady,
]);
// Memoize child components
const memoizedSearchBar = useMemo(
() => (
<SearchBar
onToggleFilters={onToggleFilters}
filtersOpen={state.filtersExpanded}
loading={loadingStates.showTableSkeleton}
data={memoizedFilteredData}
table={table}
/>
),
[
onToggleFilters,
state.filtersExpanded,
loadingStates.showTableSkeleton,
memoizedFilteredData,
table,
]
);
// No need to memoize LeaderboardTable as it handles its own sorting state
const tableComponent = (
<LeaderboardTable
table={table}
loading={loadingStates.showTableSkeleton}
onColumnVisibilityChange={onColumnVisibilityChange}
hasTableOptionsChanges={hasTableOptionsChanges}
hasColumnFilterChanges={hasColumnFilterChanges}
rowSize={state.display.rowSize}
onRowSizeChange={onRowSizeChange}
scoreDisplay={state.display.scoreDisplay}
onScoreDisplayChange={onScoreDisplayChange}
averageMode={state.display.averageMode}
onAverageModeChange={onAverageModeChange}
rankingMode={state.display.rankingMode}
onRankingModeChange={onRankingModeChange}
searchParams={searchParams}
setSearchParams={setSearchParams}
pinnedModels={state.pinnedModels}
/>
);
// Update context with loaded data
useEffect(() => {
if (data) {
actions.setModels(data);
}
}, [data, actions]);
// Log to understand loading state
useEffect(() => {
if (process.env.NODE_ENV === "development") {
console.log("Loading state:", {
dataLoading,
hasData: !!data,
hasTable: !!table,
hasFilteredData: !!filteredData,
filteredDataLength: filteredData?.length,
stateModelsLength: state.models.length,
hasFilters: Object.keys(state.filters).some((key) => {
if (Array.isArray(state.filters[key])) {
return state.filters[key].length > 0;
}
return !!state.filters[key];
}),
});
}
}, [
dataLoading,
data,
table,
filteredData?.length,
state.models.length,
filteredData,
state.filters,
]);
// If an error occurred, display it
if (dataError || processingError) {
return (
<Box sx={{ p: 3, textAlign: "center" }}>
<Typography color="error">
{(dataError || processingError)?.message ||
"An error occurred while loading the data"}
</Typography>
</Box>
);
}
return (
<Box
sx={{
width: "100%",
display: "flex",
flexDirection: "column",
}}
>
<PerformanceMonitor />
<Box
sx={{
display: "flex",
flexDirection: "column",
gap: 0,
alignItems: "center",
}}
>
<Box
sx={{
width: {
xs: "100%",
sm: "100%",
md: "80%",
},
maxWidth: "1200px",
}}
>
{loadingStates.showSearchSkeleton ? (
<SearchBarSkeleton />
) : (
memoizedSearchBar
)}
</Box>
<Box
sx={{
display: "flex",
alignItems: "flex-start",
justifyContent: "center",
width: "100%",
overflow: "hidden",
}}
>
<Box
sx={{
width: "100%",
px: 1,
}}
>
{tableComponent}
</Box>
</Box>
</Box>
</Box>
);
};
export default Leaderboard;
|