Skip to content

Commit b11817a

Browse files
committed
maintain memoized uniqueMatches for carousel
1 parent 87c99bf commit b11817a

1 file changed

Lines changed: 34 additions & 16 deletions

File tree

app/components/Dashboard.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ const cleanTeamName = (teamName) => {
2020
return teamName.replace(/\s*\([MmWw]\)\s*$/, '').trim()
2121
}
2222

23+
const getUniqueMatches = (matches, cleanTeamName) => {
24+
const uniqueKeys = new Set()
25+
return matches.filter((match) => {
26+
const cleanedOpponentTeam = cleanTeamName(match.teams.opponentTeam)
27+
let matchKey = `${match.matchDate}#${cleanedOpponentTeam}`
28+
if (!match.matchDetails.duel) matchKey = `_#${match.matchDetails.event}`
29+
30+
if (uniqueKeys.has(matchKey)) return false
31+
uniqueKeys.add(matchKey)
32+
return true
33+
})
34+
}
35+
2336
const formatMatches = (matches) => {
2437
return matches
2538
.filter((match) => match.version === 'v1')
@@ -223,6 +236,11 @@ const Dashboard = () => {
223236
return formatted
224237
}, [matches, isLoaded])
225238

239+
const uniqueMatches = useMemo(
240+
() => getUniqueMatches(formattedMatches, cleanTeamName),
241+
[formattedMatches]
242+
)
243+
226244
// Mobile detection useEffect
227245
useEffect(() => {
228246
const checkIfMobile = () => setIsMobile(window.innerWidth < 400)
@@ -324,29 +342,29 @@ const Dashboard = () => {
324342

325343
<div className={styles.carousel}>
326344
{!formattedMatches.length
327-
? // Placeholder skeletons for carousel items
328-
Array(10)
345+
? Array(10)
329346
.fill(0)
330347
.map((_, i) => (
331348
<div
332349
key={i}
333350
className={`${styles.card} ${styles.placeholderCard}`}
334351
/>
335352
))
336-
: // Map over formattedMatches to render each CarouselItem
337-
formattedMatches.map((match, index) => (
338-
<CarouselItem
339-
key={index}
340-
match={match}
341-
logo={logos[match.teams.opponentTeam]}
342-
isSelected={selectedMatchSets.includes(
343-
match.matchDetails.duel
344-
? `${match.matchDate}#${match.teams.opponentTeam}`
345-
: `_#${match.matchDetails.event}`
346-
)}
347-
onClick={handleCarouselClick}
348-
/>
349-
))}
353+
: uniqueMatches.map((match) => {
354+
const matchKey = match.matchDetails.duel
355+
? `${match.matchDate}#${match.teams.opponentTeam}`
356+
: `_#${match.matchDetails.event}`
357+
358+
return (
359+
<CarouselItem
360+
key={matchKey}
361+
match={match}
362+
logo={logos[match.teams.opponentTeam]}
363+
isSelected={selectedMatchSets.includes(matchKey)}
364+
onClick={handleCarouselClick}
365+
/>
366+
)
367+
})}
350368
</div>
351369

352370
<div className={styles.mainContent}>

0 commit comments

Comments
 (0)