Skip to content

Commit 41b38a7

Browse files
refactor: enhance sparkle overlay logic with unique index handling, increased chance for randomisation
1 parent adbedec commit 41b38a7

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

client/src/components/ui/eventHighlightCard.tsx

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Play } from "lucide-react";
22
import Image from "next/image";
3+
import { useState } from "react";
34

45
export type eventHighlightCardImage = {
56
url: string;
@@ -17,6 +18,11 @@ export type eventHighlightCardType = {
1718
row: number;
1819
};
1920

21+
export type sparkleIndexOverlay = {
22+
card: eventHighlightCardType;
23+
indexes: number[];
24+
};
25+
2026
const sparkleImages = ["/sparkle_1.png", "/sparkle_2.png", "/sparkle_3.png"];
2127

2228
// Purple card header section.
@@ -50,25 +56,13 @@ const renderCardHeader = (card: eventHighlightCardType) => {
5056
);
5157
};
5258

53-
// helper to ensure sparkles are different
54-
function getTwoUniqueIndexes(sparkleImages: string[]): [number, number] {
55-
const first = Math.floor(Math.random() * sparkleImages.length);
56-
let second = Math.floor(Math.random() * sparkleImages.length);
57-
58-
if (second === first) {
59-
second = (first + 1) % 3;
60-
}
61-
return [first, second];
62-
}
63-
6459
// only render sparkles on specific cards
65-
export function renderSparkleOverlay(card: eventHighlightCardType) {
66-
const [i1, i2] = getTwoUniqueIndexes(sparkleImages);
67-
switch (card.id) {
60+
const renderSparkleOverlay = (sparkle: sparkleIndexOverlay) => {
61+
switch (sparkle.card.id) {
6862
case 2:
6963
return (
7064
<Image
71-
src={sparkleImages[i1]}
65+
src={sparkleImages[sparkle.indexes[0]]}
7266
width={15}
7367
height={17}
7468
alt="sparkle"
@@ -78,7 +72,7 @@ export function renderSparkleOverlay(card: eventHighlightCardType) {
7872
case 3:
7973
return (
8074
<Image
81-
src={sparkleImages[i2]}
75+
src={sparkleImages[sparkle.indexes[0]]}
8276
width={15}
8377
height={17}
8478
alt="sparkle"
@@ -88,7 +82,7 @@ export function renderSparkleOverlay(card: eventHighlightCardType) {
8882
default:
8983
return null;
9084
}
91-
}
85+
};
9286

9387
export function EventHighlightCard({
9488
id,
@@ -98,6 +92,16 @@ export function EventHighlightCard({
9892
image,
9993
row,
10094
}: eventHighlightCardType) {
95+
const [indexes] = useState(() => {
96+
const first = Math.floor(Math.random() * sparkleImages.length);
97+
let second = Math.floor(Math.random() * sparkleImages.length);
98+
99+
if (second === first) {
100+
second = (first + 1) % sparkleImages.length;
101+
}
102+
return [first, second];
103+
});
104+
101105
return (
102106
<div key={id} className="flex flex-col">
103107
{renderCardHeader({ id, title, description, type, image, row })}
@@ -122,8 +126,13 @@ export function EventHighlightCard({
122126
/>
123127
)}
124128
</div>
125-
{renderSparkleOverlay({ id, title, description, type, image, row })}
129+
{renderSparkleOverlay({
130+
card: { id, title, description, type, image, row },
131+
indexes,
132+
})}
126133
</div>
127134
</div>
128135
);
129136
}
137+
138+
// render sparkle overlay function takes a card and index to then return a sparkle on the specified indexes

0 commit comments

Comments
 (0)